class ClassName: <statement-1> . . . <statement-N>
class Person: "Person类" def __init__(self, name, age, gender): print('进入Person的初始化') self.name = name self.age = age self.gender = gender print('离开Person的初始化') def getName(self): print(self.name) p = Person('ice', 18, '男') print(p.name) # ice print(p.age) # 18 print(p.gender) # 男 print(hasattr(p, 'weight')) # False # 为p添加weight属性 p.weight = '70kg' print(hasattr(p, 'weight')) # True print(getattr(p, 'name')) # ice print(p.__dict__) # {'age': 18, 'gender': '男', 'name': 'ice'} print(Person.__name__) # Person print(Person.__doc__) # Person类 print(Person.__dict__) # {'__doc__': 'Person类', '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__init__': <function Person.__init__ at 0x000000000284E950>, 'getName': <function Person.getName at 0x000000000284EA60>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__module__': '__main__'} print(Person.__mro__) # (<class '__main__.Person'>, <class 'object'>) print(Person.__bases__) # (<class 'object'>,) print(Person.__module__) # __main__
class Person: def __init__(self, name, age, gender): print('进入Person的初始化') self.name = name self.age = age self.gender = gender print('离开Person的初始化') def getName(self): print(self.name) # Person实例对象 p = Person('ice', 18, '男') print(p.name) print(p.age) print(p.gender) p.getName() # 进入Person的初始化 # 离开Person的初始化 # ice # 18 # 男 # ice
class Myclass: def foo(self): print(id(self),'foo') a=Myclass()#既然是实例对象,那就要创建实例 a.foo()#输出类里的函数地址 print(id(a))# 输出类对象的地址 #结果地址一样
class Myclass: @classmethod#类装饰器 def foo2(cls): print(id(cls),'foo2') #类对象,直接可以调用,不需要实例化 print(id(Myclass),'yy') Myclass.foo2()#直接可以调用
class Myclass: @staticmethod#静态方法 def foo3(): print('foo3') Myclass.foo3()#没有参数 a.foo3() #结果foo3
class Demo: __id = 123456 def getId(self): return self.__id temp = Demo() # print(temp.__id) # 报错 AttributeError: 'Demo' object has no attribute '__id' print(temp.getId()) # 123456 print(temp._Demo__id) # 123456
class Base: def __init__(self): self.data=[] def add(self,x): self.data.append(x) def addtwice(self,x): self.add(x) self.add(x) # child extends base class Child(Base): def plus(self,a,b): return a+b oChild=Child() oChild.add("str1") oChild.add(999) oChild.addtwice(4) print(oChild.data) print(oChild.plus(2,3))
class ClassName: <statement-1> . . . <statement-N>
class Person: "Person类" def __init__(self, name, age, gender): print('进入Person的初始化') self.name = name self.age = age self.gender = gender print('离开Person的初始化') def getName(self): print(self.name) p = Person('ice', 18, '男') print(p.name) # ice print(p.age) # 18 print(p.gender) # 男 print(hasattr(p, 'weight')) # False # 为p添加weight属性 p.weight = '70kg' print(hasattr(p, 'weight')) # True print(getattr(p, 'name')) # ice print(p.__dict__) # {'age': 18, 'gender': '男', 'name': 'ice'} print(Person.__name__) # Person print(Person.__doc__) # Person类 print(Person.__dict__) # {'__doc__': 'Person类', '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__init__': <function Person.__init__ at 0x000000000284E950>, 'getName': <function Person.getName at 0x000000000284EA60>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__module__': '__main__'} print(Person.__mro__) # (<class '__main__.Person'>, <class 'object'>) print(Person.__bases__) # (<class 'object'>,) print(Person.__module__) # __main__
class Person: def __init__(self, name, age, gender): print('进入Person的初始化') self.name = name self.age = age self.gender = gender print('离开Person的初始化') def getName(self): print(self.name) # Person实例对象 p = Person('ice', 18, '男') print(p.name) print(p.age) print(p.gender) p.getName() # 进入Person的初始化 # 离开Person的初始化 # ice # 18 # 男 # ice
class Demo: __id = 123456 def getId(self): return self.__id temp = Demo() # print(temp.__id) # 报错 AttributeError: 'Demo' object has no attribute '__id' print(temp.getId()) # 123456 print(temp._Demo__id) # 123456
class Base: def __init__(self): self.data=[] def add(self,x): self.data.append(x) def addtwice(self,x): self.add(x) self.add(x) # child extends base class Child(Base): def plus(self,a,b): return a+b oChild=Child() oChild.add("str1") oChild.add(999) oChild.addtwice(4) print(oChild.data) print(oChild.plus(2,3))
以上就是基于python3 类的属性、方法、封装、继承详解的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于基于python3类的属性、方法、封装、继承详解的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did84411