class AoTeMan{
var name:String
init(name:String){
self.name = name
}
func action(otherName:String){
print("\(name)打\(otherName)")
}
}
class Monster {
var name:String
init(otherName:String){
self.name = otherName
}
}
let s1 = AoTeMan(name: "奧特曼")
let s2 = Monster(otherName: "小怪獸")
s1.action(s2.name)
複製程式碼
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s7 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s8 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s9 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s10 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}
class PC {
func pcNumber() -> Int{
return Int(arc4random_uniform(100)) + 1
}
}
let b = PC()
let c = b.pcNumber()
var isP = true
while isP{
print("請輸入1到100的隨機數:",terminator:"")
let a = inputInt()
if a == c{
print("恭喜你回答正確")
isP = false
}else if a > c{
print("你的數字大了")
}else {
print("你的數字小了")
}
}
複製程式碼
在做以上兩個的時候還沒有發現什麼問題,畢竟是單一物件比較簡單,但是在進行下段程式碼的時候就出現錯誤了。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s8 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s9 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s10 {font-variant-ligatures: no-common-ligatures; color: #000000}
let a = 3.14
class minOvral {
var radius:Double
init(radius:Double){
self.radius = radius
}
func area() -> Double{
return a * radius * radius
}
func showRadius() -> Double{
return (radius + 3) * (radius + 3) * a
}
}
class MaxOvral {
func area(radius:Double) -> Double{
return (radius + 3) * (radius + 3) * a
}
func cf(radius:Double) -> Double{
return 2 * a * (radius + 3)
}
}
let minO = minOvral(radius: 3.0)
let maxO = MaxOvral()
print("圓環價格為:\((maxO.area(minO.radius) - minO.area()) * 3.5)")
print("周圍柵欄價格:\(maxO.cf(minO.radius) * 1.5)")
複製程式碼
對於兩個物件我就覺得比較有難度了,當然現在解決了感覺比較簡單,我沒有建立物件就直接用類呼叫方法或者屬性,開始以為建立物件只是賦值而已,如果設計到不賦值就不用建立物件,直接呼叫方法或者屬性,在繼續的觀看了一下老師的程式碼發現物件也可以直接輸入到另外一個物件的方法中,這點很重要,在以後多個物件的時候應該會佔有很大的比重。記住一點物件呼叫方法和屬性。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Heiti SC Light'; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s3 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s8 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s9 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
//0.發現類
//-在對問題的描述中找到名詞和動詞
//-名詞會成為類或類中的屬性,動詞會成為類中的方法
//1.定義類
//-資料抽象(屬性)
//-行為抽象(方法)
//-初始化方法
//訪問修飾符
//-public
//-internal(內部的)-預設
//-private(私有)
class Circle {
// stored property
//-儲存屬性(儲存和圓相關的資料)
var radius:Double
init(radius:Double){
self.radius = radius
}
//通常獲得某個計算出的值的方法都可以設計成計算屬性
// computational property
//計算屬性(通過對儲存屬性做運算得到的屬性)
var perimeter:Double{
//圓的周長是一個只讀屬性
//所以此處只有get{}沒有set{}
get {return 2 * M_PI * radius}
}
var area: Double{
get {return radius * radius * M_PI}
}
}
複製程式碼
注意類中的方法在沒有引數的情況下,另一種情況 var area:Double{ get{return。。。。} }格式
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s7 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s8 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s9 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s10 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s11 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}
init(){
answer = Int(arc4random_uniform(100)) + 1
counter = 0
hint = ""
}
//行為抽象
//判斷
複製程式碼
不用非要在init()裡面寫引數 可以直接賦初值
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Heiti SC Light'; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s9 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
import Foundation
class Point {
var x:Double
var y:Double
//被其它初始化方法所呼叫的初始化方法
//指派初始化方法/指派構造器
init(x:Double,y:Double){
self.x = x
self.y = y
}
convenience init(){
self.init(x:0,y:0)
}
convenience init(point:(Double,Double)){
self.init(x:point.0,y:point.1)
}
func distanceTo(otherPoint:Point) -> Double{
let a = x - otherPoint.x
let b = y - otherPoint.y
return sqrt(a * a + b * b)
}
func moveTo(x:Double,y:Double){
self.x = x
self.y = y
}
}
複製程式碼
##這是今天其中第一個一個重要的知識點,叫便利構造器convenience 如上第一個convenience就是在打醬油的呼叫最開始的init初始化方法,第二個convenience就是一個元組Point給x,y賦初值 有點生疏保留幾位小數程式碼格式(如下程式碼)
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}
print(NSString(format: "%.2f", a.perimeter))
複製程式碼
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
var hp:Int{
get{
return _hp
}
set{
_hp = newValue > 0 ? newValue : 0
}
}
var mp:Int{
get{
return _mp
}
set{
_mp = newValue > 0 ? newValue : 0
}
}
init(name:String,hp:Int,mp:Int){
_name = name
_hp = hp
_mp = mp
}
複製程式碼
##第二個重用的知識點,get set方法 前者只能讀取資料,後者可以改寫,set用起來很方便,比如這裡的血量和藍量不能低於0就用一個三元運算來解決,漂亮優雅,高內聚,低耦合,
以下程式碼是產生隨機變數的標準程式碼模式
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
func randomInt(min:UInt32,max:UInt32) -> Int{
return Int(arc4random_uniform(max - min + 1) + min)
}
複製程式碼
由UIView提供的類Canvas直接呼叫就是,不用再新建類了,這裡就是建立物件c然後用c來傳送訊息
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
let c = Canvas(frame:self.view.bounds)
c.backgroundColor = UIColor.clearColor()
self.view.addSubview(c)
複製程式碼
##第三個重要的知識點擴充套件類(extension),如果在某個特定的應用中你發現現有的某個型別少了某項功能,就可以通過這個方法來來新增這個功能。以下程式碼主要是在Point類中新建cgPoint這個方法來增加CGFloat單浮點這個功能。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #bb2ca2}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
extension Point{
var cgPoint:CGPoint{
get {return CGPointMake(CGFloat(x), CGFloat(y))}
}
}
複製程式碼
這或許是swift一大亮點。用問號實現兩個不確定的值得轉換,很精練,如果為空就是取預設值(即藍色)如果不為空就去輸入的那個顏色
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}
var color:UIColor?
(color ?? UIColor.blueColor()).set()
複製程式碼
以下一大亮點就是弄了一個二維陣列來實現三角形的不消失,即每產生一個一維陣列pointArray然後用陣列的append方法從最後的位置塞入newArray陣列中,最後在用一個for迴圈來使二維陣列中的每個值來實現draw()
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s7 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s8 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s9 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}
class Canvas: UIView {
var pointArray:[Point] = []
var newArray:[Triangle] = []
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
let touchPoint = touch.locationInView(self)
pointArray.append(touchPoint.myPoint)
if pointArray.count == 3{
let t = Triangle(va: pointArray[0], vb: pointArray[1], vc: pointArray[2])
// t.color = UIColor.redColor()
t.color = UIColor.randomColor()
newArray.append(t)
setNeedsDisplay()//重新整理介面
pointArray.removeAll()
}
}
}
override func drawRect(rect: CGRect) {
for t in newArray{
t.draw()
}
}
}
複製程式碼
求最大公約數
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}
func gcb(x:Int,_ y:Int) -> Int{
var a = x < y ? x : y
while a > 1{
if x % a == 0 && y % a == 0{
return a
}
a -= 1
}
return 1
}
複製程式碼
以上程式碼雖然可以求出最大公約數 但是太low了 這裡介紹一個方法(短除法)就是較大的數除以較小的數,如果除得盡較小數就是最大公約數,如果除不盡就取其餘數,用較小數除以餘數,如果除得盡,餘數就是最大公約數,如果不,繼續除直到除得盡,除得盡的那個餘數為最大公約數,這裡用了遞迴,比迴圈的效率也高很多,當然逼格也高很多。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
func gcb(x:Int,_ y:Int) -> Int{
if x > y{
return gcb(y,x)
}
else if y % x != 0{
return gcb(y % x,x)
}
else{
return x
}
}
複製程式碼
運算子過載(為自定義的型別定義運算子)簡單的說就是個某個運算子號加個什麼功能,使使用者更加容易懂。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #31595d}
//運算子過載(為自定義的型別定義運算子)
func +(one:Fraction,two:Fraction) -> Fraction{
return one.add(two)
}
func -(one:Fraction,two:Fraction) -> Fraction{
return one.sub(two)
}
func *(one:Fraction,two:Fraction) -> Fraction{
return one.mul(two)
}
func /(one:Fraction,two:Fraction) -> Fraction{
return one.div(two)
}
複製程式碼
開火車式的給予方法就是在後面.方法就行,這裡我犯的一個錯就是返回值沒有弄成Fraction。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}
func add(otherfraction:Fraction) -> Fraction{
return Fraction(num: _num * otherfraction._den + _den * otherfraction._num, den: _den * otherfraction._den)
}
func sub(otherfraction:Fraction) -> Fraction{
return Fraction(num: _num * otherfraction._den - _den * otherfraction._num, den: _den * otherfraction._den)
}
func mul(otherfraction:Fraction) -> Fraction{
return Fraction(num: _num * otherfraction._num , den: _den * otherfraction._den)
}
func div(otherfraction:Fraction) -> Fraction{
return Fraction(num: _num * otherfraction._den , den: _den * otherfraction._num)
}
複製程式碼
這裡有個小知識點可以遮蔽名字字串中最後一個字
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
let displayName = _name.substringToIndex(_name.endIndex.predecessor())
return displayName + "*"
複製程式碼
在swift裡面的值不能給空。除非自帶?,這是swift一個特點。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
//發牌
func deal() -> Card?{
if hasMoreCards{
return cardsArray.removeFirst()
}
return nil
}
複製程式碼
這裡有個小結,以後遇到要你打亂陣列裡面值得位置就用這個方法,一個迴圈然後給出另外一個陣列裡面的值,用元組進行兩兩交換位置,另外一個值隨機變換,迴圈停止交換停止。這裡我開始想到的用陣列和字典來解決,就是用一個for迴圈把陣列裡面的值弄到字典裡面去,利用字典的無序性打亂陣列裡面的值得位置 ,然後在利用一個for迴圈把字典裡面的value賦給陣列。但是老師說字典並不是無序的,是另外一種有序,只是不是我們正常的那種有序而已。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s8 {font-variant-ligatures: no-common-ligatures; color: #703daa}
//洗牌
func shuffle(){
reset()
for i in 0..<cardsArray.count{
let j = randomInt(0,max: UInt32(cardsArray.count - 1))
(cardsArray[i],cardsArray[j]) = (cardsArray[j],cardsArray[i])
}
}
複製程式碼
這裡是列舉 還有呼叫列舉的方法也是比較特殊,注意觀看以下程式碼便是。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s5 {font: 14.0px 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}
enum Suite:String{
case Spade = "♠️"
case Heart = "♥️"
case Club = "♣️"
case Diamond = "♦️"
}
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #31595d}
let suites = [Suite.Spade,.Heart,.Club,.Diamond]
複製程式碼
可以將子型別的物件賦值給父型別的變數(因為子類跟父類之間是IS-A關係) 如果要將父型別的變數轉換成子型別需要用as運算子進行型別轉換 如果能夠確認父型別的變數中就是某種子型別的物件可以用as!進行轉換 如果不確定父型別的變數中是哪種子型別可以用as?嘗試轉換 繼承: 從已有的類建立新類的過程 提供繼承資訊的稱為父類(超類/基類) 得到繼承資訊的稱為子類(派生類/衍生類) 通常子類除了得到父類的繼承資訊還會增加一些自己特有的東西 所以子類的能力一定比父類更強大
繼承的意義在於子類可以複用父類的程式碼並且增強系統現有的功能 在做物件導向的問題上,一定要事先理清思路再下手,不然越做越混亂,父類的確定,下面的屬性,還有具體方法,一定要所有子類都滿足,也就是所有子類取並集,而子類酒會繼承父類的功能,甚至在繼承父類的功能上做些改變。 // 父類有的方法子類可以重新實現 這個過程叫方法重寫 // 需要在方法前新增override關鍵字 // 重寫有時也被稱為置換/覆蓋/覆寫 注意這裡是重寫,不要和過載搞混了。