One hundred and sixty thousand

A combination of

1.1 What is a combination

A property of an object, something another object, that object wrapped object.

class Fo:
    pass
class Bar:
    pass
f.Fo()
f.bar = Bar()

1.2 Why use a combination of

    可以减少代码冗余。
class Person:
    school = 'oldboy'

class Teacher(Person): 
    def __init__(self,name,age,level,course_name,course_price,course_period):
        self.name=name
        self.age=age
        self.level=level

class Student(Person):
    def __init__(self,name,age,course,course_name,course_price,course_period):
        self.name=name
        self.age=age
        self.course=course
class Person:
    school = 'oldboy'

class Teacher(Person):
    def __init__(self,name,age,level,course):
        self.name=name
        self.age=age
        self.level=level
        #course是课程对象,表示老师教授的课程
        self.course=course

class Student(Person):
    def __init__(self,name,age,course):
        self.name=name
        self.age=age
        # course是课程对象,表示学生选的课程
        self.course = course

class Course:
    def __init__(self,course_name,course_price,course_period):
        self.name=course_name
        self.price=course_price
        self.period=course_period

1.3 in combination with inheritance

Combined with the inheritance is an important way to efficiently use the resources of the existing class. However, both the concept and usage scenarios are different.

1. inherited way

By inheritance to establish a relationship between the derived class and the base class, it is a 'yes' relationship, such as the white horse is a horse, man is an animal. When there between classes many of the same features, extract these common features make the base class, inheritance is better, such as the teacher is a person, who is a student

2. The combination of the way

In combination with the established relationship between class and class combination, it is a 'have' relationships, such as birthday professors, and professors teach python linux courses on student s1, s2, s3 ...

Conclusion is that the same type of relationship used in combination, and has included the use of inheritance relationships.

Second, polymorphism and polymorphism

2.1 What is a multi-state

Polymorphism: a class of things various forms.

Polymorphism: polymorphism refers to the use examples without considering Examples of types of

States benefit more than 2.2

1. increase the flexibility of the program

2. Add the extension of the program and

class Animal:
    def speak(self):
        pass

class Pig(Animal):
    def speak(self):
        print('哼哼哼')

class Dog(Animal):
    def speak(self):
        print('汪汪')

class People(Animal):
    def speak(self):
        print('say hello')

pig=Pig()
dog=Dog()
people=People()
pig.speak()
dog.speak()
people.speak()
def animal_speak(obj):
    obj.speak()
animal_speak(pig)
animal_speak(people)

Because python advocating freedom and casual, so let the program more flexibility, but this development will bring a lot of inconvenience. So we need to constraint the way; to avoid these situations.

1. To achieve a unified interface to abc, the code constraint (with less).

import abc
#第一在括号中写metaclass=abc.ABCMeta
class Animal(metaclass=abc.ABCMeta):
    #第二在要约束的方法上,写abc.abstractmethod装饰器
    @abc.abstractmethod
    def speak(self):
        pass

class Pig(Animal):
    def speak(self):
        print('哼哼哼')

Consistent use of abc, the method must be defined and abc.abstractmethod decorator method (speak), otherwise it will error.

  1. With exception handling implement (common)
class Animal():
    def speak(self):
        #主动抛出异常
        raise Exception('你得给我重写它啊')
class Pig(Animal):
    def speak(self):
        print('哼哼哼')
class People(Animal):
    def speak(self):
        print('say hello')
pig=Pig()
pe=People()
def animal_speak(obj):
    obj.speak()

animal_speak(pig)
animal_speak(pe)

Use exception handling, may be constrained code when the code in line with the rules occurs, it reported abnormal end code, a reminder to change.

3. Type respect Duck (artificial constraints)

As long walks like a duck (object has a binding method), then you are a duck.

class Pig:
    def speak(self):
        print('哼哼哼')
class People:
    def speak(self):
        print('say hello')

pig=Pig()
pe=People()
def animal_speak(obj):
    obj.speak()
animal_speak(pig)
animal_speak(pe)

Third, the package

3.1 What is the package

Package is to use a series of some procedures, the sealed packaging processes, the package is not the same but hidden.

3.2 How to implement a code-behind

After the hidden attribute / Hide method to hide, not external access, internal only be able to access.

Hidden attributes: __ by the variable name to hide.

#name 隐藏起来
class Person:
    def __init__(self,name,age):
        self.__name=name
        self.__age=age
    def get_name(self):
        # print(self.__name)
        return self.__name

Concealment methods: __ method to hide by name.

class Person:
    def __init__(self,name,age):
        self.__name=name
        self.__age=age
    def __speak(self):
        print('6666')

Hidden property for safety, hiding the complexity of the method is to isolate.

But in fact the hidden attribute is accessed by way of to.

print(p.__dict__)  #查找属性
print(p._Person__name)

3.3 property usage

property is a special property, it will be performed for a function (function) that access and returns the value. In other words, it is a decorative property, a method of packaging into the data attribute.

class Person:
    def __init__(self,name,height,weight):
        self.name=name
        self.height=height
        self.weight=weight
    @property
    def bmi(self):
        return self.weight/(self.height**2)
        # return self.weight/(self.height*self.height)
p=Person(,,,)
# print(p.bmi())
print(p.bmi)

Guess you like

Origin www.cnblogs.com/tangceng/p/11426175.html