1.设计一个Circle类来表示园,这个类包含圆的半径以及求周长和面积的函数。再使用这个类创建半径为1~100的圆,并计算出相应的周长及面积。
class circle:
def __init__(self,radius):
self.radius=radius
def length(self):
return self.radius*2*3.14
def area(self):
return self.radius**2*3.14
a=int(input("输入半径:"))
c=circle(a)
print("周长为:",c.length(),"面积为:",c.area())
2.编写程序并测试,有一个汽车类Vehicle,类中的私有数据成员为车轮个数wheels和车重weight;小车类Car是汽车类vehicle派生类,其中包含载客人数passenger_load,默认4人;卡车类Truck是汽车类vehicle派生类,其中包含载客人数passenger_load和载重量payload。 提示:编写类时要使用get和set方法,Vehicle类定义display()显示车轮和重量,Car和Truck中要对display()进行同名覆盖。
class Vehicle:
def __init__(self,wheels,weight):
self.setWheels(wheels)
self.setWeight(weight)
def setWheels(self,wheels):
self.__wheels = wheels
def setWeight(self,weight):
self.__weight = weight
def getWheels(self):
return self.__wheels
def getWeight(self):
return self.__weight
def display(self):
print("车轮={0},重量={1}".format(self.getWheels(),self.getWeight()))
class Car(Vehicle):
def __init__(self,wheels,weight,passenger_load = 4):
super(Car,self).__init__(wheels,weight)
self.setPassenger(passenger_load)
def setPassenger(self, passenger_load,):
self.__passenger_load = passenger_load
def getPassenger(self):
return self.__passenger_load
def display(self):
print("车轮={0},重量={1},载客人数={2}".format(self.getWheels(),self.getWeight(),self.getPassenger()))
class Truck(Vehicle):
def __init__(self,wheels,weight,passenger_load,payload):
super(Truck, self).__init__(wheels,weight)
self.setPassenger(passenger_load)
self.setPayload(payload)
def setPassenger(self,passenger_load):
self.__passenger_load = passenger_load
def setPayload(self,payload):
self.__payload = payload
def getPassenger(self):
return self.__passenger_load
def getPayload(self):
return self.__payload
def display(self):
print("车轮={0},重量={1},载客数={2},载重量={3}".format(self.getWheels(),self.getWeight(),self.getPassenger(),self.getPayload()))
vehicle = Vehicle(4,10000)
vehicle.display()
car = Car(4,5000,4)
car.display()
truck = Truck(8,20000,12,12000)
truck.display()
3.编写一个学生和教师数据输入和输出的程序。学生数据包括编号、姓名、班号、和成绩;教师的数据含有编号、姓名、职称和部门。要求设计一个person类,作为学生数据操作类student和教师数据操作类teacher的基类。
class Person:
def __init__(self,number,name):
self.setNumber(number)
self.setName(name)
def setNumber(self,number):
self.__number = number
def setName(self,name):
self.__name = name
def getNumber(self):
return self.__number
def getName(self):
return self.__name
def show(self):
print("编号:{0},姓名:{1}".format(self.getNumber(),self.getName()))
class Student(Person):
def __init__(self,number,name,ban,fen):
super(Student,self).__init__(number,name)
self.setBan(ban)
self.setFen(fen)
def setBan(self,ban):
self.__ban = ban
def setFen(self,fen):
self.__fen = fen
def getBan(self):
return self.__ban
def getFen(self):
return self.__fen
def show(self):
print("编号:{0},姓名:{1},班级:{2},成绩:{3}".format(self.getNumber(),self.getName(),self.getBan(),self.getFen()))
class Teacher(Person):
def __init__(self,numer,name,cheng,bu):
super(Teacher,self).__init__(numer,name)
self.setCheng(cheng)
self.setBu(bu)
def setCheng(self,cheng):
self.__cheng = cheng
def setBu(self,bu):
self.__bu = bu
def getCheng(self):
return self.__cheng
def getBu(self):
return self.__bu
def show(self):
print("编号:{0},姓名:{1},职称:{2},部门:{3}".format(self.getNumber(),self.getName(),self.getCheng(),self.getBu()))
person = Person(0,'好人')
person.show()
student = Student(20,'张三',6666,100)
student.show()
teacher = Teacher(38,'好运来','教授','数学学院')
teacher.show()
|