当前位置: 首页 > news >正文

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

练习 4.1

1. 判断题

判断下列描述的对错。

1)子类是父类的子集。               

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

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

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

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

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

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

2. 代码分析题

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

1

class Parent(object):x = 1class Child1(Parent):passclass Child2(Parent):passprint (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):  # privateprint('\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 = nameself.gender = genderself.age = ageself.department = departmentclass Teacher(Person):def __init__(self, name, gender, age, department=None, title=None, main_course=None):super().__init__(name, gender, age, department)self.title = titleself.main_course = main_courseclass Student(Person):def __init__(self, name, gender, age, department=None, major=None, enrollment_date=None):super().__init__(name, gender, age, department)self.major = majorself.enrollment_date = enrollment_dateclass 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.")returnprint(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)为交管部门设计一个机动车辆管理程序,功能包括如下要求。
① 车辆类型(大客、大货、小客、小货、摩托)、生产日期、牌照号、办证日期。
② 车主姓名、年龄、性别、住址、身份证号。

http://www.lryc.cn/news/274678.html

相关文章:

  • Qt 5.9.4 转 Qt 6.6.1 遇到的问题总结(一)
  • 探索生成式AI:自动化、问题解决与创新力
  • UI5与后端的文件交互(一)
  • [HCTF 2018]Warmup
  • 编译原理笔记(三)
  • DDoS攻击的多种方式
  • SpringValidation自定义注解以及分组校验
  • Multisim各版本安装指南
  • 大学生搜题软件,未来可期吗?
  • JMeter使用
  • ChatGPT 进行 SEO的使用技巧
  • PDF.js实现搜索多个不同的关键词高亮显示效果
  • ES高级用法:DeleteByQueryRequest
  • 使用docker build构建image
  • 【亲测有效】Win11 卸载MySQL5.7以及安装MySQL8.0.35
  • Beauty algorithm(三)腮红
  • DNS安全与访问控制
  • 【LMM 011】MiniGPT-5:通过 Generative Vokens 进行交错视觉语言生成的多模态大模型
  • WEB 3D技术 three.js 顶点交换
  • ROS学习笔记(11)进一步深入了解ROS第五步
  • 性能优化-OpenMP基础教程(四)-Android上运行OpenMP
  • 【转载】-财报-丈母娘教咱看财报(资产负债表-利润表-现金流量表)
  • HTML5大作业-精致版个人博客空间模板源码
  • 数字IC后端设计实现之Innovus update_names和changeInstName的各种应用场景
  • 1月6日,每日信息差
  • 部署上传漏洞的靶场环境upload-labs
  • Linux的压缩与解压
  • 互联网大厂面试题目
  • 单文件上传
  • 美经济学家预测,明年美股或将大跌86%,你怎么看?