Home 类和对象
Post
Cancel

类和对象

7-1 员工类

假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资(都是整数): (1)经理(Manager)——每月获得一份固定的工资 (2)销售人员(Salesman)——在基本工资的基础上每月还有销售提成 (3)一般工人(Worker)——则按他每月工作的天数计算工资 在Employee中提供函数getSalary()计算每个雇员一个月的工资,并在子类中重写。

在main函数中构造Employee的三个变量,分别指向Manager、Salesman、Worker的对象,调用getSalary函数,输出三个对象的工资

输入格式:

经理的月工资 销售人员的基本工资 销售人员的提成 工人的工作天数 工人每天的工资

输出格式:

经理的工资 销售人员的工资 工人的工资

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Employee:
#     def getSalary():
    def Manager(self,money):
        return money
    def Salesman(self,basep,tic):
        return eval(basep)+eval(tic)
    def Worker(self,wd,wm):
        return eval(wd)*eval(wm)

def main():
    ma = eval(input())
    sa1,sa2 = input().split(' ')
    wo1,wo2 = input().split(' ')
    
    a = Employee()
    print("%d"%a.Manager(ma))
    print("%d"%a.Salesman(sa1,sa2))
    print("%d"%a.Worker(wo1,wo2))

if __name__ == "__main__":
    main()

7-2 家电类

某大型家电企业拥有一批送货卡车,运送电视机、洗衣机、空调等家电。编程计算每个卡车所装载货物的总重量。要求有一个Appliance(家电)接口和有三个实现类TV、WashMachine和AirConditioner,这些类能够提供自重。有一个Truck类,包含了该货车上的所有家电,用一个集合(数组或集合类)表示。 Main函数中程序能够输出Truck类所装载货物的总重量。

输入格式:

家电数量 家电种类编号 家电重量

注意:各个家电的编号为:TV:1 WashMachine:2 AirConditioner:3

输出格式:

总重量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Appliance:
    T_all = []
    snum = ""
    weight = ""
    def tj(self,data):
        self.snum,self.weight = data.split(' ')
        self.T_all.append(eval(self.weight))
    def Truck(self):
        return sum(self.T_all)

def main():
    n = int(input())
    a = Appliance()
    for i in range(0,n):
        inp_data = input()
#         print(inp_data)
        a.tj(inp_data)
    print(a.Truck())

if __name__ == "__main__":
    main()

7-3 教师类

设计一个教师类Teacher,要求: 属性有编号(int no)、姓名(String name)、年龄(int age)、所属学院(String seminary),为这些属性设置相应的get和set方法。 为Teacher类重写equals方法,要求:当两个教师对象的no相同时返回true。 重写Teacher类的toString方法,通过该方法可以返回“no: , name:, age: **, seminary: **”形式的字符串。

输入格式:

两个教师对象的编号,姓名,年龄,学院

输出格式:

教师的信息 两个教师是否相等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Teacher:
    list1 = []
    no = ""
    name = ""
    age = ""
    seminary = ""
    def __toString(self,a,b,c,d):
        print("no: %s, name:%s, age: %s, seminary: %s"%(a,b,c,d))
        
    def add_data(self,data):
        self.list1.append(data)
        self.no,self.name,self.age,self.seminary = data.split(' ')
        self.__toString(self.no,self.name,self.age,self.seminary)

    def equals(self):
        if self.list1[0] == self.list1[1]:
            print("true")
        else:
            print("false")
def main():
    a = Teacher()
    for i in range(2):
        d = input()
        a.add_data(d)
    a.equals()
    
if __name__ == "__main__":
    main()

7-4 时间类

输入格式:

输入毫秒

输出格式:

按照小时、分钟、秒显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import time
class Time:
    def ctos(self,timec):
        temptime = time.localtime(timec/1000)
        # 13位时间戳不是10位有坑
        timenow = time.strftime("Hour: %H Minute: %M Second: %S", temptime)
        print(timenow)

def main():
    s = eval(input())
    a = Time()
    a.ctos(s)
if __name__ == "__main__":
    main()

7-5 定义抽象类Person、派生类Student和类Teacher

设计抽象类Person,派生出具体类:学生类Student和教师类Teacher,创建若干不同类对象后并在主方法中测试。 数据成员定义: Person [ID,姓名,生日] Student [专业,成绩] Teacher [职称,工资] 带参构造方法分别为: Person(int id,String name, int bir) Student(int id,String name, int bir, String major,double score) Teacher(int id,String name, int bir, String title, double salary) toString方法(Eclipse自动生成)

输入格式:

第一行整数n表示有n个对象,每个对象占2行,第一行为数字0(表示学生)或1(表示教师),第二行为生成对象的参数。

输出格式:

按行输出具体对象的信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Student:
    id = ""
    name = ""
    bir = ""
    major = ""
    score = ""
    def toString(self,data):
        self.id,self.name,self.bir,self.major,self.score = data.split(' ')
        print("Student [id=%s, name=%s, bir=%s, major=%s, score=%.1f]"%(self.id,self.name,self.bir,self.major,float(self.score)))

class Teacher:
    id = ""
    name = ""
    bir = ""
    title = ""
    salary = ""
    def toString(self,data):
        self.id,self.name,self.bir,self.title,self.salary = data.split(' ')
        print("Teacher [id=%s, name=%s, bir=%s, title=%s, salary=%.1f]"%(self.id,self.name,self.bir,self.title,float(self.salary)))


def main():
    a = Student()
    b = Teacher()
    n = eval(input())
    for i in range(0,n):
        x = input()
        if x == "0":
            dddd = input()
            a.toString(dddd)
        else:
            dddd = input()
            b.toString(dddd)

if __name__ == "__main__":
    main()
This post is licensed under CC BY 4.0 by the author.