One: What is a normalized design:
First, let's take a look at the following program:
class Alipay(Payment): def pay(self,money): print ( ' Used Alipay to pay %s yuan ' % money) class QQpay: def pay(self,money): print ( ' Use QQ to pay %s yuan ' % money) def pay(a,money): a.pay(money) #Do normalized design a = Alipay() pay(a,100) q = QQpay() pay(q,100)
Normalized design is the requirement: no matter which class of objects, call the same function to complete similar functions,
Two: interface class and abstract class
1. Abstract classes and interface classes are established specifications. If the metaclass of a class is ABCMeta, then this class becomes an abstract class (interface class).
2. All methods decorated by abstractmethod in abstract classes must be implemented by inherited subclasses. If they are not implemented, an error will be reported during the instantiation phase.
3. Neither abstract class nor interface class metaclass=ABCMeta can be instantiated.
from abc import ABCMeta,abstractmethod class Payment(metaclass=ABCMeta): #Abstract class interface class specification and constraints metaclass specifies a metaclass @abstractmethod def pay(self): pass #Abstract method class Alipay(Payment): def pay(self,money): print ( ' Used Alipay to pay %s yuan ' % money) def pay(a, money): a.pay(money) a = Alipay() pay(a,100)
Three: Interface isolation operation
1. What is an interface?
There is no concept of interface in python, so where does the concept of interface come from? Because java classes do not have multiple inheritance, interfaces can implement multiple inheritance. In python, the principle of interface isolation is satisfied. Since python itself supports multiple inheritance, the concept of interface is not needed.
2. Abstract classes and interface classes are the same in python. They are used to constrain the methods in subclasses. As long as the methods in abstract classes and interface classes are decorated by abstractmethod, they all need to be implemented by subclasses. It should be noted that when multiple classes have the same function and different functions, multiple interface classes should be used for separate constraints
3. In java, abstract classes and interfaces are completely different. The essence of an abstract class is still a class. If a class is a class, it must follow the rules of single inheritance. Therefore, if a subclass is constrained by an abstract class, it can only be controlled by a parent class. , When multiple classes have the same function and different functions, java can only use the interface to solve the problem.
from abc import ABCMeta,abstractmethod class FlyAnimal(metaclass=ABCMeta): @abstractmethod def fly(self):pass @abstractmethod def cal_flying_speed(self):pass @abstractmethod def cal_flying_height(self):pass class WalkAnimal(metaclass=ABCMeta): @abstractmethod def walk(self):pass class SwimAnimal(metaclass=ABCMeta): @abstractmethod def walk(self):pass class Tiger(WalkAnimal,SwimAnimal): def walk(self):pass def swim(self):pass class Monkey: def walk(self):pass def climb(self):pass class Swan(FlyAnimal,WalkAnimal,SwimAnimal): def swim(self):pass def walk(self):pass def fly(self):pass def cal_flying_speed(self):pass def cal_flying_height(self):pass class Parrot(FlyAnimal): def fly(self):pass def cal_flying_speed(self):pass def cal_flying_height(self): pass