1 #class People: #經典類 2 class People(object): #新式類 3 def __init__(self,name,age): 4 self.name=name 5 self.age=age 6 self.friends=[] 7 def eat(self): 8 print("%s is eating..." %self.name) 9 def talk(self): 10 print("%s is talking..." %self.name) 11 def sleep(self): 12 print("%s is sleeping..." %self.name) 13 14 class Relation(object): 15 def make_friends(self,obj): 16 print("%s is making friends with %s"%(self.name,obj.name)) 17 self.friends.append(obj) #不能使用obj.name,不然obj的name修改以後不能同步到朋友列表中 18 19 #子類1 20 class Man(People,Relation): #多類繼承,繼承順序爲從左到右依次繼承 21 #添加新變量 22 def __init__(self,name,age,money): 23 #People.__init__(self,name,age) #此方法在多繼承的時候沒法使用 24 #或者使用下面方法繼承父類參數 25 super(Man,self).__init__(name,age) 26 self.money=money 27 print("%s 一出生就有 %s money"%(self.name,self.money)) 28 29 def piao(self): 30 print("%s is piaoing...20s...done" %self.name) 31 #給父類方法添加新功能 32 def sleep(self): 33 People.sleep(self) #保留父類的方法 34 print("man is sleeping") 35 #子類2 36 class Woman(People,Relation): 37 def get_birth(self): 38 print("%s is born a baby..."%self.name) 39 40 #子類1的實例 41 m1=Man("NiuHanYang",22,10) 42 m1.eat() 43 m1.piao() 44 m1.sleep() 45 46 #子類2的實例 47 w1=Woman("ChenRonghua",26,) 48 w1.get_birth() 49 50 m1.make_friends(w1) 51 print(m1.friends[0].name)
1 #定義一個類, class是定義類的語法,Role是類名,(object)是新式類的寫法,必須這樣寫 2 class Role(object): 3 #類變量 4 n=123 5 name="我是類name" 6 n_list=[] 7 8 #初始化函數,在生成一個角色時要初始化的一些屬性就填寫在這裏 9 def __init__(self, name, role, weapon, life_value=100, money=15000): 10 #構造函數:在實例化時作一些類的初始化工做 11 self.name = name #實例變量(靜態屬性),做用域就是實例自己 12 self.role = role 13 self.weapon = weapon 14 self.__life_value = life_value #私有屬性,格式爲__xxx,對外隱藏,類內可見 15 self.money = money 16 17 #析構函數,函數名爲__xxx__,程序執行完畢以後再執行 18 def __del__(self): 19 print("%s 完全死了....." %self.name) 20 21 #類的方法,功能(動態屬性) 22 def show_status(self): 23 print("name:%s weapon:%s life_val:%s"%(self.name,self.weapon,self.__life_value)) 24 def got_shot(self): 25 print("ah...,I got shot...") 26 def buy_gun(self, gun_name): 27 print("%s just bought %s" %(self.name,gun_name)) 28 #私有方法 29 def __shot(self): 30 print("shooting...") 31 32 #生成一個角色,即實例化(初始化一個類) 33 r1 = Role('Alex','police','AK47') 34 #修改實例變量 35 r1.name="陳榮華" 36 print(r1.n,r1.name) 37 r1.n_list.append("from r1") 38 #給實例添加新的值 39 r1.bullet_prove=True 40 print(r1.n,r1.name,r1.bullet_prove) 41 #刪除實例的屬性之一 42 del r1.weapon 43 #修改類變量(隻影響本實例) 44 r1.n="改類變量" 45 print("r1:",r1.n) 46 #類變量從新賦值(隻影響r2) 47 Role.n="ABC" 48 49 #生成另一個實例 50 r2 = Role('Jack','terrorist','B22') 51 print(r2.n,r2.name) 52 r2.name="徐良偉" 53 r2.n_list.append("from r2") 54 print(r2.n,r2.name,r2.n_list) 55 print(Role.n_list)
1 class School(object): 2 def __init__(self,name,addr): 3 self.name = name 4 self.addr = addr 5 self.students =[] 6 self.staffs =[] 7 def enroll(self,stu_obj): 8 print("爲學員%s 辦理註冊手續"%stu_obj.name ) 9 self.students.append(stu_obj) 10 def hire(self,staff_obj): 11 self.staffs.append(staff_obj) 12 print("僱傭新員工%s" % staff_obj.name) 13 14 class SchoolMember(object): 15 def __init__(self,name,age,sex): 16 self.name = name 17 self.age = age 18 self.sex = sex 19 def tell(self): 20 pass 21 22 class Teacher(SchoolMember): 23 def __init__(self,name,age,sex,salary,course): 24 super(Teacher,self).__init__(name,age,sex) 25 self.salary = salary 26 self.course = course 27 def tell(self): 28 print(''' 29 ---- info of Teacher:%s ---- 30 Name:%s 31 Age:%s 32 Sex:%s 33 Salary:%s 34 Course:%s 35 '''%(self.name,self.name,self.age,self.sex,self.salary,self.course)) 36 37 def teach(self): 38 print("%s is teaching course [%s]" %(self.name,self.course)) 39 40 class Student(SchoolMember): 41 def __init__(self,name,age,sex,stu_id,grade): 42 super(Student,self).__init__(name,age,sex) 43 self.stu_id = stu_id 44 self.grade = grade 45 def tell(self): 46 print(''' 47 ---- info of Student:%s ---- 48 Name:%s 49 Age:%s 50 Sex:%s 51 Stu_id:%s 52 Grade:%s 53 ''' % (self.name, self.name, self.age, self.sex, self.stu_id, self.grade)) 54 def pay_tuition(self,amount): 55 print("%s has paid tution for $%s"% (self.name,amount) ) 56 57 58 school = School("老男孩IT","沙河") 59 60 t1 = Teacher("Oldboy",56,"MF",200000,"Linux") 61 t2 = Teacher("Alex",22,"M",3000,"PythonDevOps") 62 63 s1 = Student("ChenRonghua",36,"MF",1001,"PythonDevOps") 64 s2 = Student("徐良偉",19,"M",1002,"Linux") 65 66 67 t1.tell() 68 s1.tell() 69 school.hire(t1) 70 school.enroll(s1) 71 school.enroll(s2) 72 73 print(school.students) 74 print(school.staffs) 75 school.staffs[0].teach() 76 77 for stu in school.students: 78 stu.pay_tuition(5000)
1 class Animal: 2 def __init__(self, name): # Constructor of the class 3 self.name = name 4 5 def talk(self): # Abstract method, defined by convention only 6 pass #raise NotImplementedError("Subclass must implement abstract method") 7 8 @staticmethod 9 def animal_talk(obj): 10 obj.talk() 11 12 class Cat(Animal): 13 def talk(self): 14 print('Meow!') 15 16 17 class Dog(Animal): 18 def talk(self): 19 print('Woof! Woof!') 20 21 22 d = Dog("陳榮華") 23 #d.talk() 24 25 c = Cat("徐良偉") 26 #c.talk() 27 # 28 # def animal_talk(obj): 29 # obj.talk() 30 31 Animal.animal_talk(c) 32 Animal.animal_talk(d)