table of Contents
combination
1. What is the combination?
Combination refers to a property in an object, another object.
2. Why use a combination?
Combinations and inherit the same purpose, in order to reduce code redundancy.
3. How to use a combination of:
# 选课系统: 老师类,学生类,老师与学生都有名字,年龄,性别
# class People:
# def __init__(self, name, age, sex, year, month, day):
# self.name = name
# self.age = age
# self.sex = sex
# self.year = year
# self.month = month
# self.day = day
#
# def tell_birth(self):
# print(f'''
# === 出生年月日 ===
# 年: {self.year}
# 月: {self.month}
# 日: {self.day}
# ''')
#
#
# class Teacher(People):
# def __init__(self, name, age, sex, year, month, day):
# super().__init__(name, age, sex, year, month, day)
#
#
# class Student(People):
# def __init__(self, name, age, sex, year, month, day):
# super().__init__(name, age, sex, year, month, day)
#
#
# tea1 = Teacher('tank', 17, 'male', 2002, 1, 1)
# tea1.tell_birth()
#
# stu1 = Student('大壮壮', 69, 'female', 1950, 11, 11)
# stu1.tell_birth()
# 组合实现:
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('tank', 17, 'male')
date_obj = Date(2002, 1, 1)
# date_obj.tell_birth()
# 将date对象赋值到tea1对象的date属性中
tea1.date = date_obj
tea1.date.tell_birth()
Summary:
Inheritance:
the relationship between class and class, what kind of what is the relationship between child and parent class affiliation.
Combinations:
the relationship between subject and object, what kind of what the relationship between an object has another object.
Combined Exercises
'''
选课系统需求:
1.学生类,老师类, 学生和老师都有课程属性, 每一门课程都是一个对象.
课程: 课程名字,课程周期,课程价钱
2.学生和老师都有选择课程的功能, 还有打印所有课程的功能.
'''
class People:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
# 添加一门课程方法
def add_course(self, course_obj):
self.course_list.append(course_obj)
# 打印当前对象中所有课程的课程信息
def tell_all_course(self):
# 拿到当前对象的课程列表,列表中存放的是一个个的课程对象
for course_obj in self.course_list:
# 每一个课程对象调用查看课程信息的方法
course_obj.tell_course_info()
class Teacher(People):
def __init__(self, name, age, sex):
super().__init__(name, age, sex)
# 用来存放课程对象
self.course_list = []
class Student(People):
def __init__(self, name, age, sex):
super().__init__(name, age, sex)
self.course_list = []
# 课程类
class Course:
def __init__(self, course_name, course_period, course_price):
# 课程名称\周期\价格
self.course_name = course_name
self.course_period = course_period
self.course_price = course_price
def tell_course_info(self):
print(f'''
课程名称: {self.course_name}
课程周期: {self.course_period}
课程价钱: {self.course_price}
''')
tea1 = Teacher('tank', 17, 'male')
stu1 = Student('张全蛋', 20, 'female')
python_obj = Course('python', 6, 2.0)
linux_obj = Course('linux', 6, 1.0)
# 添加python与linux课程
tea1.add_course(python_obj)
tea1.add_course(linux_obj)
# tea1.course_list[0].tell_course_info()
tea1.tell_all_course()
Package
1. What is the package?
Metaphor:
letters: for example, put a bag sealed up.
Packing: for example, put a bunch of cats, dogs, nick loaded into the bag
package refers to a bunch of attributes (characteristics and skills) package to An object
purpose is to take the data stored in the object can get property manner. "".
metaphor: object like a bag, the bag was inside the pile properties.
2. package Why?
purpose packaged for easy access ., how properties can be obtained by the object attribute.
3. how packaging
features: variable ---> data attributes
skills: function ---> method properties
within a class, defines bunch attributes (characteristics and skills).
through the object. = attribute value attribute
Access restriction mechanism
1. What is the access restriction mechanism?
Defined within the class, all data attributes and methods to attribute the beginning of __,
will be hidden inside the python, so that the outside can not "direct" access to the internal properties that begin __ classes.
For example: = __name__ 'Tank'
? 2. the purpose of access restriction mechanism of
a bunch of privacy attributes and attribute can not be easily accessed by external, can be hidden, not directly call external.
benefits:
logical important data obtained more rigorous, thereby ensuring data security.
Interface: privacy attributes can be done in the business logic interface processing, and then returns the data by encapsulating an interface to the caller.
Note: in python, does not enforce access restrictions property, class internal __ the beginning of the property, just do a variation.
For direct access, the name can be invoked after deformation.
class Foo:
__name__ = 'Tank' # ---> _ __ class name attribute name
# # demo1:
# class Foo:
#
# # 数据属性
# __name = 'tank'
#
# # 方法属性
# def __run(self):
# print('running..')
#
# # 接口: 获取数据接口
# def get_name(self):
# return self.__name
#
# def set_name(self):
# self.__name = 'jason_sb'
#
# foo = Foo()
# # print(foo.__name)
# # res = foo.get_name()
# # print(res)
# # print(foo._Foo__name) # --> _Foo__name
# foo.set_name()
# print(foo.get_name())
# demo2:
# class Teacher:
#
# def __init__(self, name, age, sex):
# self.__name = name
# self.__age = age
# self.__sex = sex
#
# # 接口: 打印用户信息接口
# def get_info(self):
# # 用户认证
# user = input('user: ').strip()
# pwd = input('pwd: ').strip()
# if user == 'tank' and pwd == '123':
# print(f'''
# 姓名: {self.__name}
# 年龄: {self.__age}
# 性别: {self.__sex}
# ''')
#
# # 接口: 修改用户信息接口
# def set_info(self, name, age, sex):
# # str1 = 'string' # str('string')
# if not isinstance(name, str):
# raise TypeError('名字必须要使用字符串')
#
# if not isinstance(age, int):
# raise TypeError('年龄必须是数字')
#
# if not isinstance(sex, str):
# raise TypeError('性别必须要使用字符串')
#
# self.__name = name
# self.__age = age
# self.__sex = sex
#
#
# tea1 = Teacher('tank', 17, 'male')
# # tea1.get_info()
# tea1.set_info('jason_sb', 101, 'female')
# tea1.get_info()
# demo3: 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
1. What is the property
Python built-in decoration, the main method is to use an internal class.
2. Why use a property
using its purpose, is the internal class methods (def method name ()) becomes (def method ).
when an object calls the method, the object method () becomes a target. method (look for a single common data attributes)
3. how to use the property
the @Property
'''
计算人体的bmi: bmi值 = 体重 / (身高 * 身高)
'''
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)
# print(p.bmi()) # 打印动词,看起来不合理
# print(p.bmi) # ---> p.bmi()
# 注意: 不能对被装饰过的方法属性修改.
# p.bmi = 18
# 了解: 若真要通过此方法修改属性,可以通过另一种方式修改.
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
1. What is polymorphism?
Polymorphism refers to the various forms of the same thing.
2. The purpose of polymorphism:
polymorphism, also known as polymorphism, inheritance, polymorphism is the manifestation in the program.
Polymorphism the 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: the definition of a uniform set of standards.
subclass: follow uniform standards parent.
multi . the ultimate goal of the state: the preparation of a unified sub-class specifications, in order to allow users to more easily call the same method functions
3. how to achieve:
- inheritance
Note: in python, not mandatory subclass must follow a parent class standard, hence the abstract class.
class Animal:
# 吃
def eat(self):
pass
# 喝
def drink(self):
pass
# 叫
def speak(self):
pass
# 猪
class Pig(Animal):
# 吃
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('汪汪汪~~~')
# 正确教材
pig = Pig()
cat = Cat()
dog = Dog()
pig.speak()
cat.speak()
dog.speak()
# 反面教材
# class Animal:
# # 吃
# def eat(self):
# pass
#
# # 喝
# def drink(self):
# pass
#
# # 叫
# def speak(self):
# pass
#
#
# # 猪
# class Pig(Animal):
#
# # 吃
# def eat(self):
# print('猪在吃饭')
# pass
#
# # 喝
# def drink(self):
# pass
#
# def jiao(self):
# print('哼哼哼~~~')
#
#
# # 猫
# class Cat:
# # 吃
# def eat(self):
# print('猫在吃饭')
# pass
#
# # 喝
# def drink(self):
# pass
#
# def han(self):
# print('喵喵喵~~')
#
#
# # 狗
# class Dog:
# # 吃
# def eat(self):
# print('狗在吃饭')
# pass
#
# # 喝
# def drink(self):
# pass
#
# def speak(self):
# print('汪汪汪~~~')
#
#
# pig = Pig()
# cat = Cat()
# dog = Dog()
#
# pig.speak()
# cat.speak()
# dog.speak()
Abstract class
1. What is?
Abc module abstract_class
2. The purpose of?
Forced subclass must follow a standard set of the parent class.
3. 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
1. What is type duck?
Do not know what the current target is a matter of, but you look like a duck, then you are a duck types
in python, does not recommend the use of mandatory restrictions defined in the abstract class subclass, but it is recommended duck classes follow types
- Inheritance:
poor scalability coupling is too high, the program
- duck type:
low coupling, scalable procedure strong
# 猪
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('汪汪汪~~~')
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))