Objective-C learning notes (2) class inheritance, combination, polymorphism, etc.

Source: Internet
Author: User

1. The inherited syntax is as follows:

@ Interface testcoop: nsobject {
Int Imonth;
Int iyear;
Int iday;
}

-(Void) setyear: (INT) iyear;
-(Void) primalsetdata: (INT) iyear :( INT) Imonth :( INT) iday;
-(Void) setdata: (INT) year Imonth :( INT) Imonth iday :( INT) iday;
-(Void) displaydateinfo;

@ End

@ Interface testaddweather: testcoop {
Nsstring * pstrweather;
}

-(Void) setweather: (nsstring *) pstrweather;
-(Void) displaydateinfo;
@ End

@ End

 

2. However, objective-C does not support multi-inheritance, similar to the following statement:

@ Interface testaddweather: testcoop, printableobect

An error will be reported during compilation. However, other features of objective-C can meet the requirements of Multi-inheritance functions and will be studied later.

 

The basic inheritance syntax and polymorphism are similar to those of C ++. See the implementation code:

@ Implementation testcoop
-(Void) displaydateinfo {
Nslog (@ "today is: % d. % d. % d/N", iyear, Imonth, iday );
}

-(Void) setyear: (INT) year {
Iyear = year;
}

-(Void) primalsetdata: (INT) year :( INT) month :( INT) day {
Iyear = year;
Imonth = month;
Iday = Day;
}

-(Void) setdata: (INT) year Imonth :( INT) month iday :( INT) day {
Iyear = year;
Imonth = month;
Iday = Day;
}

@ End

@ Implementation testaddweather
-(Void) setweather: (nsstring *) pweather {
[Pstrweather release];
Pstrweather = nil;
Pstrweather = [[nsstring alloc] initwithstring: pweather];
}
-(Void) displaydateinfo {
Nslog (@ "today is: % d. % d. % d, weather: % @/N", iyear, Imonth, iday, pstrweather );
}
@ End

 

 

Test code:

 

Int main (INT argc, const char * argv []) {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

// Insert code here...
// Nslog (@ "% d-, % S % @/N", 12, "hel123lo", @ "123 ");

Testcoop * Ptest = [[testcoop alloc] init];
[Ptest primalsetdata: 2009: 03: 05];
[Ptest displaydateinfo];
[Ptest setdata: 2010 Imonth: 06 iday: 06];
[Ptest displaydateinfo];
[Ptest setyear: 1987];
[Ptest displaydateinfo];
[Ptest release];
Testaddweather * ptestweather = [[testaddweather alloc] init];
[Ptestweather setdata: 2010 Imonth: 03 iday: 05];
[Ptestweather setweather: @ "Rainy"];
[Ptestweather displaydateinfo];
[Ptestweather setweather: @ "Cloudy"];
[Ptestweather displaydateinfo];
[Ptestweather release];
[Pool drain];
Return 0;
}

 

Print the following information:

Today is: 2009.3.5
Today is: 2010.6.6
Today is: 1987.6.6
Today is: 2010.3.5, weather: Rainy
Today is: 2010.3.5, weather: cloudy

 

If you want to call a function of the parent class, you can useSuper
Keywords

For example:

@ Implementation testaddweather
-(Void) setweather: (nsstring *) pweather {
[Pstrweather release];
Pstrweather = nil;
Pstrweather = [[nsstring alloc] initwithstring: pweather];

[Super setyear: 2009];
}

 

3. The combination is similar to that of C ++. To prevent repeated header file references, it has the same usage as C ++.

When the class members are only accessed through pointers of other classes, they can only declare

@ Class testaddweather;

As follows:

 

Then Import "testaddweather. H" in the. M/. MM file to prevent compilation from slowing down due to excessive header file dependencies.

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.