class Test {
//給屬性賦默認值
var a = 0
//給屬性類型,默認值在構造器中賦值
var b: Int
//當前是缺省值,
var c: Int?//當前是缺省值,之後可能仍是缺省值
var d: Int!//當前是缺省值,之後必定有值
init(b: Int) {
self.b = b
}
}
複製代碼
struct Color {
var red: Double, green: Double, blue: Double
init(red: Double, green: Double, blue: Double) {
self.red = red;
self.green = green
self.blue = blue
}
init(white: Double) {
self.red = white
self.green = white
self.blue = white
}
}
var aColor = Color(red: 1.0, green: 0, blue: 0)
var bColor = Color(white: 0.5)
print("aColor: \(aColor)")
print("bColor: \(bColor)")
複製代碼
struct Test {
var a = 3
var b = 5
}
var a = Test(a: 3, b: 6)
var b = Test(a: 5)
print(a)
print(b)
複製代碼
當咱們在類或者結構體中寫了自定義的構造器後,默認的構造器將消失。swift
類的構造器構造過程
構造過程分爲兩個階段bash
階段一:閉包
階段二:app
class Test {
var a: Int
init(a: Int) {
print("Test-init-start")
self.a = a
print("Test-init-end")
}
}
class Test1: Test {
var b: Int
init(b: Int, a: Int) {
print("Test1-init-start")
self.b = b
super.init(a: a)
print("Test1-init-end")
}
}
class Test2: Test1 {
var c: Int
init(c:Int, b: Int, a: Int) {
print("Test2-init-start")
self.c = c
super.init(b: b, a: a)
print("Test2-init-end")
}
convenience init(){
//階段一與階段二:利用指定構造器完成整個類的屬性初始化
print("階段一:調用指定構造器沿着繼承鏈向上完成屬性的初始化 階段二:在從鏈頂向下完成自定義部分")
print("convenience init")
self.init(c: 0, b: 0, a: 0)
//最後到達遍歷構造器,這時候能夠徹底訪問屬性了
let d = (a + 1) * (b + 2) * (c + 3)
print(d)
}
}
//var test = Test2(c: 1, b: 2, a: 3)
var test1 = Test2()
//打印以下:
//階段一:調用指定構造器沿着繼承鏈向上完成屬性的初始化 階段二:在從鏈頂向下完成自定義部分
//convenience init
//Test2-init-start
//Test1-init-start
//Test-init-start
//Test-init-end
//Test1-init-end
//Test2-init-end
//6
複製代碼
結構體中的構造器ide
構造器的繼承和重寫ui
構造器便利構造器的使用spa
class Food {
var name: String
init(name:String) {
self.name = name
}
convenience init(){
self.init(name: "Unnamed")
}
}
class Material: Food {
var count: Int
init(count: Int, name: String) {
self.count = count
super.init(name: name)
}
convenience override init(name: String) {
self.init(count:1, name:name)
}
}
class ShoppingListItem: Material {
var purchased = false
func description() {
let purchas = purchased ? " ✔" : " ✘"
print("\(count) X \(name) \(purchas)")
}
}
let listItem = [ShoppingListItem(), ShoppingListItem(name: "麪包"), ShoppingListItem(count: 5, name: "巧克力")]
listItem[1].purchased = true
listItem[0].description()
listItem[1].description()
listItem[2].description()
複製代碼
struct Animal {
let species: String
init?(species: String){
if species.isEmpty {
return nil
}
self.species = species
}
}
var a = Animal(species: "")
if a == nil{
print("構造失敗")
}else{
print("構形成功:" + a!.species)
}
//打印以下
//構造失敗
複製代碼
enum Direction: Character {
case D = "D", X = "X", N = "N", B = "B"
init?(direction: Character){
switch direction {
case "D":
self = .D
case "X":
self = .X
case "N":
self = .N
case "B":
self = .B
default:
return nil
}
}
}
var a = Direction(direction: "D")
print(a!.rawValue)
if let a = Direction(direction: "T") {
print("可選枚舉構形成功:\(a.rawValue)")
}else{
print("可選枚舉構造失敗")
}
複製代碼
enum Direction: Character {
case D = "D", X = "X", N = "N", B = "B"
}
if let a = Direction(rawValue: "T") {
print("可選枚舉構形成功:\(a.rawValue)")
}else{
print("可選枚舉構造失敗")
}
複製代碼
class Product {
var name: String
init?(name: String) {
if name.isEmpty {
return nil
}
self.name = name
}
}
class CartItem: Product {
var count: Int
init?(name: String, count: Int) {
if count < 1 {
return nil
}
self.count = count
super.init(name: name)
}
}
if let a = CartItem(name: "2", count: 10){
print("構形成功: \(a.name) \(a.count)")
}else{
print("構造失敗")
}
複製代碼
class Product {
var name: String?
init() {}
init?(name: String) {
if name.isEmpty {
return nil
}
self.name = name
}
}
class CartItem: Product {
override init() {
super.init()
self.name = "Unnamed"
}
override init(name: String) {
super.init()
if self.name == nil {
self.name = "Unnamed"
}
}
}
var a = CartItem(name: "")
print(a.name!)
複製代碼
class Product {
var name: String
required init(name: String) {
self.name = name
}
}
class CartItem: Product {
var sex: Int = 1
//當該類繼承了父類全部構造器的時候,就沒必要重寫父類的必要構造器
//當該類沒有繼承父類的全部構造器的時候,就要重寫父類的必要構造器
init(sex: Int, name: String) {
self.sex = sex
super.init(name: name)
}
required init(name: String) {
self.sex = 0
super.init(name: name)
}
}
var a = CartItem(name: "s")
print(a.name)
複製代碼
class Squire {
//經過閉包馬上給屬性初始化
let colorSquire: [Bool] = {
var tempArr = [Bool]()
var isBlack = false
for _ in 1...8{
for _ in 1...8{
tempArr.append(isBlack)
isBlack = !isBlack
}
isBlack = !isBlack
}
return tempArr
}()
//計算屬性
var squireCount: Int {
return colorSquire.count
}
//下標
subscript(row: Int, col: Int) -> Bool{
return colorSquire[row * 8 + col]
}
func squireColor(row: Int, col: Int) -> Bool {
return colorSquire[row * 8 + col]
}
}
var a = Squire()
print(a.squireColor(row: 2, col: 2))
print(a[2,2])
複製代碼
計算屬性,下標,閉包初始化屬性比較代理
計算屬性,下標,閉包初始化屬性使用場景code
總結對象