original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
used in the definition of a class Final keyword to declare classes, properties, methods, and subscripts. final declared classes cannot be inherited,andfinal declared properties, methods, and subscripts cannot be overridden.
Let's look at an example:
The final class person {//declaration is final, stating that it cannot be inherited var name: String final var age: Int //-Defined Age attribute final func description () -> string { //defining Description Instance methods return "\ (name) Age is: \" } final class func printclass () -> () { //define Printclass static methods print ( "person printing ...") } init (Name: string, age: int) { self.name = name Self.age = age }} class student: person { //compilation Error var school: string convenience init () { self.init (name: "Tony", age: 18,school: "Tsinghua University") init (name: string, age: int,school: String) { self.school = school super.init (name: name, age: age) } override func description () -> String{ //compilation error //attempting to override description instance method print ("Parent class print \ (Super.description ())") return "\ (name) Age is: \, School: \ (school). " } override class func Printclass () -> () { //compilation error //attempted to override Printclass static method print ( "student printing ...") } override var age: Int { //Compile error//attempt to override age property get { return super.age } set { super.age = newvalue < 8 ? 8: newvalue } }}
definition Student class, and declared as Person Child class, the following compilation error is reported:
Inheritance from a finalclass ' person '
defined by Age properties are also Final , then in an attempt to rewrite Age property, the following compilation error is reported:
var overrides a ' final ' var
definition Description instance method, which is declared as Final , then in an attempt to rewrite Description instance method, the following compilation error is reported:
Instance method Overridesa ' final ' Instance method
definition Printclass static method, which is declared as Final , then in an attempt to rewrite Printclass when a static method is used, the following compilation error is reported:
Class method overrides a ' final ' class method
Use Final can control our classes are limited inheritance, especially in the development of some commercial software, when appropriate to add Final restrictions are necessary.
Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
650) this.width=650; "title=" 00.png "src=" http://s4.51cto.com/wyfs02/M00/7C/D6/wKioL1bZE_LhW33RAAAs2MBEZnc513.png "alt=" Wkiol1bze_lhw33raaas2mbeznc513.png "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1747532
Learn Swift from scratch Learning Note (day)--final keywords