[Swift Learning five] type of exercise

Source: Internet
Author: User

------------------------///* Note: 1: Static function can not inherit 2: The protocol is not the same as OC, although there are optional and required keywords, but not good to make a ~ The static function (+) inside the 3:OC protocol can override Override,swift, prompting for the final function. Or Swift is better, more rigorous. In the 4:convenience constructor, it is not possible to use the form of self+. Init to play out, thought not to support it, the result of direct writing is no problem. But self cannot be omitted. The 5:convenience function cannot override the override 6: a function that changes member variables within a class without mutating modification, structure, and enumeration required. But the compiler has a hint, very convenient ~. 7: If the subclass also declares a function that is the same as the parent class, the compiler has to let the add-override,oc be overwritten.  This is not the same as Delphi.   @protocol classprotocol <NSObject> @required + (nsstring*) classdescription; -(nsstring*) Description; @end/* Do not declare, the direct implementation of the line ~*/@interface classtestbase:nsobject<classprotocol> + (nsstring*) classdescription; @end @interface classtestchild:classtestbase @end @implementation classtestbase + (nsstring*) classdescription{ret Urn @ "This is a class-based static function";   }-(nsstring*) description{return @ "This is a class-based member function";} @end @implementation Classtestchild-(nsstring*) description{ Return @ "This is a member function of class subclass";   } + (nsstring*) classdescription{return @ "This is a class subclass of static function";} @end @implementation Viewcontroller-(void) Viewdidload {[Super Viewdidload];   classtestbase* c = [[Classtestchild alloc] init]; NSLog (@ "%@:%@", C.description,[classtestchild classdescription]); }*/protocol Classprotocol {static func classdescription ()->string func Description () String}class classtes tbase:classprotocol{static func classdescription () String {return "This is a class-based static function"} var truename    : string= "" var id:int = 0; Private init (atruename:string,aid:int) {self.    Truename = Atruename self.id = AID;  } convenience init (atruename:string) {self.init (Atruename:atruename, AID:-1)} func Description () String {return "This is a member function of class base class"} Func Addmyid (), Void {ID + = 1}}class classtestchild : classtestbase {override func Description () String {return "This is a member function of class subclass"} Override Init (Atru    Ename:string, Aid:int) {super.init (Atruename:atruename, aid:aid) super.id = AID + 5; } ConveniencE init (atruename:string) {self.init (Atruename:atruename, AID:-2)} override Func Addmyid (), Void { Super. Addmyid () id+=1}}let obj:classtestbase = Classtestchild (atruename: "Hello") obj. Addmyid () print (obj.id)
The usage of the class is far more than these, today is only preliminary write down, later encountered what problems to change ~

[Swift Learning five] type of exercise

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.