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