1 # 2 class Person: 3 def __init__(self, name, age, gender): 4 self.name = name 5 self.age = age 6 self.gender = gender 7 8 def show(self): 9 print(self.name) 10 11 12 # 实例化 13 person1 = Person('张三', 18, '男') 14 person2 = Person('李四', 20, '女') 15 person1.show() # 张三 16 person2.show() # 李四
1 class A: 2 @classmethod 3 def print1(self): 4 print('print in class method') 5 6 @staticmethod 7 def print2(): 8 print('print in static method') 9 10 A.print1() # print in class method 11 A.print2() # print in static method
因为python 没有抽象类、接口的概念,因此要实现这种功能得abc.py 这个类库,具体方式以下java
全部方法都必须在子类中实现python
1 import abc 2 3 class AInterface(metaclass=abc.ABCMeta): 4 @abc.abstractmethod 5 def func1(self): pass 6 7 class A(AInterface): 8 def func1(self): pass 9 10 a = A() 11 # 不实现则报错 12 # TypeError: Can't instantiate abstract class A with abstract methods func1
部分方法在子类中实现ide
1 import abc 2 3 class AInterface(metaclass=abc.ABCMeta): 4 @abc.abstractmethod 5 def func1(self): pass 6 7 def func2(self): pass 8 9 class A(AInterface): 10 def func1(self): pass 11 12 a = A() 13 a.func2() 14 # 不实现则报错 15 # TypeError: Can't instantiate abstract class A with abstract methods func1
1 class A: 2 def print(self): 3 print("print from A") 4 5 # B继承A 6 class B(A): pass 7 8 # C继承A 9 class C(A): 10 def print(self): 11 print('print from C') 12 13 b = B() 14 b.print() # print from A 15 c = C() 16 c.print() # print from C
当调用的方法在父类中已经存在同名方法,会默认调用子类的方法.如C类.若是子类中没有,则会调用父类的方法,如B类.函数
1 class A: 2 def print(self): 3 print('print from A') 4 5 6 class B: 7 def print(self): 8 print('print from B') 9 10 11 class C1(A, B): pass 12 13 14 class C2(B, A): pass 15 16 17 # C1类继承A类B类 C2类继承B类和A类 18 c1 = C1() 19 c1.print() # print from A 20 c2 = C2() 21 c2.print() # print from B
一个类继承多个类时,当调用的方法子类中没有时,会按继承顺序(从左到右)依次在父类中寻找并调用spa
子类拥有父类没有的方法或属性code
1 class A: 2 def print(self): 3 print('print from A') 4 5 6 class B(A): 7 def myprint(self): 8 print('print from B') 9 10 11 b = B() 12 b.print() # print from A 13 b.myprint() # print from B
1 class A: 2 def print(self): 3 print("print from A") # print from A 5 4 5 class B(A): 6 def print(self): 7 print('print from B') # print from B 1 8 print(super().print) # <bound method C.print of <__main__.D object at 0x00000000021EC6D8>> 2 9 super().print() 10 11 class C(A): 12 def print(self): 13 print('print from C') # print from C 3 14 print(super().print) # <bound method A.print of <__main__.D object at 0x00000000021EC6D8>> 4 15 super().print() 16 17 class D(B, C): 18 pass 19 20 d = D() 21 d.print() 22 23 # result: 24 # print from B 25 # <bound method C.print of <__main__.D object at 0x00000000021EC6D8>> 26 # print from C 27 # <bound method A.print of <__main__.D object at 0x00000000021EC6D8>> 28 # print from A
python原生支持多态blog
1 public interface Animal { 2 void talk(); 3 } 4 5 public class Dog implements Animal{ 6 @Override 7 public void talk() { 8 System.out.println("wangwang"); 9 } 10 } 11 12 public class Pig implements Animal { 13 @Override 14 public void talk() { 15 System.out.println("aoao"); 16 } 17 } 18 19 public class Main { 20 public static void main(String[] args) { 21 Animal dog = new Dog(); 22 dog.talk(); 23 Animal pig = new Pig(); 24 pig.talk(); 25 } 26 }
1 import abc 2 3 class Animal(metaclass=abc.ABCMeta): 4 @abc.abstractmethod 5 def talk(self): pass 6 7 class Pig(Animal): 8 def talk(self): 9 print('aoao') 10 11 class Dog(Animal): 12 def talk(self): 13 print('wangwang') 14 15 dog = Dog() 16 dog.talk() 17 pig = Pig() 18 pig.talk()
属性\方法名前加双下划线声明私有属性\方法.对应java中private,默认public,无protect.(*在类的外部仍然能够经过实例._类名__属性名调用)继承
1 class Person: 2 def __init__(self, name, age, gender, pwd): 3 self.name = name 4 self.__age = age 5 self.gender = gender 6 self.pwd = pwd 7 8 def __getpwd(self): 9 return self.pwd 10 11 12 person = Person('张三', 18, '男', '123') 13 print(person.name) 14 # print(person.__age) # AttributeError: 'Person' object has no attribute '__age' 15 print(person._Person__age) # 18 16 print(person.__getpwd()) # AttributeError: 'Person' object has no attribute '__getpwd'
@property能够将方法封装成属性接口
1 class Person: 2 def __init__(self, name, age): 3 self.__name = name 4 self.__age = age 5 6 @property 7 def name(self): 8 return self.__name 9 10 @name.setter 11 def name(self, new_name): 12 self.__name = new_name 13 14 @name.deleter 15 def name(self): 16 print('执行删除name操做') 17 18 19 p = Person('张三', 18) 20 print(p.name) # 张三 21 p.name = '李四' 22 print(p.name) # 李四 23 # 只是触发对应deleter装饰的函数,具体操做需在函数类完成 24 del p.name # 执行删除name操做 25 print(p.name) # 李四