#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age;
}
@end
@implementation Person
@end
@interface Student : Person
{
int _age;
}
@end
@implementation Student
@end
這裏 student 繼承 person 並且兩者都有一個一個成員變量 _age 這時候是不行的,程序報編譯錯誤
11-封裝.m:17:9: error: duplicate member '_age'
int _age;
^
11-封裝.m:5:9: note: previous declaration is here
int _age;
這是跟 java 不一樣的地方。
雖然有相同的屬性是不行的,但是可以有相同的方法,如果方法相同,系統會採取就近原則,調用離的近的方法。