8.28 day25

組み合わせ

組み合わせは何ですか?オブジェクトの属性である組み合わせは、別のクラスのオブジェクトであります

class Foo:
    def __init__(self, bar):
        self.bar = bar

class Bar:
    pass

# f=Foo()
bar=Bar()
# f=Foo(Bar())
f=Foo(bar)

これは最も単純な組み合わせの一つ、それを得ることができるものの組み合わせですか?

最も有用な組み合わせは、冗長コードを削減することです

私たちは親と二つのサブカテゴリーの組み合わせを定義する最初のことはできません。

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 = '浦之星学院'

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

これは、コードの冗長性の多くを軽減します

だから、どのように我々はそれを組み合わせて使うのですか?ここではまだ、学校、カリキュラム、教師、生徒の例を引用

class Person:
    school = '音乃木坂学院'
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):
    # course=[]  #错误
    def __init__(self,name,age):
        self.name=name
        self.age=age
        # course是课程对象,表示学生选的课程
        self.course_list = []
    def choose_course(self,course):
        # self.course=[]  #错误
        #把课程对象追加到学生选课的列表中
        self.course_list.append(course)

    def tell_all_course(self):
        #循环学生选课列表,每次拿出一个课程对象
        for course in self.course_list:
            #课程对象.name  取到课程名字
            print(course.name)

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



course=Course('Python',20199,7)
stu1=Student('nick',19)
stu1.choose_course(course)
stu2=Student('王二丫',19)
stu2.choose_course(course)
stu2.choose_course(Course('linux',19999,5))

だから、どのように我々は、オブジェクトSTU1を介してすべてのコースの名前を見ていますか?

この方法の一つ

通常の関数により、

def tell_all_course(student):
    for course in student.course_list:
        print(course.name)

# tell_all_course(stu1)
tell_all_course(stu2)

方法二

バインディングMethodオブジェクトで

stu1.tell_all_course()

多型と多型

多型は何ですか?多型は、食品の様々な形態です。例えば、動物はブタ、イヌ、人々です

多型は、実際のタイプを考慮することなく、実施例の使用を指し

ポリモーフィックな利点:

プログラムの柔軟性を増やします

プログラムのスケーラビリティを向上

多型塩基

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)

最初の方法

ABCは、インターフェース統一、制約コードを実装するために使用されるが、一般的に使用されていないことができます

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('哼哼哼')
class Dog(Animal):
    def yy(self):
        print('汪汪')

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


pig=Pig()
pig.speak()

# people = People()    # 会报错
# people.zz()

しかし、これは多型を利用することはできません

第二の方法

例外処理と実装(共通)

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)   # 主动抛出异常

ダックタイピングを提唱

限り(オブジェクトがバインドメソッドを持っている)、そして、あなたはアヒルのあるアヒルのように歩き

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)

Linuxの

#传统写法
class File:
    def read(self):
        pass
    def write(self):
        pass
#内存类
class Memory(File):
    def read(self):
        print('Memory...read')
    def write(self):
        print('Memory...write')

class Network(File):
    def read(self):
        print('Network...read')
    def write(self):
        print('Network...write')
# 鸭子类型的写法

# 内存类
class Memory:
    def read(self):
        print('Memory...read')

    def write(self):
        print('Memory...write')

class Network:
    def read(self):
        print('Network...read')

    def write(self):
        print('Network...write')

def read(obj):
    obj.read()
m = Memory()
n = Network()
read(m)
read(n)

パッケージ

それが何を意味するのパッケージ?
、パッケージ自体を理解することの意味と
、それは袋に伴い、袋、子猫、子犬、少し野郎をもたらしたかのようにパッケージをした後、袋に穴を封止します

、内部のものを梱包した後、隠された、ではない外部からのアクセスを非表示にする方法

コード内で非表示にする方法
隠し属性/非表示方法は、内部にアクセスすることができるだけ、ではなく、外部からのアクセスを非表示にした後、

隠し属性:変数名__によって非表示にします

隠蔽方法:メソッド名__によって非表示にします

安全のために隠されたプロパティ

#name 隐藏起来
# 隐藏属性是为了安全
class Person:
    def __init__(self, name, age):
        self.__name=name
        self.__age=age
    def get_name(self):
        # print(self.__name)
        return '[----%s-----]'%self.__name

p=Person('nick',89)
print(p.age)
#访问name
print(p.name)
print(p.__name)
print(p.get_name())
#隐藏的属性访问不到?实际上有方法能访问到
#通过变形隐藏了属性
print(p._Person__name)

print(p.__dict__)
#隐藏方法:隔离复杂度

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

p=Person('nick',89)
p.__speak()
print(Person.__dict__)
p._Person__speak()

#什么时候属性变形,只要再类内部,以__变量名 命名的变量,都会被隐藏,会发生的变形,在外部放入的  __变量名 属性是不隐藏的
class Person:
    def __init__(self,name,age):
        self.__name=name
        self.__age=age
    def set_xx(self,xx):
        self.__xx=xx

p=Person('nick',18)
# ._p_xx="xxx"

p.set_xx('6688')
print(p.__dict__)
#计算人的bmi指数
#property装饰器:把方法包装成数据属性
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('lqz',1.82,70)
# print(p.bmi())
print(p.bmi)
# p.name='ppp'
p.bmi=90

プロパティのセッターとデリータ

class Person:
    def __init__(self,name,height,weight):
        self.__name=name
        self.__height=height
        self.__weight=weight
    @property
    def name(self):
        return '[我的名字是:%s]'%self.__name
    #用property装饰的方法名.setter
    @name.setter
    def name(self,new_name):
        # if not isinstance(new_name,str):
        if type(new_name) is not str:
            raise Exception('改不了')
        if new_name.startswith('sb'):
            raise Exception('不能以sb开头')
        self.__name=new_name

    # 用property装饰的方法名.deleter
    @name.deleter
    def name(self):
        # raise Exception('不能删')
        print('删除成功')
        # del self.__name

p=Person('lqz',1.82,70)
# print(p.name)
# p.name='pppp'
# p.name='xxx'
#改不了,直接抛一异常
# p.name=999
# p.name='sb_nick'

# print(p.name)

del p.name
print(p.name)

おすすめ

転載: www.cnblogs.com/hyc123/p/11426189.html