1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
class Person: def __init__(self): self.name='刘备' self.gender='男' self.weight='60' print('An instance created')
p1=Person() print(p1.name) print(p1.gender) print(p1.weight)
''' __dict__ #属性 __doc__ #文档字符串 __name__ #类名 __module__ #类定义所在模块 __bases__ #类所有父类构成元素 '''
class Stu: Name="张三" age=19 stu=Stu() print("学生姓名:%s,年龄:%d"%(stu.Name,stu.age))
class Stu: def __init__(self): self.name="张三" self.stuid=1 def displayCount(self): print("学生姓名:%s,学号%d"%(self.name,self.stuid)) stu=Stu() stu.displayCount()
class Stu: empCount=0 def __init__(self,name,stuid): self.name=name self.stuid=stuid Stu.empCount+=1 def displayCount(self): print("学生总数%d人"%(Stu.empCount)) def displaystu(self): print("name:",self.name,"stuid:",self.stuid) stu=Stu("张三",1) stu.displayCount() stu.displaystu()
class Stu: def __init__(self,name,stuid): self.name=name self.stuid=stuid def __del__(self): print("已释放资源") stu=Stu("张三",1) del stu print("进行垃圾回收")
class JustCounter: __secretCount=0 publicCount=0 def count(self): self.__secretCount+=1 self.publicCount+=1 print(self.__secretCount) couunter=JustCounter() couunter.count() couunter.count() print(couunter.publicCount) print(couunter._JustCounter__secretCount)
class Person: def __init__(self,name,gender='男',weight=70): self.name=name self.gender=gender self.weight=weight print('A person named %s'%self.name) def say(self): print('My name is %s'%(self.name)) class Teacher(Person): def teach(self,lesson): print("%s terchs %s"%(self.name,lesson)) class Student(Person): def study(self,lesson): print("%s studies %s"%(self.name,lesson)) p=Person('关羽','男','80') p.say() t=Teacher('孔子','男','90') t.say() t.teach('python') s=Student('王昭君','女','60') s.say() s.study('python')
class Parent: def myMethod(self): print("调用父类方法") class Child(Parent): def myMethod(self): print("调用子类方法") c=Child() c.myMethod()
class Computation(): def __init__(self,value): self.value=value def __add__(self, other): return self.value+other def __sub__(self, other): return self.value-other c=Computation(5) x=c+5 print("重构后加法运算的结果是:",x) y=c-3 print("重构后减法运算结果是:",y)
''' __init__ __del__ __add__ __radd__ #右侧加法 __iadd__ #实地加法 __sub__ #减法 __or__ __repr__ __str__ #打印,转换 call #调用 getattr__ #点号运算 setattr #属性赋值语句 delattr #属性删除 __getattribute__ #属性获取 __getitem__ #索引运算 __setltem__ #索引赋值语句 __delitem__ #索引和分片删除 __len__ _ _boll_ _ __lt__ __gt__ __le__ __ge__ __ep__ __ne__#特定比较 __iter__ __next__ #迭代环境 __contains__ #成员关系测试 __index__ #整数值 __enter__ __exit__ #环境管理器 __get __sel__ __delete__#描述符属性 __new__ #创建 '''
|