Practice#7–2 定義兩個類別, 用到繼承, 類別裡包含屬性和方法。 然後用類別生出物件 存取物件的屬性和呼叫物件的方法。

透過足球員的訓練菜單來練習這次的作業

  1. 先定義一般球員的練習菜單
class FootballTraining {var running = 5000var balltraining = 4func trainingNormal() {running = running * 2print(running, "KM")}func trainingDouble() {balltraining = balltraining * 2print(balltraining, "Hours")}}var normalPlayer = FootballTraining()var goodPlayer = FootballTraining()print("普通球員每天跑")normalPlayer.trainingNormal()print("好的球員每天還要踢球")goodPlayer.trainingDouble()

一般的球員每天要跑10000公里,而好的球員每天還要踢8小時的球……
那如果要成為超級巨星,又要怎麼練習呢?

超級巨星的養成,一定是在既有的基礎上,再比別人多努力付出,才會得到甜美的果實,所以這時候就可以用上繼承

class ExtraTraining: FootballTraining {var shootingPracitce = 100let name = "超級巨星"func excellentPlayer() {shootingPracitce = shootingPracitce * 50print(shootingPracitce, "Times")}}var superStar = ExtraTraining()print(superStar.name, "每天跑")superStar.trainingNormal()print(superStar.name, "每天也會練球")superStar.trainingDouble()print(superStar.name, "還要比別人多練習射門")superStar.excellentPlayer()

PS想定義真的要很清楚,不然寫出來的東西很亂七八糟
這次在想法上妥協了很多東西😢
程度上果然像紙一樣薄啊……

--

--

Kuanjen Chen
Kuanjen Chen

Written by Kuanjen Chen

A rookie of coding, learning how to create apps by using xcode & swift.