0
点赞
收藏
分享

微信扫一扫

python面向周天作业

罗蓁蓁 2022-04-17 阅读 45
python

 

class Master(object):
    def __init__(self):
        self.kongfu = "古法煎饼果子配方"  # 实例变量,属性
    def make_cake(self):  # 实例方法,方法
        print("[古法]按照<%s>制作了一份煎饼国子", self.kongfu)
    def dayandai(self):
        print("大烟袋")
class school(object):
    def __init__(self):
        self.kongfu = "现代煎饼果子配方"
    def make_cake(self):  # 实例方法,方法
        print("[现代]按照<%s>制作了一份煎饼果子" % self.kongfu)
    def xiaodayandai(self):
        print("小烟袋")
class Prence(school, Master):  # 继承多个父类
    def __init__(self):
        self.kongfu = "猫氏煎饼果子配方"
    def make_cake(self):
        print("按照<%s>制作了一份煎饼果子" % self.kongfu)


damao = Prence()
print(Prence.__mro__)
print(damao.kongfu)
damao.make_cake()
damao.dayandai()
damao.xiaodayandai()
# 实现一个简单的金融类,要求功能有:
# 父类股票类,控股法输出股票买入和卖出;
# 子类(公募机构)继承自父类,操作输出公募机构买入和卖出;
# 子类(私募机构)继承自父类,操作输出私募机构买入和卖出;
# 创建对象实现相关方法的调用
# 尝试使用尽可能多的方法调用父类方法
# 子类(公募机构)学会了父类的控股法操作
# 子类(私募机构)学会了父类的控股法操作并专研了自己的输出股票和私募买入和卖出(重写)

class guPiao(object):
    def maiRu(self):
        print("控股法股票买入")

    def maiChu(self):
        print("控股法股票卖出")


class gongCuanJiGou(guPiao):
    def maiRu(self):
        print("公募机构买入")

    def maiChu(self):
        print("公募机构卖出")

    def fulei(self):
        guPiao.maiChu(self)
        guPiao.maiRu(self)


class siCuanJiGou(guPiao):
    def maiRu(self):
        print("私募机构买入")

    def maiChu(self):
        print("私募机构卖出")

    def fulei(self):
        guPiao.maiChu(self)
        guPiao.maiRu(self)

    def chongxie(self):
        print("自己研制的方法")


aa = siCuanJiGou()
bb = gongCuanJiGou()

bb.fulei()
bb.maiRu()
bb.maiChu()
aa.fulei()
aa.maiRu()
aa.maiChu()
aa.chongxie()

 

 

 

# 课后作业2

# . 创建一个老师类
# 老师类中有学习的方法
# 老师类中有私有的挣钱的方法,老师类中有私有类属性money = 100000,还有一个公有的类属性age=45
# 创建一个学生类,继承自老师类,并重写了老师类中学习的方法
# 调用学生类中挣钱的方法
# 调用老师类中的私有属性
# 修改老师类中的私有属性为200000
# 调用老师类的私有方法
# 修改类属性(分别影响某一个对象,类)

class teacher(object):
    # 共有的类
    age = 45

    def __init__(self):
        self.__money = 10000

    def study(self):
        print("我是学习方法")

    def __zhengqian(self):
        print("我是挣钱的方法")

    def set_money(self, money):
        self.__money = money

    def get_money(self):
        return self.__money

    def fun1(self):
        print(self.__money)

    def fun2(self):
        self.__zhengqian()


# 继承老师类的学生类
class student(teacher):
    def study(self):
        print("我是学习方法")


a = teacher()
b = student()
b.study()
b.fun1()
b.fun2()
a.set_money(20000)  # 修改
print(a.get_money())
a.age = 200
print(a.age)
 实例方法
class People(object):
    def selfmethod(self):
        print("我是实例方法")


p = People()
p.selfmethod()
People.selfmethod()


# __new__方法
class A(object):
    def __init__(self):
        print("这是init方法")

    def __new__(cls):
        print("这是new方法")
        return object.__new__(cls)


a = A()


# 单例模式
class Foo(object):
    instance = None

    def __init__(self):
        self.name = "中国"

    def __new__(cls):
        if Foo.instance:
            return Foo.instance
        else:
            Foo.instance = object.__new__(cls)
            return Foo.instance


obj1 = Foo()
obj2 = Foo()
print(obj1, obj2)

 

 

 

举报

相关推荐

day17-面向对象作业

Python作业

python作业

python 作业

Python 面向对象

python总结作业

0 条评论