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) # 李四