table of Contents
python day21
combination
What is a combination
Refers to a combination of attributes in the object, another object is
Why use a combination of
As inheritance and object combination, in order to 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 Student(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('momo', 24, 'female')
date_obj = Date(2018, 8, 17)
tea1.date = date_obj
tea1.date.tell_birth()
Combination Summary
inherit:
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
It refers to a package stack of attributes (characteristics and skills) encapsulated into an object
Purpose is to fetch the stored data objects may be .
the way to obtain properties
Why package
The purpose of the package for easier access, through the object .
to obtain the attributes of attributes embodiment
How to package
Wherein: Variable ---> data attributes
Skills: function ---> method attribute
Within the class defined pile attributes (characteristics and skills)
Via the object .
attribute property value =
Access restriction mechanism
What is the access restriction mechanism
Within the class definition, all in order __
since the beginning of the data attributes and methods Properties will be hidden inside the python, can not let the outside "直接"
inside access __
at the beginning of the property
The purpose of access restriction mechanism
Private property and can not be easily accessed by external attributes, you can hide, not directly call external
advantage:
Logic important data obtained more rigorous, thereby ensuring the security of data
Interface: privacy attributes via an interface package, in doing business logic interface processing, and then returns to the caller data
the Attention : in python, does not enforce restrictions of access to property, inside the class __
attribute beginning, just do a variation, if you want direct access, you can call the name of the deformed
__name = 'momo' #---> _类名__属性名
class ATM:
def __flow(self):
print('打印账单')
def withdraw(self):
self.__flow()
print('访问限制!!')
atm = ATM()
atm.withdraw()
property
What is proprety
The method of python decorators built, mainly to the use of an internal class
Why use property
The purpose of using it, is an internal class method (method name DEF ()) becomes (DEF Method)
In object to call a method, the target .
method () becomes a target .
method (it looks like an ordinary data attributes)
How to use the property
class People:
def __init__(self,name,weight,height):
self.name = name
self.weight = weight
self.height = height
@property
def bmi(self):
return self.height/(self.weight ** 2)
@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('momo',95,1.6)
print(p.bmi)
# 注意: 不能对被装饰过的方法属性修改.
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, still call the same 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
class Animal:
def speak(self):
pass
class Cat(Animal):
def speak(self):
print('喵喵喵~~~~')
class Dog(Animal):
def speak(self):
print('汪汪汪~~~~')
class Pig(Animal):
def speak(self):
print('哼哼哼~~~~')
cat = Cat()
dog = Dog()
pig =pig()
cat.speak()
dog.speak()
pig.speak()
Abstract class
What is an abstract class
abc module abstract_class
purpose of usage
Forced subclass must follow the uniform standards of the parent class
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()