参考以下代码:
- #import <Foundation/Foundation.h>
- @interface Person : NSObject//Person类,表示人,拥有_age和_weight 2个属性,继承自NSObject
- {
- @public
- int _age;
- double _weight;
- }
- @end
- @interface GoodStudent : Person// GoodStudent类,表示学生,继承自Person类
- {
- @public
- int _mathScore; // GoodStudent类拥有Person类的_age和_weight 2个属性
- int _OCScore; // 此外,GoodStudent类还拥有自己的3个属性
- int _ChineseScore;
- }
- @end
- @interface Score : NSObject // Score类,表示分数,拥有_mathScore、_OCScore和_ChineseScore 3个属性,继承自NSObject
- {
- @public
- int _mathScore;
- int _OCScore;
- int _ChineseScore;
- }
- @end
- @interface BadStudent : Score // BadStudent类,表示学生,继承自Score类,拥有Score类的3个属性
- {
- @public // 此外,BadStudent类还拥有自己的_age和_weight 2个属性
- int _age;
- double _weight;
- }
- @end
- int main()
- {
- GoodStudent *goodStu = [GoodStudent new];
- goodStu->_age = 10;
- goodStu->weight = 56.2;
- goodStu->_mathScore = 98;
- goodStu->_OCScore = 88;
- goodStu->_ChineseScore = 96;
-
- BadStudent *badStu = [BadStudent new];
- badStu->_age = 10;
- badStu->weight = 56.2;
- badStu->_mathScore = 98;
- badStu->_OCScore = 88;
- badStu->_ChineseScore = 96;
- NSLog(@”goodStu--> age:%d , weight:%f kg , mathScore:%d , OCScore:%d , ChineseScore:%d” , goodStu->_age , goodStu->weight , goodStu->_mathScore , goodStu->_OCScore , goodStu->_ChineseScore);
- NSLog(@” badStu--> age:%d , weight:%f kg , mathScore:%d , OCScore:%d , ChineseScore:%d” , badStu->_age , badStu->weight , badStu->_mathScore , badStu->_OCScore , badStu->_ChineseScore);
- return 0;
- }
复制代码
|
|