Analyzing the inheritance characteristics in the object programming of Swift language physiognomy _swift

Source: Internet
Author: User
Tags inheritance stdin

The ability to take greater than form is defined as inheritance. A general class can inherit properties and methods from another class. Classes can be further divided into subclasses and superclass.

Subclasses: When a class inherits properties from another class, methods and functions are called subclasses

Superclass: Classes contain attributes, methods, and functions are inherited by other classes called super Classes

The classes in Swift contain the parent class and call access methods, properties, functions, and overriding methods. In addition, property watchers are also used to add properties and modify the methods of stored or computed attributes.

Base class
A class is called a "base class" if it does not inherit methods, properties, or functions from other classes.

Copy Code code as follows:

Classstuddetails{var Stname:string!var Mark1:int!var Mark2:int!var mark3:int!
Init (stname:string, Mark1:int, Mark2:int, mark3:int) {self.stname = Stname
SELF.MARK1 = Mark1
SELF.MARK2 = Mark2
SELF.MARK3 = Mark3
}}let stname = "Swift" let Mark1 =98let mark2 =89let mark3 =76

println (Stname)
println (MARK1)
println (MARK2)
println (MARK3)


When we use playground to run the above program, we get the following results.

Swift
76

Here the Studdetails class is defined as the base class, which is used to contain the student name and three account marks: Mark1, MARK2, and Mark3. The ' let ' keyword is initialized in playground and uses the "println" function to print the values of the underlying class.

Sub Class
Defining a new class on an existing base class is called a subclass. Subclasses inherit the properties, methods, and functionality of their base classes. To define a subclass to use ":" Before the base class name.

Copy Code code as follows:

Classstuddetails{var Mark1:int;var Mark2:int;

Init (Stm1:int, results stm2:int) {
Mark1 = STM1;
MARK2 = STM2;}

Func print () {
println ("Mark1:\ (MARK1), mark2:\ (MARK2)")}}class display:studdetails{
Init () {super.init (stm1:93, results:89)}}let marksobtained = display ()
Marksobtained.print ()


When we use playground to run the above program, we get the following results.

mark1:93, mark2:89

The "Studdetails" class is defined as the superclass of the student tag declaration and the ' display ' of the subclass inherits from its superclass for marking. Subclasses define student tags and call print methods to display student flags.

Overwrite/rewrite
accesses instances of the superclass, type methods, for example, type properties and subscript subclasses provide the concept of overrides. The ' override ' keyword is used to override the method declared in the superclass.

Access to Super class methods, properties, and subscripts
the "super" keyword is used as a prefix to access the methods, properties, and subscripts declared in the superclass.


method and property override methods override the
methods that inherit instances and types can override methods defined in subclasses through the ' override ' keyword. Here, override printing in subclasses to access the superclass print Type property.

Copy Code code as follows:

Class Cricket {
Func print () {
println ("Welcome to Swift Super Class")}}class tennis:cricket {override func print () {
println ("Welcome to Swift Sub Class")}}let cricinstance = Cricket ()
Cricinstance.print () Let tennisinstance = Tennis ()
Tennisinstance.print ()


When we use playground to run the above program, we get the following results.

Welcome to Swift Super class
Welcome to Swift Sub class

Property Overrides
you can override an inherited instance or class property to provide custom getter and setter properties, or add a property observer to make overriding the property to the observer when the underlying property value changes.

overriding property getter and setter
Swift allows users to provide custom getter and setter overrides for inherited properties, whether stored or computed. The subclass does not know the inherited property name and type. Therefore, it is essential that the user specify the name in the subclass and specify the type of overridden property in the superclass.

This can be done in two ways:

When a setter is defined as an override property, the user must also define the getter.

When we do not want to modify the getter of inherited properties, we can inherit values from the superclass by using the simple syntax "Super.someproperty".

Copy Code code as follows:

Classcircle{var radius =12.5var Area:string{return "of rectangle for \ (RADIUS)"}}classrectangle:circle{varprint= 7overridevar Area:string{returnsuper.area + "is now overridden as \ (print)"}}let rect =rectangle ()
Rect.radius =25.0
Rect.print=3
println ("Radius \ (Rect.area)")


When we use playground to run the above program, we get the following results.

Radius of rectangle for 25.0 are now overridden as 3

overriding property observers
when a new attribute needs to be added for inherited attributes, the concept of "property overrides" is introduced in Swift. Notifies the user when the inherited property value is changed. However, overrides do not apply to inherited constant storage properties and inherited read-only computed properties.

Copy Code code as follows:

Classcircle{var radius =12.5var Area:string{return "of rectangle for \ (RADIUS)"}}classrectangle:circle{varprint= 7overridevar Area:string{returnsuper.area + "is now overridden as \ (print)"}}let rect =rectangle ()
Rect.radius =25.0
Rect.print=3
println ("Radius \ (Rect.area)") Classsquare:rectangle{overridevar radius:double{
Didset {print=int (radius/5.0) +1}}}let sq =square ()
Sq.radius =100.0
println ("Radius \ (Sq.area)")


When we use playground to run the above program, we get the following results.

Radius of rectangle for 25.0 are now overridden as 3
Radius of rectangle for 100.0 are now overridden as 21

Final property to prevent overriding
when a user does not need to have access to a superclass method, attribute or subscript, Swift introduces the "final" attribute to prevent overwriting. When the final attribute is declared, the superclass's methods, attributes, and subscripts are not allowed to be overwritten. In the superclass can not have ' final '. When the final attribute is declared, the user restricts child class creation.

Copy Code code as follows:

Finalclasscircle{finalvar radius =12.5var Area:string{return "of rectangle for \ (RADIUS)"}}classrectangle:circle{ Varprint=7overridevar Area:string{returnsuper.area + "is now overridden as \ (print)"}}let rect =rectangle ()
Rect.radius =25.0
Rect.print=3
println ("Radius \ (Rect.area)") Classsquare:rectangle{overridevar radius:double{
Didset {print=int (radius/5.0) +1}}}let sq =square ()
Sq.radius =100.0
println ("Radius \ (Sq.area)")


When we use playground to run the above program, we get the following results.

Copy Code code as follows:

<stdin>:14:18:error:var overrides a ' final ' var
Override Var area:string {
^
<stdin>:7:9: Note:overridden declaration is here
var area:string {
^
<stdin>:12:11:error:inheritance from a final class ' Circle '
Class Rectangle:circle {
^
<stdin>:25:14:error:var overrides a ' final ' var
Override Var radius:double {
^
<stdin>:6:14:note:overridden declaration are here
Final var radius = 12.5


When the superclass declaration is final and the data type is also declared as ' final ', the program will not allow the subclass to be created, or it will raise an error.

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.