Static attribute:
1 class Room: 2 tag = 1 3 def __init__(self,name,owner,width,length,height): 4 self.name = name 5 self.owner = owner 6 self.width = width 7 self.length = length 8 self.height = height 9 10 @property 11 def area(self): 12 # print('%s live in %s with the aera %s square' % (r1.owner, r1.name, r1.width * r1.length)) 13 return self.width * self.length 14 def test(self): 15 print("from test",self.name) 16 @classmethod #provide for class 17 def tell_info(cls): #cls 18 print(cls) 19 print("--->>",cls.tag) 20 # def tell_info(self): 21 # print("------>>>",self.tag) 22 @staticmethod # no conbination,the tool bags of class 23 def wash_body(a, b, c): 24 print('%s %s %s is taking shower' % (a, b, c)) 25 26 # def test1(x,y): 27 # print(x,y) #no use 28 29 # Room.wash_body('alex', 'liu', 'chicken') 30 # r1 = Room('bathroom', 'alex', 100, 100, 10000) 31 # r1.wash_body('alex', 'liu', 'chicken') 32 33 # r1 = Room('bathroom','alex',100,100,10000) 34 # r2 = Room('garden','zxver',100,100,10000) 35 # # r1.area() 36 # # r1.area 37 # print(r1.area) #hidden the backstage logical function 38 # print('%s live in %s with the aera %s square'%(r1.owner,r1.name,r1.width*r1.length)) 39 # print(Room.tag) 40 # Room.tell_info(r1) #use self must hava a instantiation 41 Room.tell_info() #use @classmethod will send the Room attribute to the bracket,it will be used 42 #when the class want use the attribute in itself.
Combination:
1 # class Hand: 2 # pass 3 # 4 # class Foot: 5 # pass 6 # 7 # class Trunk: 8 # pass 9 # 10 # class Head: 11 # pass 12 # 13 # class Person: 14 # def __init__(self,id_num,name): 15 # self.id_num = id_num 16 # self.name = name 17 # self.hand = Hand() 18 # self.foot = Foot() 19 # self.head = Head() 20 # self.trunk =Trunk() 21 # 22 # p1 = Person('11111','alex') 23 # print(p1.__dict__) 24 25 class School: 26 def __init__(self,name,addr,type, area,rent): 27 self.name = name 28 self.addr = addr 29 self.type = type 30 self.area = area 31 self.rent = rent 32 self.course_list = [] 33 def recruit_teacher(self): 34 pass 35 def dismiss_teacher(self): 36 pass 37 def recruit_student(self): 38 pass 39 def dismiss_student(self): 40 pass 41 def open(self): 42 pass 43 def close(self): 44 pass 45 def open_branch(self): 46 pass 47 48 class Teacher: 49 def __init__(self,name,age,gender,city,education,major,hobby,salary): 50 self.name = name 51 self.age = age 52 self.gender = gender 53 self.city = city 54 self.education = education 55 self.major = major 56 self.hobby = hobby 57 self.salary = salary 58 self.school = School() 59 def teach(self): 60 pass 61 def internship(self): 62 pass 63 64 class Class: 65 def __init__(self,name,number): 66 self.name = name 67 self.number = number 68 def open(self): 69 pass 70 def close(self): 71 pass 72 73 class Course: 74 def __init__(self,name,fee,period): 75 self.name = name 76 self.fee = fee 77 self.period = period 78 79 80 def open(self): 81 pass 82 83 def close(self): 84 pass 85 86 class Student: 87 def __init__(self,name,age,gender,addr,grade,school): 88 self.name = name 89 self.age = age 90 self.gender = gender 91 self.addr = addr 92 self.grade = grade 93 self.school = school 94 def study(self): 95 pass 96 def compile(self): 97 pass 98 99 s1 = School('oldboy','chongqing','selfish','nanchuan',10000) 100 s2 = School('oldboy','chongqing','selfish','fuling ',10000) 101 s3 = School('oldboy','chongqing','selfish','wulong',10000) 102 103 # c1 = Course('python',10,'1h',s1) #s1 include the information of the school 104 # # print(c1.__dict__) 105 # # print(c1.school) 106 # # print(c1.school.name) 107 # msg = ''' 108 # 1 oldboy nanchuan 109 # 2 oldboy fuling 110 # 3 oldboy wulong 111 # ''' 112 # while True: 113 # 114 # menu = { 115 # '1':s1, 116 # "2":s2, 117 # "3":s3 118 # } 119 # choice = input('please choose area >>:') 120 # school_obj = menu[choice] 121 # name = input('create course >>:') 122 # fee = input('the fee >>:') 123 # period = input('the period >>:') 124 # 125 # new_course = Course(name,fee,period,school_obj) 126 # print('the course %s is belonging to %s school'%(new_course.name,new_course.school.area)) 127 128 129 #>>>>>>>>>the low method to create the choosing courses system: 130 # c1 = Course('pyhton','30','24h') 131 # c2 = Course('pyhton','20','12h') 132 # print(s1.course_list.append(c1)) 133 # print(s1.course_list.append(c2)) 134 # print(s1.__dict__) 135 # for course_obj in s1.course_list: 136 # print(course_obj.name)
Simple inherit:
1 class Dad: 2 'this is father class' 3 money = 10 4 def __init__(self,name): 5 print('dad') 6 self.name = name 7 8 def hit(self): 9 print("%s is punching his son "%self.name) 10 11 class Son(Dad): 12 money = 1000000000 13 def __init__(self,name,age): 14 self.name = name 15 self.age = age 16 17 print(Dad.money) 18 print(Dad.__dict__) 19 print(Son.__dict__) 20 # s1 = Son('alex') 21 s1 = Son('alex',18) #the priority 22 print(s1.name) 23 print(s1.money) 24 s1.hit() 25 26 27 #>>>>>>>>>>>>>>>>>>>:the useage of combination and inherit: 28 ''' 29 1: conbination:when the class is remarkblly different,and the lower stage need the conponents in the bigger stage 30 2:inherit:when there are many common functions between the class,and extract the common functions to make a base class . 31 32 '''
Port inherit:
1 import abc #realize the port inherit 2 3 class All_file(metaclass=abc.ABCMeta): 4 @abc.abstractmethod #>>>>>>>use this to restrict the use of the function,if without it will report wrong 5 def read(self): 6 #the base class do not have to accomplish,but the lower class must define it 7 pass 8 9 @abc.abstractmethod 10 def write(self): 11 #the base class do not have to accomplish,but the lower class must define it 12 pass 13 #>>>>>>port inherit requires the lower class to state all the fuction which in the higher class 14 class Disk(All_file): 15 def read(self): 16 print('disk read') 17 def write(self): 18 print('disk write') 19 class Cdrom(All_file): 20 def read(self): 21 print('cd read') 22 def write(self): 23 print('cd write') 24 class Mem(All_file): 25 def read(self): 26 print('mem read') 27 def write(self): 28 print('menm write') 29 30 31 m1 = Mem() 32 m1.read() 33 m1.write()
the inherit sequence:
Call father level class:
1 #>>>>>>>>>the method is not good ,cause if the father class change names ,it will be difficult to change 2 #>>>the inner code 3 4 #it provide another way:super() 5 6 7 class Vehicle: 8 Country = 'china' 9 def __init__(self,name,speed,load,power): 10 self.name = name 11 self.speed = speed 12 self.load = load 13 self.power = power 14 15 def run(self): 16 print('engine>>>') 17 18 class Subway(Vehicle): 19 def __init__(self,name,speed,load,power,line): 20 # Vehicle.__init__(self,name,speed,load,power) 21 # super().__init__(name,speed,load,power) 22 super(Subway,self).__init__(name,speed,load,power) 23 # self.name = name 24 # self.speed = speed 25 # self.load = load 26 # self.power = power 27 self.line = line 28 29 def show_info(self): 30 print(self.name,self.line) 31 32 def run(self): 33 # Vehicle.run(self) 34 super().run() 35 print('%s %s line is departing'%(self.name,self.line)) 36 37 line6 = Subway('line6','368km/h',100000000,'electricity',6) 38 print(line6.name) 39 line6.show_info() 40 line6.run()