python學習筆記 7 更加抽象

2022-05-01 09:42:08 字數 4154 閱讀 7331

#!/usr/bin/env python

# -*- coding: utf-8 -*- # 指定原始碼檔案編碼,python預設為ascii

__metaclass__ = type # 確定使用新式類

class person:

def setname(self, name): # self 為是當前例項的引用

self.name = name

def getname(self):

return self.name

def greet(self):

print "hello, world! i'm %s." % self.name

def __inaccessible(self): # 私有函式,外界不可以訪問

print "bet you can't see me..."

def _inaccessible(self): # 不會被import匯出

print "bet you can't see me..."

foo = person() # 建立物件

bar = person()

foo.setname("foo")

bar.setname("bar")

print foo.getname()

print bar.getname()

foo.greet()

bar.greet()

#foo.__inaccessible()

foo._inaccessible()

print "------------------------"

''''''

class membercounter:

print "class membercounter begin defined"

members = 0

members2 = 0

def init(self):

membercounter.members += 1

membercounter.members2 += 1

m1 = membercounter()

m1.init()

m2 = membercounter()

m2.init()

print "----"

print "m1.members", m1.members

print "m2.members", m2.members

print "membercounter.members", membercounter.members

print "----"

print "modify m1.members"

m1.members='m1'

print "----"

print "m1.members", m1.members

print "m2.members", m2.members

print "membercounter.members", membercounter.members

print "----"

print "modify membercounter.members"

membercounter.members = "membercounter"

print "----"

print "m1.members", m1.members

print "m2.members", m2.members

print "membercounter.members", membercounter.members

print "----"

print "create m3"

m3=membercounter()

print "----"

print "m1.members", m1.members, "m1.members2", m1.members2

print "m2.members", m2.members, "m2.members2", m2.members2

print "m3.members", m3.members, "m3.members2", m3.members2

print "membercounter.members", membercounter.members, "membercounter.members2", membercounter.members2

print "----"

print "modify membercounter.members2"

membercounter.members2= "members2"

print "----"

print "m1.members", m1.members, "m1.members2", m1.members2

print "m2.members", m2.members, "m2.members2", m2.members2

print "m3.members", m3.members, "m3.members2", m3.members2

print "membercounter.members", membercounter.members, "membercounter.members2", membercounter.members2

print "----"

m3.members2="m3.members2"

membercounter.members2= "members2 last"

print "----"

print "m1.members", m1.members, "m1.members2", m1.members2

print "m2.members", m2.members, "m2.members2", m2.members2

print "m3.members", m3.members, "m3.members2", m3.members2

print "membercounter.members", membercounter.members, "membercounter.members2", membercounter.members2

print "----"

''''''

print "---------------------"

class filter:

def init(self):

self.blocked = # 建立成員變數

def filter(self, sequence):

return [x for x in sequence if x not in self.blocked]

class spamfilter(filter): # 繼承,可以是逗號分隔的多個基類

def init(self): # 過載

self.blocked = ['spam']

f = filter()

f.init()

print f.filter([1,2,3])

s = spamfilter()

s.init()

print s.filter(["spam", "spam","spam","spam", "eggs", "bacom" ])

print "---------------------"

class c:

def dis(self):

self.c=1

print self.c

c=c()

c.dis()

c.cc=3

print "c.cc", c.cc

print "c.__dict__", c.__dict__

print "c.__dict__", c.__dict__

print "filter.__dict__", filter.__dict__

print "f.__dict__", f.__dict__

print "s.__dict__", s.__dict__

print "m1.__dict__", m1.__dict__

print "m2.__dict__", m2.__dict__

print "m3.__dict__", m3.__dict__

print "membercounter.__dict__", membercounter.__dict__

Python學習筆記(七)更加抽象

本章主要介紹內容為建立自己的物件 7.2 建立自己的類 首先看使用乙個簡單的類 metaclass type class person def setname self,name self.name name def getname self return self.name def greet s...

python學習(七) 更加抽象

python是物件導向的語言。7.1 物件的魔力 7.1.1 多型 不管是字串還是列表,count 函式都可以正常工作。ab b c count c 1 abc count c 1下面的加號 運算子對於整數和列表都可以起作用,而且行為不同 1 2 3 fsafda fsdfs fsafdafsdfs...

python之更加抽象 2014 4 6

更加抽象 12 50pm 14 50 p112 1.物件的魔力 多型 如count 在多種資料型別中都可以實現計數的功能 封裝 對全域性作用域中其他區域隱藏多餘資訊的原則 繼承2.類和型別 建立類class person def setname self,name self.name name de...