13-2 Object-oriented supplement

Interface classes

c #, java is object-oriented programming language. In the development process, there is a design pattern called the interface.

  • Interfaces: python does not support native
  • Abstract class: python native support of
from abc import abstractmethod,ABCMeta

class Payment(metaclas=ABCMeta): # 元类,默认的元类,type
    @abstractmethod
    def pay(self,money):pass #没有实现这个方法

Specification: abstract class or interface class may

Interfaces: Supports multiple inheritance, all methods of the interface class must not be achieved --Java

Abstract class: does not support multiple inheritance, abstract class method can have some code to achieve --Java

class Wechat(Payment):
    def pay(self,money):
        print(f'已经用微信支付了{money}元')
        
class Alipay(Payment):
    def pay(self,money):
        print(f'已经用支付宝支付了{money}元')
        
class Applepay(Payment):
    def pay(self,money):
        print(f'已经使用applepay支付了{money}元')

def pay(pay_obj,money): # 统一支付入口
    pay_obj.pay(money)

    

# wechat = Wechat()
alipay = Alipay()
# app = Applepay()
# wechat.pay(100)
# alipay.pay(200)
p = Payment()

Multiple inheritance interface class

from abc import abstractmethod,ABCMeta

class Swim_Animal(metaclass = ABCMeta):
    @abstractmethod
    def swim(self):pass
    
class Walk_Animal(metaclass = ABCMeta):
    @abstractmethod
    def walk(self):pass
    
class Fly_Animal(metclass = ABCMeta):
    @abstractmethod
    def fly(self):pass
    
class Tiger(Walk_Animal,Swim_Animal):
    def walk(self):pass
    def swim(self):pass

    
class OldYing(Fly_Animal,Walk_Animal):pass
class Swan(Swim_Animal,Walk_Animal,Fly_Animal):pass

#接口类 刚好满足接口隔离原则,面向对象开发的思想  规范

Abstract class

everything is a file in python

import abc # 利用abc模块实现抽象类

class All_File(metaclass=abc.ABCMeta):
    all_type='file'
    @abc.abstractmethod #定义抽象方法,无序实现功能
    def read(self):
        '''子类必须实现read功能'''
        pass
    
    @abstractmethod # 定义抽象方法,无需实现功能
    def write(self):
        '''子类必须实现write功能'''
        pass
    
class Txt(All_file): # 子类继承抽象类,但是必须定义read方法和write方法
    def read(self):
        print('文本数据的读取方法')
    def write(self):
        print('文本数据的读取方法')
        
class Sata(All_File): # 子类继承抽象类,但是必须定义read和write方法
    def read(self):
        print('硬盘数据的读取方法')
    def write(self):
        print('硬盘数据的读取方法')
        
class Process(All_file): # 子类继承抽象类,但是必须定义read和write方法
    def read(self):
        print('进程数据的读取方法')
    def write(self):
        print('进程数据的读取方法')
        
wenbenwenjian = Txt()
yingpanwenjian = Sata()
jinchengwenjian = Process()

# 这样大家都是被 归一化了,也就是一切皆文件的思想
wenbenwenjian.read()
yingpanwenjian.write()
jinchengwenjian.read()

print(wenbenwenjian.all_type)
print(yingpanwenjian.all_type)
print(jinchengewenjian.all_type)

An abstract class is a development specification.

In general, single inheritance function can be achieved are the same, so the parent class can have some simple basic realization

The case of multiple inheritance: Since the function is more complex, it is not easy to abstract concrete realization of the same functionality written in the parent class

Whether or interface class is an abstract class, are the object-oriented development specification, all classes and interfaces can not be instantiated abstract classes.

Spread

Java: Java in the inheritance of all classes are single inheritance, so the abstract class perfect solution to the problem of single inheritance norms in demand.

With respect to the needs of multiple inheritance, because they do not support Java syntax itself, so creating an interface specification Interface concept to solve the problem of multiple inheritance.

python: python no interface class. python comes with multiple inheritance, so we directly use class to implement the interface class.

Support abstract class in python: single inheritance in general can not be instantiated. python code that can be achieved

Polymorphism

What is polymorphism?

Dynamic python is a strongly typed language. Advocating duck typing.

Advantages: loose coupling between each have no effect similar class.

Disadvantages: too casual, can only rely on conscious

Method classes and objects binding and non binding method

@property

Let function method inside the class becomes a property

@bmi.setter 和 @bmi.deleter

Let this function method bmi () can modify and delete the same property,
so @ bmi.setter @ bmi.deleter decorative function and method names must be the property of decorative function method name bmi (), can not be arbitrarily defined function names, other names will be replaced if error, show the property is (actually a function) can not be modified or deleted.

The method name is arbitrarily defined function class attribute property method can have, this mixed mind.

Guess you like

Origin www.cnblogs.com/chenych/p/11082550.html