继承与方法重写
在 Objective-C 中通过子类化构建类层次结构,重写继承的方法,并使用 super 调用父类实现。
继承与方法重写 是 CoddyKit 上的免费 Objective-C iOS Development for Legacy & Enterprise Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Objective-C iOS Development for Legacy & Enterprise Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Inheritance?
Inheritance lets a class reuse and extend another class. The new subclass gets all the methods and properties of its superclass, then adds or changes behavior.
Declaring a Subclass
The colon syntax in the interface declares the superclass. Here Dog inherits from Animal.
@interface Dog : Animal
- (void)fetch;
@endInherited Members
A subclass automatically has the superclass's public methods and properties — no need to redeclare them.
Dog *d = [[Dog alloc] init];
[d eat]; // inherited from Animal
[d fetch]; // defined on DogOverriding a Method
To change inherited behavior, redeclare a method with the same name in the subclass. This is overriding.
@implementation Dog
- (void)makeSound {
NSLog(@"Woof!");
}
@endCalling super
Use super to invoke the superclass version, extending rather than replacing its behavior.
- (void)makeSound {
[super makeSound];
NSLog(@"...and wags tail");
}Overriding init
Custom initializers almost always call [super init] first to set up inherited state, then add their own.
- (instancetype)init {
self = [super init];
if (self) {
_legs = 4;
}
return self;
}Polymorphism
A superclass-typed variable can hold any subclass instance. The correct overridden method runs at runtime — this is polymorphism.
Animal *pet = [[Dog alloc] init];
[pet makeSound]; // prints Woof!The instancetype Return
Initializers return instancetype, which adapts to the actual class being created — safer than hardcoding the type.
+ (instancetype)dogWithName:(NSString *)name;Checking Class at Runtime
Objective-C is dynamic. You can ask an object about its class.
if ([pet isKindOfClass:[Dog class]]) {
NSLog(@"It is a dog");
}NSObject the Root
Almost every class ultimately inherits from NSObject, which provides core behavior like alloc, init, isEqual:, and memory management hooks.
Designing Hierarchies
Favor shallow hierarchies. Use inheritance for genuine is-a relationships; for shared behavior across unrelated classes, prefer protocols and categories instead.
Quick Check
Test your inheritance knowledge.
Recap
You learned inheritance in Objective-C:
- Subclass with the
: Superclasssyntax - Inherited methods and properties come for free
- Override methods to change behavior
- Use
superto extend the parent version - Polymorphism picks the right method at runtime
Inheritance and polymorphism are pillars of OOP in Objective-C.
常见问题解答
「继承与方法重写」课时是免费的吗?
是的 — 「继承与方法重写」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Objective-C iOS Development for Legacy & Enterprise Apps 课程的其余内容,请升级到 CoddyKit PRO。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。
「继承与方法重写」这节课中我会学到什么?
在 Objective-C 中通过子类化构建类层次结构,重写继承的方法,并使用 super 调用父类实现。 你通过在浏览器中直接运行的动手代码来练习 Objective-C iOS Development for Legacy & Enterprise Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Objective-C iOS Development for Legacy & Enterprise Apps 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Objective-C iOS Development for Legacy & Enterprise Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「继承与方法重写」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Objective-C iOS Development for Legacy & Enterprise Apps 课中编写并运行代码吗?
能。每节 Objective-C iOS Development for Legacy & Enterprise Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。