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