Swift class, inheritance, interface

Source: Internet
Author: User

Import Foundationclasshello{var _name:string?="Swift Global"Init (name:string) {//Defines the constructor method for parameters in a class _name=name; println ("Hello, \ (name)"); } init () {//define no parameters in the class constructor method println ("This is init method"); } func SayHello () {//define Member method println ("Hello \ (_name)")    }        classfunc Meclass () {//Define class method println ("This is class method"); }    }classhi:hello{Overridefunc SayHello () {//inherits the Hello class and overloads its member method Super.sayhello ()//calls the parent class's method println ("Hloo override \ (_name)"); }}classhichild:hi{}//Inherit Hi class extension hi{//dynamically Expand Hi class, add a member method, inherit the Hi class subclass can also be adjusted With this method, Func Sayhaha () {println ("hhhaaa"); }}var H1=Hello ()//Instance Hello class, call the non-participating Init method, OutputThis is init method
= Hello (name:"Hello init")     //Call the parameter Init method, output:Hello, hello init
= Hi ()       //instantiate subclass, call the parent class's no-reference constructor, output This is init method
Hi.sayhello ()//method of calling class, output  hloo override Optional ("Swift global")

= Hichild () //instantiation, invoking the constructor of the parent class's parent class, output this is init method

H3.sayhaha () //Parent class extends a method, its subclasses can call this method, output hhhaaa
H3.sayhello () //Call the parent class method, output hloo override Optional ("Swift global")

Hello.meclass () //Call class method, output This is class

Swift class, inheritance, interface

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.