Files
SwiftStudy/14 初始化.playground/Contents.swift
2022-01-13 10:53:35 +08:00

105 lines
2.0 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIKit
// init()
//class Size {
// var width: Int = 0
// var height: Int = 0
//}
//
//class Size {
// var width: Int
// var height: Int
//
// init() {
// width = 0
// height = 0
// }
//}
class Size {
var width: Int = 0
var height: Int = 0
//
// init()
init(width: Int, height: Int) {
self.width = width
self.height = height
}
// 便
convenience init(width: Int) {
self.init(width: width, height: 0)
}
}
var s1 = Size(width: 10, height: 20)
var s2 = Size(width: 30)
class Person {
var age: Int
//
init(age: Int) {
self.age = age
}
// 便
convenience init() {
self.init(age: 0)
}
}
class Student: Person {
var score: Int
//
init(age: Int, score: Int) {
self.score = score
// super.init
super.init(age: age)
}
// 便便
convenience init() {
self.init(score: 0)
}
// 便
convenience init(score: Int) {
self.init(age: 0, score: score)
}
}
class Animal {
var legs: Int
var name: String
var age: Int
init(name: String, age: Int, legs: Int) {
self.legs = legs
self.age = age
self.name = name
}
init(name: String, age: Int) {
self.legs = 4
self.age = age
self.name = name
}
convenience init(name: String) {
self.init(name: name, age: 1, legs: 4)
}
}
class Cat: Animal {
}
var cat = Cat(name: "淘气", age: 3, legs: 4)
var cat1 = Cat(name: "淘气", age: 3)
var cat2 = Cat(name: "淘气")