# __*__ coding: utf-8 __*__ # __author__ = "David.z" # dict ={ # 1:{"name":"alex", # "role":"terrorist", # "weapon":"C33", # "life_value":"100", # "money":15000,}, # 1:{"name":"alex", # "role":"terrorist", # "weapon":"C33", # "life_value":"100", # "money":15000,}, # 2:{"name":"tom", # "role":"terrorist", # "weapon":"B25", # "life_value":"100", # "money":15000,}, # 3:{"name":"jack", # "role":"police", # "weapon":"B51", # "life_value":"100", # "money":15000,}, # 4:{"name":"lily", # "role":"police", # "weapon":"G31", # "life_value":"100", # "money":15000,}, # } # print(dict[1]) # print(dict[2]) # print(dict[3]) # print(dict[4]) class Role(object): n=123 n_list=[] 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#这个是一个私有属性,如果要调用,需要在里面在加一个方法调用,如def show_status() self.money = money def __del__(self): pass #print("%s彻底死了。。。"%self.name) def show_status(self): #这里就是我在调用私有属性,同理,如果要用私有方法,也是加__ print("name:%s weapon:%s life_value:%s"%(self.name, self.weapon, self.__life_value )) return "hello" def shot(self): print("%s shooting..."%self.name) def got_shot(self): print("ah...,%s got shot..."%self.name) def buy_gun(self,gun_name): print("%s just bought %s"%(self.name,gun_name)) r1 = Role('Alex',"police",'AK47') #实例化,(初始化一个类,造了一个对象) r1.n="r1变量改了" r1.n_list.append("r1添加") r2 = Role('Jack',"terrorist",'B22') #生成一个角色 r2.n="r2变量改了" Role.n = "类变量改了" r2.n_list.append("r2添加") print(r1.show_status()) # print(Role.n,r1.got_shot(),r1.n) # print("类:",Role.n_list) # print("r1:",r1.n_list) # print("r2:",r2.n_list) # print(Role.n,r2.buy_gun("狙击"),r2.n)
今天开始学习面向对象和面向过程的两种编程方式。
其实我们之前开始写的脚本类的编程,都可以叫做面向对象的编程。
那么什么面向对象,借用Alex的话,世间万物都有是有类可以分的。这里我们就引出了类。
面向对象包括:对象、封装、继承、多态的特性。
类里面我们说有对象和实例化。还包括构造函数和析构函数。
构造函数: def __init__(self):
析构函数: def __del__(self):
然后讲到了类的继承。
比如有一个父类People ,两个子类,一个Man ,一个Women。
我们说子类可以调用父类里的方法,当子类和父类的方法有同名时,也可以可以通过
把父类的方法先传进去。
但是两个子类直接的方法的是不可以调用的。
1 # __*__ coding: utf-8 __*__ 2 # __author__ = "David.z" 3 class People: 4 def __init__(self,name,age): 5 self.name = name 6 self.age = age 7 def eat(self): 8 print("%s is eating..."%self.name) 9 def sleep(self): 10 print("%s is sleeping..."%self.name) 11 def talk(self): 12 print("%s is talking..."%self.name) 13 14 class Man(People): #继承父类 15 def piao(self): 16 print("%s is piaoing......20s....done"%self.name) 17 18 def sleep(self): 19 People.sleep(self) #把父类方法线传进去 20 print("子类man is sleeping") 21 22 class Woman(People): 23 def get_birth(self): 24 print("%s is born a baby..."%self.name) 25 m1 = Man("ZhangSan",22) 26 m1.eat() 27 m1.sleep() 28 m1.piao() 29 w1 = Woman("ChenRonghua",26) 30 w1.get_birth()
其实这种是单继承,还有多继承,就是继承2个类或者多个类。
如代码
1 # __*__ coding: utf-8 __*__ 2 # __author__ = "David.z" 3 # class People:#经典类 4 5 class People(object):#新式类 6 7 def __init__(self,name,age): 8 self.name = name 9 self.age = age 10 def eat(self): 11 print("%s is eating..."%self.name) 12 def sleep(self): 13 print("%s is sleeping..."%self.name) 14 def talk(self): 15 print("%s is talking..."%self.name) 16 17 class Relation(object): 18 def __init__(self): 19 print (self.name) 20 21 friends = [] 22 def make_friends(self,obj): 23 print("%s is making friends with %s "%(self.name,obj.name)) 24 self.friends.append(obj) 25 26 class Man(People,Relation): #继承父类 27 # def __init__(self,name,age,money): 28 # # People.__init__(self,name,age) 29 # super(Man, self).__init__(name,age)#继承父类的,可以上面那样写,也可以这样写,新式类写法 30 # self.money = money 31 # print ("%s 一出生就有%s 元钱"%(self.name,self.money)) 32 def piao(self): 33 print("%s is piaoing......20s....done"%self.name) 34 35 def sleep(self): 36 People.sleep(self) #把父类方法线传进去 37 print("子类man is sleeping") 38 39 class Woman(People,Relation): 40 def get_birth(self): 41 print("%s is born a baby..."%self.name) 42 m1 = Man("ZhangSan",22) 43 # m1.eat() 44 # 45 # m1.sleep() 46 # m1.piao() 47 48 w1 = Woman("ChenRonghua",26) 49 w1.get_birth() 50 m1.make_friends(w1) 51 w1.name = "陈三炮" 52 print(m1.friends[0].name)
这里说一下py2和py3还有新式类和经典类的继承方式。
里面包含两个概念,广度优先和深度优先。
默认py3里面经典类和新式类都是广度优先的方式。
而py2里面经典类是深度优先,新式类才是广度优先。
如图。
1 class A(object):#现在是py3,也可以变成新式类加object 2 def __init__(self): 3 print("A") 4 class B(A): 5 pass 6 # def __init__(self): 7 # print("B") 8 class C(A): 9 # pass 10 def __init__(self): 11 print("C") 12 class D(B,C): 13 pass 14 # def __init__(self): 15 # print("D") 16 17 obj = A() 18 obj = B() 19 obj = C() 20 obj = D()