dog例子開頭
class Dog:
def __init__(self,name):
self.name = name
def bulk(self):
print("%s:wang wang wang!"%self.name)
d1 = Dog("陳榮華")
d2= Dog("陳三炮")
d3=Dog("陳老炮")
d1.bulk()
d2.bulk()
d3.bulk()
CS遊戲
class Role(): #類名
n = 123 #類變量 用途:大家共用的屬性
n_list=[]
name = "我是類name"
def __init__(self, name, role, weapon, life_value=100, money=15000):
#構造函數
#在實例化時做一些類的初始化的工作
self.name = name #實例變量(靜態屬性) 作用域就是實例本身
self.role = role
self.weapon = weapon
self.__life_value = life_value # 加_ _ 私有屬性
self.money = money
def __del__(self):
print("%s 徹底掛了。。。"%self.name)
def __shot(self): #類的方法(函數) 功能(動態屬性)
print("shooting...") #__shot(self) 私有方法
def got_shot(self):
print("%s ah...,I got shot..."%self.name)
def buy_gun(self, gun_name):
print("%sjust bought %s" %(self.name,gun_name))
def show_status(self): #定義訪問私有屬性的方法 私有屬性內部可以訪問
self.__life_value -=50
r1.got_shot()
print("name:%s,weapon:%s life_value:%s"%(self.name,self.weapon,self.__life_value))
#print(Role.n)
r1 = Role('Alex', 'police', 'AK47')#把一個類變成一個具體對象的過程叫實例化 實例化(初始化一個類,造了一個對象)
r1.buy_gun("ak47")
r1.got_shot()
print(r1.show_status()) #訪問私有屬性
#print(r1.__life_value) 私有屬性之後外面訪問不了
#del r1 驗證析構函數
#r1.__shot() 私有方法,外面訪問不了
r2 = Role('jack', 'terrorist', 'B22')
r2.got_shot()
'''
r1.name="陳榮華" #修改屬性
r1.bullet_prove=True #添加屬性
r1.n="改的類變量" #相當於給r1新加了一個屬性
r1.n_list.append("from r1")
print(r1.weapon)
del r1.weapon #刪除屬性
#print(r1.weapon)
print("r1:",r1.n,r1.name,r1.bullet_prove) #變量先找實例本身,在去類裏面找
#
#r1.got_shot()
r2 = Role('jack', 'terrorist', 'B22') #生成一個角色 Role的實例
r2.name="徐良偉"
print("r2:",r2.n,r2.name)
r2.n_list.append("from r2")
#r2.got_shot() #Role.got_shot(r2)
#r1.buy_gun("AK47")
Role.n="ABC"
print(Role.n)
print(r1.n,r2.n) #改的類變量只對r2有效
print(r2.n_list) #對於類的列表,類,實例共用同一塊內存地址,所以改了實例改了類列表,對類和其他實例都有效,都會跟着改
print(Role.n_list)
'''
繼承詳解
#class People: 經典類
class People(object): #新式類
def __init__(self,name,age):
self.name=name
self.age=age
self.friends=[]
def eat(self):
print("%s is eating...."%self.name)
def talking(self):
print("%s is talking..."%self.name)
def sleep(self):
print("%s is sleeping..."%self.name)
class Relation(object): #沒有構造函數就去父類People裏面找obj.name
def make_friends(self,obj): #調用的時候已經實例化完成
print("%s is making friend with %s"%(self.name,obj.name))
self.friends.append(obj)
class Man(Relation,People): #繼承People 多繼承
'''
def __init__(self,name,age,money): #初始化的時候要將父類的參數寫進去 加上money 重構了父類
#People.__init__(self,name,age) #調用父類的方法 經典類寫法
super(Man,self).__init__(name,age) #兩種方法 推薦用這種方法 新式類寫法
self.money=money #添加的參數
print("%s 一出生就有%s money"%(self.name,self.money))
'''
def piao(self):
print("%s is piaoing...20s...done"%self.name)
def sleep(self):
People.sleep(self) # 重構父類方法 添加新功能 self執行父類傳進去
print("man is sleeping")
class Women(People,Relation): #多繼承
def get_birth(self):
print("%s is born a baby..."%self.name)
#m1=Man("Niuhanyang",22,10)
m1=Man("Niuhanyang",22)
#m1.eat()
#m1.piao()
#m1.sleep()
w1 = Women("chengronghua",26)
#w1.get_birth()
m1.make_friends(w1) #調用make_friends的時候已經實例化完成
w1.name="chensanpao"
print(m1.friends[0].name)
本節筆記
#類變量用途:共用的屬性,節省開銷
class Person:
cn="中國"
def __init__(self,name,age,addr,cn="china"):
self.name = name
#p1=Person(name,age,addr)
#析構函數:在實例釋放,銷燬的時候自動執行的,通常用於做一些收尾工作,如關閉一些數據庫連接,關閉打開的臨時文件,也叫析構方法
#私有方法,私有屬性
#繼承 節省代碼
#3.X版本統一按廣度優先來繼承(先找橫向的)
class A:
def __init__(self):
print("A")
class B(A):
pass
# def __init__(self):
# print("B")
class C(A):
pass
# def __init__(self):
# print("C")
class D(B,C):
pass
# def __init__(self):
# print("D")
obj=D()
#2.X版本 class A:經典類按深度優先來繼承(先縱向方向找) class A(object):新式類按廣度優先來繼承
#廣度優先效率高