美文网首页
类的继承之新增、定制

类的继承之新增、定制

作者: Iphone60Plus | 来源:发表于2020-03-28 12:58 被阅读0次
class Student:
    # 初始化函数,为每个实例创建4个参数(其中后3个参数有默认值)
    def __init__(self, name, job=None, time=0.00, time_effective=0.00): 
        self.name = name
        self.job = job
        self.time = time
        self.time_effective = time_effective

    def count_time(self, hour, rate):
        self.time += hour
        self.time_effective += hour * rate  # 有效时间=投入时间×学习效率

class Aaa(Student):# 创建子类继承
    # 修改初始化方法,注意:job进行赋值为字符串’programmer‘
    def __init__(self,name,job='programmer',time=0.00,time_effective=0.00):
        self.name = name
        self.job = job
        self.time = time
        self.time_effective = time_effective
    # 修改父类方法,rate=1
    def count_time(self,hour,rate=1):
        self.time += hour
        self.time_effective += hour * rate  # 有效时间=投入时间×学习效率

student1 = Student('韩梅梅')
print(student1.job)
student1.count_time(10, 0.8)  # 学习效率为0.8
print(student1.time_effective)

student2 = Aaa('张三')# 子类的实例化
print(student2.job)
student2.count_time(10)
print(student2.time_effective)

相关文章

  • 类的继承之新增、定制

  • python零基础13:类的继承和定制

    类的定制和继承是什么? 类的继承 类的定制 类的继承要怎么写? 继承基础语法 继承之多层继承 继承的多重继承 类的...

  • Python 面向对象高级编程

    使用__slots__ 使用@property 多重继承 定制类 使用枚举类 使用元类

  • 接口的继承与抽象类

    接口继承 接口继承(inheritance)与类继承很类似,就是以被继承的 interface 为基础,增添新增的...

  • 面对对象高级编程

    面向对象高级编程: 面向对象编程:封装、继承和多态 面向对象高级编程:多重继承、定制类和元类

  • 9月18日-4期C语言学习总结

    今天学习:继承与派生 派生类对象由两部分组成:1.由基类继承成员,2.派生类新增加的自己特有的成员。 类的继承方式...

  • iOS系统新特性

    IOS5的新特性iCloud定制UIStoryboardARCCoreImagel滤镜新增Json解析类 IOS6...

  • CardView使用教程

    Android 5.0 版本中新增了CardView,CardView 继承自FrameLayout类,并且可以设...

  • 面向对象的三大特性

    继承:对象的一个新类可以从现有的类中派生,派生类可以从它的基类那继承方法和实例变量,且派生类可以修改或新增新的方法...

  • Swift5.1继承

    13.继承 重写1.重写:⼦类可以为继承来的实例方法,类方法,实例属性,类属性,或下标提供⾃己定制的实现。我们把这...

网友评论

      本文标题:类的继承之新增、定制

      本文链接:https://www.haomeiwen.com/subject/qpbjuhtx.html