面向对象之组合

组合

# 组合 就是一个对象拥有一个属性,该属性的值是另外一个对象
解决类与类之间的代码冗余问题
	1.继承:学生继承了人类,所以学生是人,同理推出满足什么是什么的关系
  2.

组合举例1)

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

class Bar():
    def __init__(self,n):
        self.n = n

obj = Foo(10)
# 新增一个对象
obj1 = Bar(100)
# 再obj1的名称空间中新增一个y = 9
obj1.y = 9
# 对象的名称空间中新增 x = 1
obj.x = 1
print(obj.__dict__) # {'m': 10, 'x': 1}
print(obj1.__dict__) # {'n': 100, 'y': 9}

组合举例2)

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


class Bar():
    def __init__(self, n):
        self.n = n


obj = Foo(10)
# 新增一个对象
obj1 = Bar(100)
# 对象的名称空间中新增 x = obj1所指向的名称空间地址
obj.x = obj1
# 所以obj.x = obj1,那么想要拿到Bar的数据和obj.n是同理
print(obj.x.n)  # 100
# 此时obj同样也可以取到自己Foo的数据
print(obj.m)  # 10
print(obj.__dict__)  # {'m': 10, 'x': <__main__.Bar object at 0x7fe16c2bb2b0>}

组合深度举例3)

class People():
    school = 'SH'

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender


# 学生类
class Student(People):

    def __init__(self, name, age, gender, course=None):
        if course is None:
            course = []
        People.__init__(self, name, age, gender)
        self.course = course

    def choose_course(self, course):
        self.course.append(course)
        print('%s 选课成功 %s' % (self.name, self.course))


class Teacher(People):

    def __init__(self, name, age, gender, level):
        self.level = level
        People.__init__(self, name, age, gender)

    def score(self, stu_obj, score):
        stu_obj.score = score
        print('%s 给 %s 打了 %s 分' % (self.name, stu_obj.name, score))


# s1 = Student('junkie', 18, 'nan', 'python')
# # print(s1.course)
# t1 = Teacher('junjie', 19, 'nv', 5)
# print(t1.level)

def student_user_info():
    name = input('请输入您的名字>>>').strip()
    age = input('请输入您的年纪>>>').strip()
    gender = input('请输入您的性别>>>').strip()
    s1 = Student(name, age, gender)

    while True:
        cs = input('请输入您选择课程(q is out)>>>').strip()
        if cs == 'q': break
        s1.course.append(cs)
        continue

    return s1


import json
import os
import settings

s1 = student_user_info()


def write(s):
    path = os.path.join(settings.BASE_DB, '%s.json' % s.name)
    with open(path, 'w', encoding='utf8') as f:
        json.dump(s.__dict__, f)


write(s1)

组合深度举例4)

class People():
    school = 'SH'

    def __init__(self, name, age, gender, ):
        self.name = name
        self.age = age
        self.gender = gender


class Admin(People):
    pass

# 再定义一个父类
class Course():
    def __init__(self, name, period, price, ):
        self.name = name
        self.period = period
        self.price = price


python = Course('python', '6mon', 10000)
linux = Course('linux', '5mon', 20000)


class Student(People, Course):
    def __init__(self, name, age, gender, course=None):
        if course is None:
            course = []
        self.courses = course
        super().__init__(name, age, gender, )

    def choose_course(self, stu_obj, course):
        stu_obj.courses.append(course)


stu = Student('junjie', 18, 'male')
stu.courses.append(python)
for course in stu.courses:
    print(course.name)
    print(course.price)
    print(course.period)
posted @ 2021-12-07 22:26  谢俊杰  阅读(33)  评论(0编辑  收藏  举报