python开发案例教程-清华大学出版社(张基温)答案(4.3)

练习 4.1

1. 判断题

判断下列描述的对错。

1)子类是父类的子集。               

2)父类中非私密的方法能够被子类覆盖。           

3)子类能够覆盖父类的私密方法。     

4)子类能够覆盖父类的初始化方法。     

5)当创建一个类的实例时,该类的父类的初始化方法会被自动调用。     

6)所有的对象都是object类的实例。     

7)如果一个类没有显式地继承自某个父类,则就默认它继承自object类。     

2. 代码分析题

阅读下面的代码,给出输出结果。

1

class Parent(object):
    x = 1


class Child1(Parent):
    pass


class Child2(Parent):
    pass

print (Parent.x, Child1.x, Child2.x)
Child1.x = 2
print (Parent.x, Child1.x, Child2.x)
Parent.x = 3
print (Parent.x, Child1.x, Child2.x)

1 1 1
1 2 1
3 2 3

(2) 

class FooParent(object):  
    def __init__(self):  
        self.parent = 'I\'m the parent.'  
        print ('Parent')  
      
    def bar(self,message):  
        print( message,'from Parent')
        
class FooChild(FooParent):  
    def __init__(self):  
        super(FooChild,self).__init__()  
        print ('Child')  
          
    def bar(self,message):  
        super(FooChild, self).bar(message)  
        print ('Child bar fuction')  
        print (self.parent)

        
if __name__ == '__main__':
	fooChild = FooChild()
	fooChild.bar('HelloWorld')

Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.

 (3)

class A(object):
    def tell(self):
        print('A tell')
        self.say()

    def say(self):
        print('A say')
        self.__work()

    def __work(self):
        print('A work')


class B(A):
    def tell(self):
        print('\tB tell')
        self.say()
        super(B, self).say()
        A.say(self)

    def say(self):
        print('\tB say')
        self.__work()

    def __work(self):
        print('\tB work')
        self.__run()

    def __run(self):  # private
        print('\tB run')


b = B();b.tell()

    B tell
    B say
    B work
    B run
A say
A work
A say
A work

3. 程序设计题

1)编写一个类,由int类型派生,并且可以把任何对象转换为数字进行四则运算。

2)编写一个方法,当访问一个不存在的属性时,会提示“该属性不存在”,但不停止程序运行。

3)为学校人事部门设计一个简单的人事管理程序,满足如下管理要求。
① 学校人员分为三类:教师、学生、职员。
② 三类人员的共同属性是姓名、性别、年龄、部门。
③ 教师的特别属性是职称、主讲课程。
④ 学生的特别属性是专业、入学日期。
⑤ 职员的特别属性是部门、工资。
⑥ 程序可以统计学校总人数和各类人员的人数,并随着新人进入注册和离校注销而动态变化。

class Person:
    def __init__(self, name, gender, age, department):
        self.name = name
        self.gender = gender
        self.age = age
        self.department = department


class Teacher(Person):
    def __init__(self, name, gender, age, department=None, title=None, main_course=None):
        super().__init__(name, gender, age, department)
        self.title = title
        self.main_course = main_course


class Student(Person):
    def __init__(self, name, gender, age, department=None, major=None, enrollment_date=None):
        super().__init__(name, gender, age, department)
        self.major = major
        self.enrollment_date = enrollment_date


class Employee(Person):
    def __init__(self, name, gender, age, department=None, salary=None):
        super().__init__(name, gender, age, department)
        self.salary = salary

    # 人员列表,用于存储所有人员信息  


people = []


# 添加新人员函数  
def add_person(person_type, name, gender, age, department):
    if person_type == 'teacher':
        people.append(Teacher(name, gender, age, department))
    elif person_type == 'student':
        people.append(Student(name, gender, age, department))
    elif person_type == 'employee':
        people.append(Employee(name, gender, age, department))
    else:
        print("Invalid person type.")
        return
    print(f"{person_type} {name} added successfully.")
    print(f"Total {person_type}s: {len([p for p in people if isinstance(p, person_type)])}")
    print("Total people: ", len(people))


# 统计各类人员数量的函数  
def count_people():
    total_teachers = len([p for p in people if isinstance(p, Teacher)])
    total_students = len([p for p in people if isinstance(p, Student)])
    total_employees = len([p for p in people if isinstance(p, Employee)])
    print(f"Total teachers: {total_teachers}")
    print(f"Total students: {total_students}")
    print(f"Total employees: {total_employees}")
    print(f"Total people: {len(people)}")
    

4)为交管部门设计一个机动车辆管理程序,功能包括如下要求。
① 车辆类型(大客、大货、小客、小货、摩托)、生产日期、牌照号、办证日期。
② 车主姓名、年龄、性别、住址、身份证号。

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-07 00:44:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-07 00:44:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-07 00:44:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-07 00:44:04       18 阅读

热门阅读

  1. 力扣(leetcode)第383题赎金信(Python)

    2024-01-07 00:44:04       38 阅读
  2. 每日一题 - 240106 - D - Loong and Takahashi

    2024-01-07 00:44:04       79 阅读
  3. RocketMQ

    RocketMQ

    2024-01-07 00:44:04      32 阅读
  4. HTTP网络相关知识

    2024-01-07 00:44:04       34 阅读
  5. Kibana

    Kibana

    2024-01-07 00:44:04      33 阅读
  6. 详解Nacos和Eureka的区别

    2024-01-07 00:44:04       27 阅读
  7. 【LeetCode】1070. 产品销售分析 III

    2024-01-07 00:44:04       36 阅读
  8. Qt3D类使用说明

    2024-01-07 00:44:04       33 阅读
  9. ros python 接收GPS RTK 串口消息再转发 ros 主题消息

    2024-01-07 00:44:04       43 阅读
  10. Ubuntu中安装和配置SSH的完全指南

    2024-01-07 00:44:04       34 阅读