Object-oriented three properties: encapsulation, inheritance, polymorphism

Package

Packaging is to use special syntax for member properties and member methods of packaging, to protect and hide the purpose but must pay attention to, can not be a member of all the package dead, meaningless members encapsulated mainly for internal class using the special members of the syntax of the package, have different access rights

Package level

封装的级别
成员 ==> 公有的
_成员 ==> 受保护的 (约定俗成,而python没有具体实现)
__成员 ==> 私有的
公有的 public 受保护的 protected 私有的 private
在类的内部 OK OK OK
在类的外部 OK No(python可以) No

To achieve encapsulation

公有的封装:
定义:默认定义的成员都属于公有成员
特征:公有的成员可以在任何位置进行访问和操作
受保护封装
定义:在成员名称前面加一个下划线 _成员名称
特征:受保护的成员和公有成员一样可以在任何位置进行访问,但是一般不要随便访问和操作受保护成员
私有的封装
定义:在成员名称前面加两个下划线 __成员名称
特征:私有的成员只能在当前类的内部去访问和操作,不能在类的外部进行操作

View an object's member

# 查看对象的所以成员
print(ym.__dict__) # 可以获取当前对象的所有成员信息
# print(Person.__dict__) # 可以获取当前类的所有成员信息

#{'name': '杨幂', '_age': 28, '_Person__sanwei': '60 55 60'}

To understanding:

1. not achieved in python protected package, are convention developer.

The 2.python privatization package is implemented by renaming strategy, not a real privatization

 

inherit

What is inherited?

Heritage cultural heritage, arts, inherited the mantle. . .

Computer inheritance

In object-oriented, a class to inherit the parent class, then the class would have all members (except private members) in the parent class

concept:

Other classes inherited class, also called base class is called the parent class or superclass

Class inherit other classes, this class is called a subclass, also called the derived class

Inherited meaning:

Improved code reuse, a new relationship class and class to facilitate the operation of other logic

Inheritance syntax

class 父类():
pass

class 子类(父类):
pass

Inherited characteristics

1. When you do not specify inherit the parent class, all classes inherit from the object class (system provided) to know

2. subclass inherits the parent class, it has all the members of the parent class, including magic methods (except private members)

3. subclass inherits the parent class, members will not copy the parent class to subclass, away references

4. subclass inherits the parent class can override the method of the parent class, called overwriting

The subclasses override the parent class method, you can still use super (). Parent class to call the parent class method name () way

6. If a subclass parent class definition does not exist, is called the parent class extension

7. parent class can be inherited a plurality of subclasses, inherit the chain may also be present. Chain Inheritance: A class inherits Classes B, C class inherits class B, class C inherits class D. . .

Single and multiple inheritance

Single inheritance

Single inheritance: a class can only inherit a parent's way.

Syntax:

class 父类():
pass

class 子类(父类):
pass

Multiple Inheritance

Multiple inheritance: a class inheritance to multiple parent classes.

Syntax:

class 父亲():
pass

class 母亲():
pass

class 子类(父亲,母亲):
pass

Diamond inheritance (diamond inheritance)

  A
B C
D
# D类去继承了B类和C类,然后B类和C类又分别继承了A类,这种继承关系称为 菱形继承

Question: In this diamond inheritance relationship, the relationship between class and class, and super (the order of call)

'''
在定义类后,程序会自动生成一个继承的列表 MRO (Method Realtion Order) 方法关系列表
MRO列表生成原则:
1. 子类永远在父类的前面
2. 同一等级的类,按照子类中的继承顺序摆放
3. 先子类,后父类的顺序原则,最终的类时系统提供的object类
MRO的调用方法
类名.mro()
'''
C.mro()
# [<class 'C'>, <class 'F'>, <class 'M'>, <class 'HuMan'>, <class 'object'>]

# super()在调用时,并不是查找父类,而是去MRO列表上找上一个类。
# super()方法在调用时,会自动把当前self传入到上一级的类的方法中

Class relationships detection issubclass ()

issubclass () detecting a class is a subclass of another class

# 检测一个类是否是另一个类的子类
res = issubclass(D,B) # True 检测D类是不是B类的子类
res = issubclass(D,C) # True 检测D类是不是C类的子类
res = issubclass(D,A) # True 检测D类是不是A类的子类
res = issubclass(A,D) # False 检测A类是不是D类的子类

Polymorphism

A method for the same, due to the different calling object, generating a result of different forms of

Example:

#  多态 普通版本
# 对于同一个方法,由于调用的对象不同(或者传入的对象不同),最终实现了不同的结果

# 定义电脑类
class Computer():
# 在电脑类中定义一个 sub 的规范的接口 方法
def usb(self,obj):
obj.start()

# 定义鼠标类
class Mouse():
def start(self):
print('鼠标启动成功,可以双击单击嗨起来。。。')

# 定义键盘类
class KeyBord():
def start(self):
print('键盘启动成功了,赶紧双击666。。。')

# 定义 U盘 类
class Udisk():
def start(self):
print('U盘启动了,赶紧检查一下我的种子还在不在。。。')

# 实例化对象
c = Computer() # 电脑对象
m = Mouse() # 鼠标对象
k = KeyBord() # 键盘对象
u = Udisk() # u盘对象


# 把不同的设备插入到电脑的usb的接口中
c.usb(m)
c.usb(k)
c.usb(u)

Guess you like

Origin www.cnblogs.com/niaocaizhou/p/12082260.html