Swift object-oriented explanation, swift object-oriented

Source: Internet
Author: User

Swift object-oriented explanation, swift object-oriented

1. First, let's take a look at how to define attributes, constructors, methods, and class methods in a Class and Class in Swift.

Import Foundationclass Hello {// member attribute var _ name: NSString? = "Jikexueyuan" // constructor init () {}// overload constructor init (name: NSString) {_ name = name} // method func sayHello () {println ("Hello \ (_ name)")} // class method class func sayHi () {println ("Hello Swift")} var hello = Hello (name: "chen") hello. sayHello () Hello. sayHi ()

 

2. Inheritance in Swift is represented by colons like C. Once a class inherits its parent class, it also has the attributes and methods of the parent class. In a subclass, You can override the method of the parent class or call the method of the parent class.

Class Hi: Hello {// override the parent class method override func sayHello () {super. sayHello () // call the parent class method println ("Hi \ (_ name)")} var hi = Hi () hi. sayHello ()

 

3. When we use a third-party class library or a system class library during programming, we can use the extension keyword to implement some classes in these class libraries that need to be extended. If the subclass inherits the parent class with extension, the corresponding subclass should also have an extension method.

// Extension Hi {func sayHaha () {println ("Haha")} hi. sayHaha ()

 

4. Use the protocol keyword to define the interface in Swift.

protocol Person{    func getName()->NSString}class Man: Person {    func getName() -> NSString {        return "jikexueyuan"    }}var man = Man()var hiMan = Hi(name: man.getName())hiMan.sayHello()

 

5. Due to the inheritance of polymorphism, the Code has the transformation of sub-classes and parent classes, that is, the upward and downward transformations. An upward transformation is to assign a subclass instance to a reference variable of the parent class without any type conversion. Reference variables can only be converted to actual types during compilation. This type of forced conversion is downward transformation.

// Let obj: NSObject = "Hello" println (obj) // let objStr: NSString = obj! NSStringprintln (objStr)

 

6. In Swift, namespaces are implemented using nested methods of classes. However, when the namespaces are very complex and there are many classes in the namespaces, they will become very bloated. In this case, you can define the nesting of namespaces in a Swift file and use the extension keyword in another Swift file to extend the namespace, finally, you can directly call it in the used file.

//---Space.swiftclass com{    class jikexueyuan{        }}//---Hello.swiftextension com.jikexueyuan{    class Hello {        func sayHello(){            println("Hello")        }    }}//---Hi.swiftextension com.jikexueyuan{    class Hi  {        func sayHi(){            println("Hi")        }    }}//---Main.swiftvar spaceHello = com.jikexueyuan.Hello()spaceHello.sayHello()var spaceHi = com.jikexueyuan.Hi()spaceHi.sayHi()



Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.