標籤:
綜合之前的類的學習,做以下執行個體練習:(建議先不要看代碼,自己先試著寫;代碼僅供參考,有多種實現方法)
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 True10 else:11 return False12 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 class Equilateral(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 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())
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 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)
Car
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)
ElectricCar
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 d13 14 point1 = Point3D(3,4,0)15 print(point1)16 print(point1.distance())
Point3D
Python學習 —— 階段綜合練習二