Swift学習ノートクラスの継承リロードと構築関数
8063 ワード
class Vehicle {
var numberOfWheels: Int
var maxPassengers: Int
func description() -> String {
return String(numberOfWheels)
}
init(){
numberOfWheels = 0
maxPassengers = 1
}
}
let someVehicle = Vehicle()
class Bicycle: Vehicle {
override init() {
super.init()
numberOfWheels = 2
}
}
let bicycle = Bicycle()
bicycle.description()
class Car: Vehicle {
var speed: Double = 0.0
override init() {
super.init()
maxPassengers = 5
numberOfWheels = 4
}
override func description() -> String {
return super.description()
}
}
let car = Car()
class SpeedLimitedCar: Car {
override var speed: Double{
get {
return super.speed
}
set {
super.speed = min(newValue, 40.0)
}
}
}
let limitedCar = SpeedLimitedCar()
limitedCar.speed = 60.0
class AutomaticCar: Car {
var gear = 1
final var name:String = ""
override var speed: Double{
didSet{
gear = Int(speed / 10.0) + 1
}
}
override func description() -> String {
return super.description()
}
}
let automatic = AutomaticCar()
automatic.speed = 35.0
struct Color {
var red = 0.0, green = 0.0, blue = 0.0
init(red: Double, green: Double, blue: Double){
self.red = red
self.green = green
self.blue = blue
}
}
let magenta = Color(red: 1.0, green: 0.0, blue: 1.0)
class SurveyQuestion {
let text: String
var response: String?
init(text: String){
self.text = text
}
func ask() {
println(text)
}
}
let cheeseQuestion = SurveyQuestion(text: "DO you like cheese?")
cheeseQuestion.ask()
cheeseQuestion.response = "Yes, I do like cheese."
class ShoppingListItem {
var name: String?
var quantity = 1
var purchased = false
}
var item = ShoppingListItem()
struct Size {
var width = 0.0, height = 0.0
}
let twoByTwo = Size(width: 2.0, height: 2.0)
struct CheckerBoard {
let boardColors: [Bool] = {
var temporaryBoard = [Bool]()
var isBlack = false
for i in 1...10{
for j in 1...10{
temporaryBoard.append(isBlack)
isBlack = !isBlack
}
isBlack = !isBlack
}
return temporaryBoard
}()
}
let board = CheckerBoard()
class Timer {
var str = "strart"
deinit{
str = "close"
}
}
クラスのリロード、構造、構造関数.構造が複雑で、他の言語と変わらない.