综合之前的类的学习,做以下实例练习:(建议先不要看代码,自己先试着写;代码仅供参考,有多种实现方法)
1. Triangle & Equilateral
1). 创建class Triangle 表示三角形,包含三个属性值:angle1、angle2、angle3;
类方法 check_angles():若三个角相加 == 180,return True;若不是,return False
1 class Triangle(object): 2 def __init__(self,angle1,angle2,angle3): 3 self.angle1 = angle1 4 self.angle2 = angle2 5 self.angle3 = angle3 6 7 def checkAngles(self): 8 if (self.angle1+self.angle2+self.angle3) == 180 : 9 return True 10 else: 11 return False 12 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())
2). 创建class Equilateral 继承上例1的Triangle,表示等边三角形,同Triangle不同点在于,其属性值的三个角均为60;而对应的 check_angles() 自然始终返回True
1 class Equilateral(Triangle): 2 def __init__(self,angle1=60,angle2=60,angle3=60): 3 self.angle1 = angle1 4 self.angle2 = angle2 5 self.angle3 = angle3 6 7 t3 = Equilateral() 8 print(t3.angle1,t3.angle2,t3.angle3) 9 print(t3.checkAngles())
如上示例代码可满足条件,但 更好的做法 是调用父类构造函数,并重写check_angles() 使其始终返回 True,参见代码如下
1 class Equilateral(Triangle): 2 def __init__(self,angle1=60,angle2=60,angle3=60): 3 Triangle.__init__(self,angle1,angle2,angle3) 4 5 def checkAngles(self): 6 return True 7 8 t3 = Equilateral() 9 print(t3.angle1,t3.angle2,t3.angle3) 10 print(t3.checkAngles())
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 class Car(object): 2 condition = "new" 3 def __init__(self,model,color,mpg): 4 self.model = model 5 self.color = color 6 self.mpg = mpg 7 8 def displayCar(self): 9 print ("This is a {s.color} {s.model} car with {s.mpg} MPG.".format(s=self)) 10 11 def driveCar(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)
2). 创建class ElectricCar 继承 Car,新增一属性变量 battery_type;重写driveCar()函数,改变 condition = "like new"
1 class ElectricCar(Car): 2 def __init__(self,model,color,mpg,battery_type): 3 Car.__init__(self,model,color,mpg) 4 self.battery_type = battery_type 5 6 def driveCar(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)
3. Point3D
创建class Point3D,表示三维坐标上的一个点,包含三个属性变量:x,y,z
类 __repr__ 方法显示为 (x,y,z)
类方法 distance() 返回改点距原点(0,0,0) 的距离
Python 类方法 __repr__ 重写 print class_name 时的显示,参考代码即可理解
1 import math 2 class Point3D(object): 3 def __init__(self,x,y,z): 4 self.x = x 5 self.y = y 6 self.z = z 7 def __repr__(self): 8 return ("({s.x},{s.y},{s.z})".format(s=self)) 9 10 def distance(self): 11 d = math.sqrt(self.x**2+self.y**2+self.z**2) 12 return d 13 14 point1 = Point3D(3,4,0) 15 print(point1) 16 print(point1.distance())
4. Employee & PartTimeEmployee
1). 创建class Employee,包含成员变量hour_wage =20,构造函数包含属性变量:name;
类方法 calculateWage() 计算当天工钱,传参hours,return hours*hour_wage
1 class Employee(object): 2 hour_wage = 20 3 def __init__(self,name): 4 self.name = name 5 6 def calculateWage(self,hours): 7 return self.hour_wage*hours 8 9 Peter = Employee("Peter") 10 print(Peter.calculateWage(5))
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 class PartTimeEmployee(Employee): 2 hour_wage=18 3 parttime_wage=15 4 5 def calculateWage(self,hours): 6 if hours>=8: 7 return self.hour_wage*hours 8 else: 9 return self.parttime_wage*hours 10 11 May = PartTimeEmployee("May") 12 print(May.calculateWage(5)) 13 print(May.calculateWage(8))