Python类的使用

Python类的使用

这篇主要是记录实验报告中关于Python面向对象程序设计部分,主要是掌握类的继承和组合

1. 设计一个Circle类来表示园,这个类包含圆的半径以及求周长和面积的函数。再使用这个类创建半径为1~100的圆,并计算出相应的周长及面积。
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())

实验结果如下
Python类的使用_第1张图片

2. 编写程序并测试,有一个汽车类Vehicle,类中的私有数据成员为车轮个数wheels和车重weight;小车类Car是汽车类vehicle派生类,其中包含载客人数passenger_load,默认4人;卡车类Truck是汽车类vehicle派生类,其中包含载客人数passenger_load和载重量payload。提示:编写类时要使用get和set方法,Vehicle类定义display()显示车轮和重量,Car和Truck中要对display()进行同名覆盖。
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()

结果如下
Python类的使用_第2张图片

3. 编写一个学生和教师数据输入和输出的程序。学生数据包括编号、姓名、班号、和成绩;教师的数据含有编号、姓名、职称和部门。要求设计一个person类,作为学生数据操作类student和教师数据操作类teacher的基类。
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()

结果如下
Python类的使用_第3张图片

写在最后

还是C++和Java的继承和封装用起来舒服

你可能感兴趣的:(Python)