定义一个人员类People,其属性有:姓名、性别、年龄;基于People实现学生类Student,添加属性:学号、入学时间和入学成绩;基于People实现教师类Teacher,添加属性:职务、部门、工作时间;基于Student实现究生类Graduate,添加属性:研究方向和导师,分别定义其中的构造函数和输出函数。程序中定义各种类的对象,并完成测试。 基于以上类利用接口实现在职研究生类 GradOnWork

 

class People:
    def __init__(self, name='qaz', gender='', age=21):
        self.name = name
        self.gender = gender
        self.age = age

    def display(self):
        print("姓名:" + self.name + "\n性别:" + self.gender + "\n年龄:" + str(self.age))


class Student(People):
    def __init__(self, student_id="20210001", into_time="2021-09-01", into_score="100.0"):
        People.__init__(self, name='qaz', gender='', age=21)
        self.student_id = student_id
        self.into_time = into_time
        self.into_score = into_score

    def display(self):
        print("姓名:" + self.name + "\n性别:" + self.gender + "\n年龄:" + str(
            self.age) + "\n学号:" + self.student_id + "\n入学时间:" + self.into_time + "\n入学成绩:" + str(self.into_score))


class Teacher(People):
    def __init__(self, post="院长", department="软件工程学院", job_time="2000-1-01"):
        People.__init__(self, name='wsx', gender='', age=40)
        self.post = post
        self.department = department
        self.job_time = job_time

    def display(self):
        print("姓名:" + self.name + "\n性别:" + self.gender + "\n年龄:" + str(
            self.age) + "\n职务:" + self.post + "\n部门:" + self.department + "\n工作时间:" + self.job_time)


class Graduate(Student):
    def __init__(self, research_fields="人工智能", teacher="wjm"):
        Student.__init__(self, student_id="20210002", into_time="2021-09-01", into_score="60.0")
        self.research_fields = research_fields
        self.teacher = teacher

    def display(self):
        print("姓名:" + self.name + "\n性别:" + self.gender + "\n年龄:" + str(
            self.age) + "\n学号:" + self.student_id + "\n入学时间:" + self.into_time + "\n入学成绩:" + str(
            self.into_score) + "\n研究方向:" + self.research_fields + "\n导师:" + self.teacher)


class GradOnWork(Teacher, Graduate):
    def __init__(self):
        Teacher.__init__(self, post="辅导员", department="软件工程学院", job_time="2000-1-01")
        Graduate.__init__(self, research_fields="人工智能", teacher="wjm")

    def display(self):
        print("姓名:" + self.name + "\n性别:" + self.gender + "\n年龄:" + str(
            self.age) + "\n职务:" + self.post + "\n部门:" + self.department + "\n工作时间:" + self.job_time + "\n学号:" + self.student_id + "\n入学时间:" + self.into_time + "\n入学成绩:" + str(
            self.into_score) + "\n研究方向:" + self.research_fields + "\n导师:" + self.teacher)
p = People() # 人员类
p.display()
s = Student() # 学生类
s.display()
t = Teacher() # 教师类
t.display()
g = Graduate() # 研究生类
g.display()
gw = GradOnWork() # 在职研究生类
gw.display()
View Code

 

设计三维图形类体系,要求如下:

设计三维图形功能接口,接口包含周长、面积、体积计算方法;

基于以上接口,首先定义点类,包含xy坐标数据成员,坐标获取及设置方法、显示方法等

以点基类派生圆类,增加表示半径的数据成员,半径获取及设置方法,重载显示函数,并可计算周长和面积等

以圆类为基础派生球类、圆柱类、圆锥类;要求派生类球、圆柱、圆锥中都含有输入和输出显示方法;并可计算面积、周长。

程序中定义各种类的对象,并完成测试。

import math
class Shape:
    def cal_perimeter(self):
        pass
    def cal_area(self):
        pass
    def cal_volume(self):
        pass
class Point(Shape):
    def __init__(self,x=0,y=0):
        self.x = x
        self.y = y
    def setX(self,x):
        self.x = x
    def setY(self,y):
        self.y = y
    def getX(self):
        return self.x
    def getY(self):
        return self.y
    def display(self):
        print('({}'.format(self.x)+',{})'.format(self.y))
class Circle(Point):
    def __init__(self,r):
        Point.__init__(self,x=0,y=0)
        self.r = r
    def setR(self,r):
        self.r = r
    def getR(self):
        return self.r
    def cal_perimeter(self):
        perimeter=round(2*3.14*self.r,2)
        return perimeter
    def cal_area(self):
        area=round(3.14*self.r**2,2)
        return area
    def display(self):
        print('圆的周长:',self.cal_perimeter())
        print('圆的面积:',self.cal_area())
class Ball(Circle):
    def __init__(self,r):
        Circle.__init__(self,r=10)
    def cal_volume(self):
        perimeter=round((4/3)*3.14*(self.r**3),2)
        return perimeter
    def cal_area(self):
        area=round(3.14*self.r**2*4,2)
        return area
    def display(self):
        print('球的体积:',self.cal_volume())
        print('球的面积:',self.cal_area())
class Circular(Circle):
    def __init__(self,r,h):
        Circle.__init__(self,r=10)
        self.h = h
    def cal_volume(self):
        perimeter=round(3.14*(self.r**2)*self.h,2)
        return perimeter
    def cal_area(self):
        area=round(3.14*2*self.r*self.h+2*3.14*self.r**2,2)
        return area
    def display(self):
        print('圆柱的体积:',self.cal_volume())
        print('圆柱的面积:',self.cal_area())
class CircularCone(Circle):
    def __init__(self,r,h):
        Circle.__init__(self,r=10)
        self.h = h
    def cal_volume(self):
        perimeter=round((1/3)*3.14*(self.r**2)*self.h,2)
        return perimeter
    def cal_area(self):
        area=round(3.14*self.r*(self.r+math.sqrt(self.r**2+self.h**2)),2)
        return area
    def display(self):
        print('圆锥的体积:',self.cal_volume())
        print('圆锥的面积:',self.cal_area())


p = Point(6,2)
p.display()

c = Circle(4)
c.display()

r = float(input("请输入球体半径:"))
b = Ball(r)
b.display()

r = float(input("请输入圆柱底面半径:"))
h = float(input("请输入圆柱的高:"))
cc = Circular(r,h)
cc.display()

r = float(input("请输入圆锥底面半径:"))
h = float(input("请输入圆锥的高:"))
cn = CircularCone(r,h)
cn.display()
View Code

设计教师接口,该接口包含教师工资计算方法。应用(一)中的高校人员信息包,设计不同职称的教师类:教授,副教授,讲师,教师的基本信息包括姓名、性别、出生年月、职称、课时工作量等属性。注意学校对教师每月工资的计算规定如下:固定工资+课时补贴;教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。

程序中定义各种教师类的对象,并编写程序求这些教师的月工资。

class Teacher:
    def __init__(self, name, gender, birth, zhicheng, work):
        self.name = name
        self.gender = gender
        self.birth = birth
        self.zhicheng = zhicheng
        self.work = work

    def income(self):
        if self.zhicheng == "教授":
            return self.work * 50 + 5000
        elif self.zhicheng == "副教授":
            return self.work * 30 + 3000
        elif self.zhicheng == "讲师":
            return self.work * 20 + 2000

    def display(self):
        print(self.name + self.zhicheng + "的工资:" + str(Teacher.income(self)))


class Professor(Teacher):
    def __init__(self):
        Teacher.__init__(self, name='QAZ', gender='', birth="1969-1-1", zhicheng="教授", work=3)


class Professor_Fu(Teacher):
    def __init__(self):
        Teacher.__init__(self, name='WSX', gender='', birth="1980-02-02", zhicheng="副教授", work=4)


class Instructor(Teacher):
    def __init__(self):
        Teacher.__init__(self, name='EDC', gender='', birth="1993-03-03", zhicheng="讲师", work=7)


p = Professor()
p.display()
pf = Professor_Fu()
pf.display()
i = Instructor()
i.display()
View Code

定义一个异常类Cexception解决日期类实现中的自定义异常处理。设计的日期类应包含以下内容:

①  有三个成员数据:年、月、日;

②  有设置日期的成员函数;

③  有用格式"月/日/年"输出日期的成员函数;

④  要求在日期设置及有参构造函数中添加异常处理。

class Cexception :
    def __init__(self,year,month,day):
        if Cexception.judge(year,month,day) :
            self.year = year
            self.month = month
            self.day = day
        else:
            self.year = -1
            self.month = -1
            self.day = -1
            print("日期有误")
    def setTime(self,year,month,day):
        if Cexception.judge(year,month,day) :
            self.year = year
            self.month = month
            self.day = day
        else:
            self.year = -1
            self.month = -1
            self.day = -1
            print("日期有误")
    def display(self):
        if self.month ==-1 or  self.year == -1 or self.day == -1:
            print("",end="")
        else:
            print(str(self.month)+"/"+str(self.day)+"/"+str(self.year))
    def judge(year,month,day):
        leap = False
        legal = False
        if year>0:
            if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
                leap = True
            if month in [1, 3, 5, 7, 8, 10, 12]:
                if 1 <= day <= 31:
                    legal = True
            elif month in [4, 6, 9, 11]:
                if 1 <= day <= 30:
                    legal = True
            elif month == 2:
                if not leap and 1 <= day <= 28:
                    legal = True
                elif leap and 1 <= day <= 29:
                    legal = True
        else:
            legal = False
        return legal

c = Cexception(-1,8,5)
c.display()
c = Cexception(2018,8,2)
c.display()
c = Cexception(2019,5,29)
c.display()
c = Cexception(2020,5,8)
c.display()
View Code

定义一个平面点类Point,对其重载运算符关系运算符,关系运算以距离坐标原点的远近作为基准,远的为大。程序完成对其的测试。

 

import math


class Point():
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __lt__(self, other):
        l1 = math.sqrt(self.x ** 2 + self.y ** 2)
        l2 = math.sqrt(other.x ** 2 + other.y ** 2)
        return l1 > l2

    def __le__(self, other):
        l1 = math.sqrt(self.x ** 2 + self.y ** 2)
        l2 = math.sqrt(other.x ** 2 + other.y ** 2)
        return l1 == l2

    def __gt__(self, other):
        l1 = math.sqrt(self.x ** 2 + self.y ** 2)
        l2 = math.sqrt(other.x ** 2 + other.y ** 2)
        return l1 < l2

    def __ge__(self, other):
        l1 = math.sqrt(self.x ** 2 + self.y ** 2)
        l2 = math.sqrt(other.x ** 2 + other.y ** 2)
        return l1 >= l2

    def __eq__(self, other):
        l1 = math.sqrt(self.x ** 2 + self.y ** 2)
        l2 = math.sqrt(other.x ** 2 + other.y ** 2)
        return l1 <= l2

    def __ne__(self, other):
        l1 = math.sqrt(self.x ** 2 + self.y ** 2)
        l2 = math.sqrt(other.x ** 2 + other.y ** 2)
        return l1 != l2


p1 = Point(5, 4)
p2 = Point(6, 7)
p = p1 > p2
print(p)
p = p1 == p2
print(p)
p = p1 < p2
print(p)
p = p1 >= p2
print(p)
p = p1 <= p2
print(p)
p = p1 != p2
print(p)
View Code

 

posted on 2023-04-23 14:40  夜的第七章i  阅读(211)  评论(0编辑  收藏  举报