一、实验目的和要求
(一) 目的
面向对象程序设计的方法是从日常处理问题思路中形成的一种新的设计方法。通过了解对象、类、封装、继承、方法、构造函数和析构函数等面向对象的程序设计的基本概念,掌握Python类的定义、类的方法、类的继承等,为解决复杂现实问题,培养编程能力及工程应用技能,打下坚实的基础。
(二) 要求
1 了解面向对象的程序设计思想
2掌握Python对象、类、封装、继承、方法、构造函数和析构函数等.
3掌握Python的类的继承与组合
二、实验内容及步骤
- 设计一个Circle类来表示圆,这个类包含圆的半径以及求周长和面积的函数。再使用这个类创建半径为1~100的圆,并计算出相应的周长及面积。
- 编写程序并测试,有一个汽车类Vehicle,类中的私有数据成员为车轮个数wheels和车重weight;小车类Car是汽车类vehicle派生类,其中包含载客人数passenger_load,默认4人;卡车类Truck是汽车类vehicle派生类,其中包含载客人数passenger_load和载重量payload。
提示:编写类时要使用get和set方法,Vehicle类定义display()显示车轮和重量,Car和Truck中要对display()进行同名覆盖。
3.编写一个学生和教师数据输入和输出的程序。学生数据包括编号、姓名、班号和成绩;教师的数据含有编号、姓名、职称和部门。要求设计一个person类,作为学生数据操作类student和教师数据操作类teacher的基类。
三、实验代码清单
1、class Circle:
def __init__(self,r):
self.r=r
def getArea(self):
return 3.1415926*self.r**2
def getPerimeter(self):
return 3.1415926*self.r*2
c=Circle(5)
print("%.2f" %c.getArea())
print("%.2f" %c.getPerimeter())
2、class Vehicle(object):
def __init__(self,wheels,weight):
self.setWheels(wheels)
self.setWeight(weight)
def setWheels(self,wheels):
if type(wheels) !=int:
raise Exception('wheels must be int')
self.__wheels=wheels
def setWeight(self,weight):
if type(weight)!=int:
raise Exception('weight must be int')
self.__weight=weight
def getWeight(self):
return self.__weight
def getWheels(self):
return self.__wheels
def show(self):
print(self.getWheels(),self.getWeight())
class Car(Vehicle):
def __init__(self,wheels,weight,passenger_load=4):
super(Car,self).__init__(wheels,weight)
Vehicle.__init__(self,wheels,weight)
self.setPassenger(passenger_load)
def setPassenger(self,passenger_load):
if type(passenger_load) !=int:
raise Exception('passenger_load must be int')
self.__passenger_load=passenger_load
def getPassenger(self):
return self.__passenger_load
def show(self):
super(Car,self).show()
print(self.getPassenger())
class Truck(Vehicle):
def __init__(self,wheels,weight,passenger_load,payload):
super(Truck,self).__init__(wheels,weight)
Vehicle.__init__(self,wheels,weight)
self.setPassenger(passenger_load)
self.setPayload(payload)
def setPassenger(self,passenger_load):
if type(passenger_load)!=int:
raise Exception('passenger_load must be int')
self.__passenger_load=passenger_load
def setPayload(self,payload):
if type(payload) !=int:
raise Exception('payload must be int')
self.__payload=payload
def getPayload(self):
return self.__payload
def getPassenger(self):
return self.__passenger_load
def show(self):
super(Truck,self).show()
print(self.getPassenger(),self.getPayload())
if __name__=='__main__':
vehicle=Vehicle(8,100)
vehicle.show()
car=Car(8,100)
car.show()
truck=Truck(8,100,8,100)
truck.show()
3、class Person(object):
def __init__(self,number,name):
self.setNumber(number)
self.setName(name)
def setNumber(self,number):
if type(number) !=int:
raise Exception('number must be int')
self.__number=number
def setName(self,name):
if not isinstance(name,str):
raise Exception('name must be string')
self.__name=name
def getName(self):
return self.__name
def getNumber(self):
return self.__number
def show(self):
print(self.getNumber(),self.getName())
class Student(Person):
def __init__(self,number,name,classNumber,grade):
super(Student,self).__init__(number,name)
Person.__init__(self,number,name)
self.setClassNumber(classNumber)
self.setGrade(grade)
def setClassNumber(self,classNumber):
if type(classNumber) != int:
raise Exception('classNumber must be int')
self.__classNumber=classNumber
def setGrade(self,grade):
if type(grade) !=int and grade<=100 and grade >=0:
raise Exception('grade must be int and 0<=grade<=100')
self.__grade=grade
def getClassNumber(self):
return self.__classNumber
def getGrade(self):
return self.__grade
def show(self):
super(Student,self).show()
print(self.getClassNumber(),self.getGrade())
class Teacher(Person):
def __init__(self,number,name,title,department):
super(Teacher,self).__init__(number,name)
Person.__init__(self,number,name)
self.setTitle(title)
self.setDepartment(department)
def setDepartment(self,department):
if type(department) !=str:
raise Exception('department must be a string')
self.__department=department
def setTitle(self,title):
if type(title) !=str:
raise Exception('department must be a string')
self.__title=title
def getTitle(self):
return self.__title
def getDepartment(self):
return self.__department
def show(self):
super(Teacher,self).show()
print(self.getTitle(),self.getDepartment())
if __name__=='__main__':
zhangsan=Person(201600555,'zhang san')
zhangsan.show()
studentLi=Student(2016005716,'Li Si',8,100)
studentLi.show()
teacherHuang=Teacher(2016005542,'Huang huihong','nothing','clearer')
teacherHuang.show()
四、实验运行结果
1
2
3
本文来自博客园,作者:一路向北~~,转载请注明原文链接:https://www.cnblogs.com/ylxb2539989915/p/16338557.html