python面向对象编程(1)

:::tips
1、定义一个表示时间的类Time,要求它提供下面操作:
(1)Time(hours,minutes,seconds)创建一个时间对象
(2)t.hours()、t. minutes()和t. seconds()分别返回时间对象t的小时、分钟和秒钟值
(3)使用运算符==和<判断两个时间对象的相等和小于关系
(4)使用运算符+和-为Time对象定义加法和减法操作,例如t1+t1表示基于当前时间t1向后延长一段t2时间,而t1-t2则表示基于当前时间t1向前提前一段t2时间。
提示:(3)和(4)要应用运算符重载
:::

class Time:
    def __init__(self,hour,minute,second):
        self.hour=hour
        self.minute=minute
        self.second=second
    def hours(self):
        print(f"该时间的小时数:{self.hour}")
    def minutes(self):
        print(f"该时间的分钟数:{self.minute}")
    def seconds(self):
        print(f"该时间的秒数:{self.second}")
    def __eq__(self,other):
        return (self.hour==other.hour and self.minute==other.minute and self.second==other.minute)
    def __lt__(self,other):
        return (self.hour<other.hour or (self.hour==other.hour and self.minute<other.minute) or (self.hour==other.hour and self.minute==other.minute and self.second<other.second))
    def __add__(self,other):
        a=self.hour+other.hour
        b=self.minute+other.minute
        c=self.second+other.second
        if c>=60:
            c=c-60
            b=b+1
        if b>=60:
            b=b-60
            a=a+1
        if a>=24:
            a=a-24
        return print(f"相加后的时间:{a}:{b}:{c}")
    def __sub__(self,other):
        a=self.hour-other.hour
        b=self.minute-other.minute
        c=self.second-other.second
        if c<0:
            c=60+c
            b=b-1
        if b<0:
            b=60+b
            a=a-1
        if a<0:
            a=a+24
        return print(f"相减后的时间:{a}:{b}:{c}")
t=Time(2,31,21)
f=Time(2,38,31)
t.hours()
t.minutes()
t.seconds()
print(f"两个时间相等吗:{t==f}")
print(f"时间t小于时间f吗:{t<f}")
t+f
t-f

:::tips
2、假设
校成员类(SchoolMember)具有成员的姓名(name)、年龄(age)、性别(sex)属性。
教师类(Teacher)继承了学校成员类,该类还记录了教师的职称(title)、工资(salary)、奖金(prize)等信息。
学生类(Teacher)也继承了学校成员类,该类还记录了学生所属系部(dept)、成绩(score)等信息。
请定义以上3个类,要求同时满足如下功能:
(1)创建教师和学生对象时需要调用父类方法进行必要的初始化
(2)能反映学校成员总人数的增减变化,即每创建一个对象时,则总人数加1;而对象注销时,总人数减1
(3)能够计算每位教师的总收入(工资+奖金),虽然不允许直接访问总收入,但可以通过提供相应的方法返回总收入值
(4)能使用print()函数输出每个对象的相关信息
:::

class SchoolMember:
    count= 0
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
        SchoolMember.count += 1
    def a_infor(self):
        print("姓名:"+self.name+" 年龄:"+"{}".format(self.age)+" 性别:"+self.sex,end=' ')
    def counting(self):
        print(SchoolMember.count)
    def a_del(self):
        SchoolMember.count -= 1
        del self
    
class Teacher(SchoolMember):
    def __init__(self,name,age,sex,title,salary,prize):
        super().__init__(name,age,sex)
        self.title=title
        self.salary=salary
        self.prize=prize
    def  __income(self):
        return(self.salary+self.prize)
       
    def  t_infor(self):
        super().a_infor()
        print("职位:老师"+" 职称:"+self.title+" 工资:"+"{}".format(self.salary)+" 奖金:"+"{}".format(self.prize))
    
    def t_del(self):
        
        del self
        print(1)
class Student(SchoolMember):
    def __init__(self,name,age,sex,dept,score):
        super().__init__(name,age,sex)
        self.dept=dept
        self.score=score   
    def s_infor(self):
        super().a_infor()
        print("职位:学生"+" 系部:"+self.dept+" 成绩:"+"{}".format(self.score))


student1=Student('小明',20,"男","计算机工程学院",95)
student1.s_infor()
student1.counting()

teacher=Teacher('aaa',50,'男',"教授",10000,800)
teacher.t_infor()
teacher.counting()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值