Swift2 Development Guide version 2 Study Notes (Day 47)-final keywords

Source: Internet
Author: User

Swift2 Development Guide version 2 Study Notes (Day 47)-final keywords
Use the final keyword to declare the class, attribute, method, and subscript in the class definition. Classes declared by final cannot be inherited, and attributes, methods, and subscripts declared by final cannot be overwritten.

The following is an example:

Final class Person {// declared as final, indicating that it cannot be inherited var name: String final var age: Int // defines the age attribute final func description () -> String {// define description instance method return "\ (name) age: \ (age)"} final class func printClass ()-> () {// defines the static printClass method print ("Person print... ")} 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 // try to override the description instance method print ("parent print \ (super. description () ") return" \ (name): \ (age), school: \ (school ). "} Override class func printClass ()-> () {// compilation error // try to override the static printClass method print (" Student print... ")} override var age: Int {// compilation error // try to override the age attribute get {return super. age} set {super. age = newValue <8? 8: newValue }}}

 

 

When the Student class is defined and declared as the Person subclass, the following compilation error is reported:

Inheritance from a finalclass 'person'

If the defined age attribute is also final, the following compilation error will be reported when attempting to override the age attribute:

Var overrides a 'final' var

Define the description instance method, which is declared as final. when attempting to override the description instance method, the following compilation error will be reported:

Instance method overridesa 'final' instance method

Define the static method of printClass. If it is declared as final, the following compilation error will be reported when attempting to override the static method of printClass:

Class method overrides a 'final' class method

Using final can control the limited inheritance of our classes, especially when developing some commercial software, it is necessary to add final restrictions as appropriate.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.