ひよこメモ

備忘ブログ Ruby, Rails, AWS, html5, css3, javascript, vim,

XcodeのPlaygroundが便利

Xcodeを開いて
FIle > New > Playground

コンパイルせず即座に確認できる。
こちらのブログを参考にテスト
Swift実践入門 〜 今からはじめるiOSアプリ開発! 基本文法を押さえて、簡単な電卓を作ってみよう - エンジニアHub|若手Webエンジニアのキャリアを考える!

class MyApp {
    //親クラス
    class Shape {
        var name: String
        
        init(name: String) {
            self.name = name
        }
    }
    
    //子クラス 四角形
    class Rectangle: Shape {
        var width: Double
        var height: Double
    
        init(name: String, width: Double, height: Double) {
            self.width = width
            self.height = height
            super.init(name: name)
        }

        func area() -> Double {
            return width * height
        }
    }
    
    //子クラス 三角形
    class Triangle: Shape {
        var bottom: Double
        var height: Double
        
        init(name: String, bottom: Double, height: Double) {
            self.bottom = bottom
            self.height = height
            super.init(name: name)
        }
        
        func area() -> Double {
            return bottom * height / 2.0
        }
    }
}

var square = MyApp.Rectangle(name: "四角形!", width: 15, height: 20)
print(square.name)
print(square.area())