The object-oriented python of encapsulation, polymorphism, and type of ducks

The default type

class A:

    class_name = 'python23期'

    def __init__(self, name, age):

        self.name = name
        self.age =age

a1 = A('李业', 21) # 实例化一个a1的对象
print(a1.name)

a2 = A('李东宇', 24) # 实例化一个a2的对象
print(a2.age)

Package

The encapsulating a lot of data to ⼀ object in. The code fixing function package to ⼀ code blocks, functions, objects, packaged into modules which belong thought encapsulated Specifically concrete study. For example you write ⼀ a B is a function of ⽜ that this may also be referred to as encapsulation. ⾯ to a subject in mind. ⽆ is seemingly the more crucial ⼀ combination of content from the system to ⼀ ⼀ storing anything and Use. this is the package.

Polymorphism

There are a variety of forms such as water affairs

What is polymorphic: default python support polymorphism, is a variety of data types may be defined, without specifying the type of data to which it belongs, may be strings, lists, and other arbitrary data type tuple

python中定义变量不用规定变量的类型
a = 'alex'
a = [1, 2, 3]
a = (22, 33)


Java
int a = 12 # a必须是整型
String b = 'abc'# b必须是一个字符串类型的

Ducks type

python, you look like a duck, then you are a duck

That is, independent of each other two classes, in itself is nothing to do, and then they have a common internal name func, this is duck

format

class A:

    def login(self):
        pass

    def register(self):
        pass


class B:

    def login(self):
        pass

    def register(self):
        pass
# A,B两个类,没有任何关系,独立两个,但是里面的功能相似,所以python一般会将类似于A,B两个类
# 里面的相似的功能让其命名相同.
# 1. A,B虽然无关系,但是很默契的制定了一个规范.让你使用起来更方便.

super

format

class A:
    def f1(self):
        print('in A f1')

    def f2(self):
        print('in A f2')


class Foo(A):
    def f1(self):
        # super().f2()
        super(Foo, self).f2()
        print('in A Foo')


obj = Foo()
obj.f1()
# 执行结果
in A f2
in A Foo


class A:
    def f1(self):
        print('in A')

class Foo(A):
    def f1(self):
        super(Foo,self).f1()
        print('in Foo')  # 2

class Bar(A):
    def f1(self):
        print('in Bar')  # 1

class Info(Foo,Bar):

    def f1(self):
        super(Info,self).f1()
        print('in Info f1')  # 3

obj = Info()
print(Info.mro())  # [Info, Foo, Bar, A]
obj.f1()
# 执行结果
[<class '__main__.Info'>, <class '__main__.Foo'>, <class '__main__.Bar'>, <class '__main__.A'>, <class 'object'>]
in Bar
in Foo
in Info f1

super () method in the strict sense is not the implementation of the parent class.

Single inheritance: super () method is certainly the implementation of the parent class.

Multiple inheritance: super (S, self) strictly in accordance with the order of execution of self mro subordinate class, the next a *** S class.

Constraint class

# 版本二 统一接口
class Wecht:

    def pay(self,money):
        print(f'利用微信支付了{money}')


class Alipay:

    def pay(self, money):
        print(f'利用支付宝支付了{money}')


def pay(obj, money): # 定义一个统一化的设计
obj.pay(money)

obj1 = Wecht()
obj2 = Alipay()

pay(obj1,200)
pay(obj2,300)

# 输出结果
利用微信支付了200
利用支付宝支付了300

# 版本三 野路子写法

# 版本四 按照之前的代码逻辑进行改变

发现问题:
以上代码没有约束,原因就是想怎么写就怎么写,都能实现当时的功能(个人理解)
在上面的情况下(在一些重要的逻辑,与用户数据相关等核心部分),我们要建立一种约束,避免发生此类错误

There are two constraints class solutions

  1. Establish a constraint in the parent class
  2. Using the concept of abstract classes (a designated specification)

The first solution (recommended way)

In the method of the parent class defines a pay, active throw an exception, if the child class does not define pay method, and

再建立一个父类pay的方法,约定俗称定义一种规范,子类要定义pay方法,但是没有强制性,还是可以执行
    raise 主动报错提醒
class Payment:
    def pay(self,money):
        raise Exception('必须得定义这个类')

class Wechat(Payment):

    def pay(self,money):
        print(f'利用微信支付了{money}')


class Alipay(Payment):

    def pay(self, money):
        print(f'利用支付宝支付了{money}')

class QQpay(Payment):

    def fuqian(self, money):
        print(f'利用QQ支付了{money}')

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

obj1 = Wechat()
obj2 = Alipay()

pay(obj1,200)
pay(obj2,300)

obj3 = QQpay()
pay(obj3,599)

# 输出结果
Traceback (most recent call last):
  File "/Users/wuqiang/work/PycharmProjects/python23/day24/day24.py", line 50, in <module>
    pay(obj3,599)
  File "/Users/wuqiang/work/PycharmProjects/python23/day24/day24.py", line 41, in pay
    obj.pay(money)
  File "/Users/wuqiang/work/PycharmProjects/python23/day24/day24.py", line 22, in pay
    raise Exception('必须得定义这个类')
Exception: 必须得定义这个类

The second solution

Using the concept of an abstract class, the base class is provided as before, if not defined subclass pay method, when the instance of the object will throw an error

from abc import ABCMeta,abstractmethod
class Payment(metaclass=ABCMeta):
    @abstractmethod
    def pay(self, money):
        pass

class Wecht(Payment):

    def pay(self,money):
        print(f'利用微信支付了{money}')


class Alipay(Payment):

    def pay(self, money):
        print(f'利用支付宝支付了{money}')

class QQpay(Payment):

    def fuqian(self, money):
        print(f'利用QQ支付了{money}')
 
obj3 = QQpay()
# 输出结果
    obj3 = QQpay()
TypeError: Can't instantiate abstract class QQpay with abstract methods pay

Guess you like

Origin www.cnblogs.com/zanao/p/11227551.html