day21 interface class and abstract class, isolation principle, open closed principle, polymorphism

1. Interface classes and abstract classes:

# Abstract classes and interface classes 
# java programming principles and design patterns
# design pattern programming landmark design methods evolved from java
# singleton pattern
# a class has only one instance
# algorithm introduction calculation method time and space Question authority general

# java
# object-oriented
# java cannot have multiple inheritance
# programming principle
# python
# open closed principle
# open is open to extension
# closed is closed to modification
# dependency inversion principle
# interface isolation principle
# already written program The code is not allowed to be modified ## Example of payment function
# Alipay payment # qq payment # apply_pay # WeChat payment## Create a specification 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('Use Alipay to pay %s yuan'%money)

class QQpay(Payment):
def pay(self,money ):
print('Used qq to pay %s yuan'%money)

class Wechatpay(Payment):
def pay(self,money):
print('Used WeChat to pay %s yuan'%money)
def recharge(self): pass

def pay(a,money):
a.pay(money)

a = Alipay()
a.pay(100)
pay(a,100) # Normalized design: no matter which class object, call the same Function to complete similar functions
q = QQpay()
# q.pay(100)
pay(q,100)
w = Wechatpay()
pay(w,100) # It will report an error when it is used by



abstract classes and interface classes Matter : Establishing a specification
to formulate a metaclass of a class is ABCMeta,
then this class becomes an abstract class (interface class)
# The main function of this class is to establish a specification
. #
# All methods decorated by abstractmethod in abstract classes must be implemented by inherited subclasses
# If not implemented, an error will be reported during the instantiation phase
. #
# Whether it is an abstract class or an interface Class metaclass=ABCMeta cannot be instantiated
# p = Payment() # Error

2. Interface isolation principle:

# What is an interface 
# There is no concept of an interface in python
# Where does the concept of an interface come from?
# java classes do not have multiple inheritance interfaces to implement multiple inheritance
# describe zoo
# can swim, walk, climb trees, fly
# tigers
# frogs
# swans
# monkeys
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
# All flying animals have the characteristics of some flying animals
# All walking animals have some characteristics of walking animals

# The role of the interface class:
# In java, it can satisfy the interface isolation principle, and complete Constraints of multiple inheritance
# In python, the principle of interface isolation is satisfied. Since python itself supports multiple inheritance, there is no need for the concept of interfaces.

# Abstract classes and interface classes
# In python
#There is no difference, they are all used to constrain methods in subclasses
#As long as methods decorated by abstractmethod in abstract classes and interface classes need to be implemented by subclasses
#It should be noted that when there is a When the same function has different functions, multiple interface classes should be used for different constraints

# In java
# Abstract classes and interfaces are completely different
# The essence of abstract classes is that a class is a class and must follow the rules of single inheritance. So 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 interfaces to solve problems

# During the interview
# abstract A class is a specification for defining a class in python
# interface
# When writing code for a company class
# If you encounter an abstract class, remember to implement the corresponding methods one by one according to the specifications in the abstract class.


Three, polymorphism:
# Polymorphism 
# java c++ c# —— Strongly typed language
# Operation between the same data types
# def func(int a):pass
# func('a')

# Shell language —— Weakly typed language
# 1+'1'
# def func(a):pass
# 1 'a' [1,2,3] ()

# Between strong types and weak types - python dynamic strongly typed language
# Do operations between the same data types
# def func (a):pass
class Payment:
def pay(self):pass

class QQpay(Payment):
def pay(self,money):
print('Use qq to pay %s yuan'%money)

class Wechatpay(Payment):
def pay(self,money):
print('Use WeChat to pay %s yuan'%money)
def recharge(self):pass

# def pay(Payment pay_obj,int money): # java polymorphism developed under a class Objects of multiple classes can be passed as parameters here
# pay_obj.pay(money)

# Whether it is 2.* or 3.* of python: it has its own polymorphism effect

# qq_obj = QQpay()
# print(type(qq_obj)) # The data type of an object is the class it is in
# # # qq_obj.pay (100)
# pay(qq_obj,100)
# we_obj = Wechatpay()
# # # we_obj.pay(200)
# pay(we_obj,200)
# def len(object obj):pass
# class len_class:pass
# class tuple( len_pass):pass
# class list(len_pass):pass
# class str(len_pass):pass
# tuple list str dict set

# Duck type
# class QQpay():
# def pay(self,money):
# print('Use qq Paid %s yuan'%money)
#
# class Wechatpay():
# def pay(self,money):
# print('Used WeChat to pay %s yuan'%money)
#
# def pay(pay_obj,money):
# pay_obj.pay(money)

# Index
class list:
def index(self):pass
class str:
def index(self):pass
class tuple:
def index(self):pass
# [].index()
# ''. index()
# ().index()


# Polymorphism and duck typing
# Polymorphism is implemented through inheritance
# java Objects of multiple classes developed under a class can be passed as parameters to a function or method
# In python There is no need to deliberately implement polymorphism in python, because python itself has its own polymorphism effect
# duck type
# does not constrain which method names must be in certain classes through a specific inheritance relationship
# It is a conventional concept to ensure that multiple Similar functions in a class are called the same name


# Review the examples in class to remember concepts
# Object-oriented mind map


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324535418&siteId=291194637