Skip to content

Latest commit

 

History

History
52 lines (37 loc) · 2.5 KB

File metadata and controls

52 lines (37 loc) · 2.5 KB

Class

Наследование или подкласс класса

Класс, от которого вы наследуете, называется «родительским» или «суперклассом», а новый класс называется «дочерним» классом.

Наследование широко используется в Cocoa Touch, даже в самых простых программах.

class CountrySinger: Singer { } - Класс (Singer), от которого вы наследуете, называется «родительским» или «суперклассом», а новый класс (CountrySinger) называется «дочерним» или подклассом.

Final classes

  1. ✔️ final

Final class меняет метод диспатч на дайрект (оптимизация). Без final он будет думать, что мы можем наследовать и хранить в Table Dispatch.

This code prints the same output twice – true or false?

class Statue {
    var sculptor = "Unknown"
}
var venusDeMilo = Statue()
venusDeMilo.sculptor = "Alexandros of Antioch"
var david = Statue()
david.sculptor = "Michaelangelo"
print(venusDeMilo.sculptor) // Alexandros of Antioch
print(david.sculptor) // Michaelangelo

This creates two different statues, so it prints two different sculptors

class Starship {
	var maxWarp = 9.0
}
var voyager = Starship()
voyager.maxWarp = 9.975
var enterprise = voyager
enterprise.maxWarp = 9.6
print(voyager.maxWarp) // 9.6
print(enterprise.maxWarp) // 9.6

Final

  1. ✔️ Method Dispatch in Swift

final меняет метод диспатч на дайрект (оптимизация), без final он будет думать, что мы можем наследовать и хранить в table dispatch.

final enables direct dispatch on a method defined in a class. This keyword removes the possibility of any dynamic behavior. It can be used on any method, even in an extension where the dispatch would already be direct. This will also hide the method from the Objective-C runtime, and will not generate a selector.

!!!Прочитать про 3 метода Dispatch