table of Contents
combination
What is a combination
Refers to a combination of attributes in the object, another object is
Why use a combination of
Reduce code redundancy
class People:
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
class Teacher(People):
def __init__(self,name,age,sex)
super().__init(name,age,sex)
class Date:
def __init__(self,year,month,day)
self.year = year
self.month = month
self.day = day
def tell_birth(self)
print(f'''
=== 出生年月日 ===
年: {self.year}
月: {self.month}
日: {self.day}
''')
tea1 = Teacher('tank',14,'male')
date_obj = Date(2005,1,1)
# date_obj.tell_birth()
# 将date对象赋值到tea1对象的date属性中
tea1.date = date_obj
to sum up:
Inheritance: the relationship between class and class, what kind of what is the relationship between child and parent class affiliation
Combination: the relationship between objects and object, what kind of what the relationship between an object has another object
Package
What is the package
metaphor:
Seal: for example, a bag sealed up to
Packing: for example, put a bunch of cats and dogs loaded into the bag
It refers to a pile of package properties (characteristics and skills) encapsulated into an object
Purpose is to fetch the stored data object may '' get property manner
Analogy: an object like a bag, the bag was inside the pile properties
Why package
Purpose is to facilitate access to the package, the object may be acquired by the attribute. Attribute mode
How to package
Wherein: Variable ---> data attributes
Skills: function ---> method attribute
Within the class that defines a bunch of attributes (characteristics and skills).
Through the object. Property attribute value =
Access restriction mechanism
What is the access restriction mechanism
Within the definition of the class, the data attributes and methods everything starting with __ properties, will be hidden
Internal external can not directly access class properties
For example: __name = 'tank'
The purpose of access restriction mechanism
A bunch of privacy attributes and attribute can not be easily accessed by external, can hide shadow, not directly call external
benefit:
Logic important data obtained more rigorous, thereby ensuring the security of data
Interface: privacy attributes via an interface package, do the business logic within the interface, and then
The data is returned to the caller
Note: in python, does not enforce restrictions of access to property, inside the class at the beginning of __ property, but made a deformation
For direct access, call the deformation
class Foo:
__name = 'tank' # --> __类名__属性名
# 例如:ATM
class ATM:
# 1.插卡
def __insert_card(self):
print('插卡')
pass
# 2.输入密码
def __input_pwd(self):
print('密码验证')
pass
# 3.输入取款金额
def __input_money(self):
print('输入金额')
pass
# 4.开始吐钱
def __get_money(self):
print('执行吐钱')
pass
# 5.打印账单
def __print_flow(self):
print('打印账单')
pass
# 取钱直接接口
def withdraw(self):
self.__insert_card()
self.__input_pwd()
self.__input_money()
self.__get_money()
self.__print_flow()
print('取款程序执行完毕!')
atm = ATM()
atm.withdraw()
property
What is the property
python decorator built, mainly to use the method within the class.
Why use property
The purpose of using it, the method is an internal class
How to use the property
@property
class People:
def __init__(self,name,weight,height):
self.name = name
self.weight = weight
self.height = height
@property
def bmi(self):
return self.weight/(self.height*self.height)
# 了解
@property
def get_name(self):
return self.name
# 改
@get_name.setter
def set_name(self, val):
self.name = val
# 删除
@get_name.deleter
def del_name(self):
del self.name
p = People('jason', 200, 1.6)
Note: You can not modify the method of the decorated property
Learn: if we have to modify the properties modified by this method, may be another way to modify
print(p.get_name)
p.set_name = 'tank'
print(p.get_name)
# p.del_name()
# print(p.get_name)
del p.del_name
print(p.get_name)
Polymorphism
What is polymorphic
Polymorphism refers to the various forms of the same thing
Polymorphic object
Polymorphic also known as polymorphism, inheritance, polymorphism is the manifestation in the program
Polymorphic purpose is to allow a variety of different types of objects, in the case of the same function, call the same name method name
Parent Class: defining a set of uniform standards
Subclass: follow uniform standards parent
Polymorphic ultimate goal: the preparation of standardized and unified sub-categories, in order to allow users to more easily call the same function method
How to achieve
Inherit
Note: in python, not mandatory subclass must comply with a standard set of the parent class, so there are abstract classes
Abstract class
What is
abc module abstract_class
The purpose of
Forced subclass must comply with a standard set of parent class
how to use
import abc
import abc
class Animal(metaclass=abc.ABCMeta):
# 吃
@abc.abstractmethod
def eat(self):
pass
# 喝
@abc.abstractmethod
def drink(self):
pass
# 叫
@abc.abstractmethod
def speak(self):
pass
# 猪
class Pig(Animal):
# 吃
def eat(self):
print('猪在吃饭')
pass
# 喝
def drink(self):
pass
def speak(self):
print('哼哼哼~~~')
# 派生
def run(self):
pass
pig = Pig()
Ducks type
What is type duck
In the case where the object is the thing I do not know, but you look like a duck, then you are the type of duck
Ducks do not recommend the use of abstract classes in python enforce restrictions defined subclasses, but sub-classes follow the duck type
# 猪
class Pig:
# 吃
def eat(self):
print('猪在吃饭')
pass
# 喝
def drink(self):
pass
def speak(self):
print('哼哼哼~~~')
# 猫
class Cat:
# 吃
def eat(self):
print('猫在吃饭')
pass
# 喝
def drink(self):
pass
def speak(self):
print('喵喵喵~~')
# 狗
class Dog:
# 吃
def eat(self):
print('狗在吃饭')
pass
# 喝
def drink(self):
pass
def speak(self):
print('汪汪汪~~~')
Inheritance:
Coupling high, poor scalability
Ducks type:
Low coupling, scalable procedure strong
Polymorphic Operating xuanji
# 猪
class Pig:
# 吃
def eat(self):
print('猪在吃饭')
pass
# 喝
def drink(self):
pass
def speak(self):
print('哼哼哼~~~')
# 猫
class Cat:
# 吃
def eat(self):
print('猫在吃饭')
pass
# 喝
def drink(self):
pass
def speak(self):
print('喵喵喵~~')
# 狗
class Dog:
# 吃
def eat(self):
print('狗在吃饭')
pass
# 喝
def drink(self):
pass
def speak(self):
print('汪汪汪~~~')
dog = Dog()
cat = Cat()
pig = Pig()
def BARK(animal):
animal.speak()
BARK(dog)
BARK(cat)
BARK(pig)
str1 = '1234'
list1 = [1, 2, 3]
print(str1.__len__())
print(list1.__len__())
def LEN(d):
return d.__len__()
print(LEN(str1))
print(LEN(list1))
print(len(str1))
print(len(list1))