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.