综合之前的类的学习,做以下实例练习:(建议先不要看代码,自己先试着写;代码仅供参考,有多种实现方法)

1. Triangle & Equilateral

1). 创建class Triangle 表示三角形,包含三个属性值:angle1、angle2、angle3;

类方法 check_angles():若三个角相加 == 180,return True;若不是,return False

1 classTriangle(object):2 def __init__(self,angle1,angle2,angle3):3 self.angle1 =angle14 self.angle2 =angle25 self.angle3 =angle36

7 defcheckAngles(self):8 if (self.angle1+self.angle2+self.angle3) == 180:9 returnTrue10 else:11 returnFalse12

13 t1 = Triangle(40,50,90)14 print(t1.angle1,t1.angle2,t1.angle3)15 print(t1.checkAngles())16 t2 = Triangle(40,50,91)17 print(t2.checkAngles())

Triangle

2). 创建class Equilateral 继承上例1的Triangle,表示等边三角形,同Triangle不同点在于,其属性值的三个角均为60;而对应的 check_angles() 自然始终返回True

1 classEquilateral(Triangle):2 def __init__(self,angle1=60,angle2=60,angle3=60):3 self.angle1 =angle14 self.angle2 =angle25 self.angle3 =angle36

7 t3 =Equilateral()8 print(t3.angle1,t3.angle2,t3.angle3)9 print(t3.checkAngles())

Equilateral 1

如上示例代码可满足条件,但 更好的做法是调用父类构造函数,并重写check_angles() 使其始终返回 True,参见代码如下

1 classEquilateral(Triangle):2 def __init__(self,angle1=60,angle2=60,angle3=60):3 Triangle.__init__(self,angle1,angle2,angle3)4

5 defcheckAngles(self):6 returnTrue7

8 t3 =Equilateral()9 print(t3.angle1,t3.angle2,t3.angle3)10 print(t3.checkAngles())

Equilateral 2

2.  Car & ElectricCar

1).创建class Car 成员变量condition = "new",包含三个构造属性:model,color,mpg;

类方法 displayCar() print 拼接的字符串 This is a {color} {model} car with {mpg} MPG.  如 "This is a blue Xmodel car with 40 MPG."

类方法 driveCar() 改变成员变量condition = "used"

1 classCar(object):2 condition = "new"

3 def __init__(self,model,color,mpg):4 self.model =model5 self.color =color6 self.mpg =mpg7

8 defdisplayCar(self):9 print ("This is a {s.color} {s.model} car with {s.mpg} MPG.".format(s=self))10

11 defdriveCar(self):12 self.condition = "used"

13

14 car1 = Car("DeLorean", "silver", 88)15 car1.displayCar()16 print(Car.condition)17 print(car1.condition)18 car1.driveCar()19 print(car1.condition)

Car

2). 创建class ElectricCar 继承 Car,新增一属性变量 battery_type;重写driveCar()函数,改变 condition = "like new"

1 classElectricCar(Car):2 def __init__(self,model,color,mpg,battery_type):3 Car.__init__(self,model,color,mpg)4 self.battery_type =battery_type5

6 defdriveCar(self):7 self.condition = "like new"

8

9 car2 = ElectricCar("dd","Red",88,"molten salt")10 print(car2.battery_type,car2.condition)11 car2.displayCar() #继承Car方法

12 car2.driveCar() #调用重写后的方法

13 print(car2.condition)

ElectricCar

3. Point3D

创建class Point3D,表示三维坐标上的一个点,包含三个属性变量:x,y,z

类 __repr__ 方法显示为 (x,y,z)

类方法 distance() 返回改点距原点(0,0,0) 的距离

Python 类方法 __repr__  重写 print class_name 时的显示,参考代码即可理解

1 importmath2 classPoint3D(object):3 def __init__(self,x,y,z):4 self.x =x5 self.y =y6 self.z =z7 def __repr__(self):8 return ("({s.x},{s.y},{s.z})".format(s=self))9

10 defdistance(self):11 d = math.sqrt(self.x**2+self.y**2+self.z**2)12 returnd13

14 point1 = Point3D(3,4,0)15 print(point1)16 print(point1.distance())

Point3D

4. Employee & PartTimeEmployee

1).创建class Employee,包含成员变量hour_wage =20,构造函数包含属性变量:name;

类方法 calculateWage() 计算当天工钱,传参hours,return hours*hour_wage

1 classEmployee(object):2 hour_wage = 20

3 def __init__(self,name):4 self.name =name5

6 defcalculateWage(self,hours):7 return self.hour_wage*hours8

9 Peter = Employee("Peter")10 print(Peter.calculateWage(5))

Employee

2). 创建class PartTimeEmployee 继承 Employee,成员变量hour_wage=18, parttime_wage=15 构造函数同Employee

类方法calculateWage()重写,若是hour>=8,则 return hour_wage*hours;若是hour<8,return parttime_wage*hours

1 classPartTimeEmployee(Employee):2 hour_wage=18

3 parttime_wage=15

4

5 defcalculateWage(self,hours):6 if hours>=8:7 return self.hour_wage*hours8 else:9 return self.parttime_wage*hours10

11 May = PartTimeEmployee("May")12 print(May.calculateWage(5))13 print(May.calculateWage(8))

PartTimeEmployee

更多推荐

python定义一个triangle类_Python定义一个三角形类Python学习阶段综合练习2,python,triangle,二...