这篇主要是记录实验报告中关于Python面向对象程序设计部分,主要是掌握类的继承和组合
class Circle:
def __init__(self,v):
self.__value=v
def getArea(self):
return 3.1415926*self.__value**2
def getPerimeter(self):
return 3.1415926*self.__value*2
c=Circle(5)
print("%.2f" %c.getArea())
print("%.2f" %c.getPerimeter())
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()
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()
还是C++和Java的继承和封装用起来舒服