无法识别的选择器发送到实例 0x10010c730->目标 C
我是编程新手。我看过这段代码。将派生类对象返回给基类。 这样基类就可以指向派生类的方法。 这里 B 类中的静态函数将其对象返回到基类 班级。
基派生类.m
#import <Foundation/Foundation.h>
#import "B.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[B p];
[pool drain];
return 0;
}
A.h
#import <Foundation/Foundation.h>
@interface A : NSObject {
}
@end
A.m
#import "A.h"
@implementation A
@end
B.h
#import <Foundation/Foundation.h>
#import "A.h"
@interface B : A {
}
+(A*)p;
-(void)display;
@end
B.m
#import "B.h"
@implementation B
+(A*)p
{
NSLog(@"returning derived class object to the base class!!");
return [B new];
}
-(void)display
{
NSLog(@"Hello");
}
@end
I am new to programming.I have seen this code.returning a derived class object to the base class.
So that the base class can then point to the derived class methods.
Here a static function in class B is returning its object to the base
class.
base-derivedclass.m
#import <Foundation/Foundation.h>
#import "B.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[B p];
[pool drain];
return 0;
}
A.h
#import <Foundation/Foundation.h>
@interface A : NSObject {
}
@end
A.m
#import "A.h"
@implementation A
@end
B.h
#import <Foundation/Foundation.h>
#import "A.h"
@interface B : A {
}
+(A*)p;
-(void)display;
@end
B.m
#import "B.h"
@implementation B
+(A*)p
{
NSLog(@"returning derived class object to the base class!!");
return [B new];
}
-(void)display
{
NSLog(@"Hello");
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
p
是一个类方法。在 Obj-C 中,您可以通过在方法声明中使用+
来表示类方法,并使用-
来表示实例方法。您可以使用以下方法调用类方法:或者您可以通过以下方式将
p
更改为实例方法:您可以检查 Objective-C:入门 开始使用这些内容。
p
is a class method. In Obj-C you denote class method by using+
in method declaration and-
to denote instance method. You can call the class method by using this:Or you can change
p
to instance method by this:You can check Objective-C: A Primer to get started with these.
这里的内存管理也存在一些混乱:
您创建并耗尽自动释放池,以便将内容放入其中(创建后),然后立即处理它们(当您耗尽它们时)。但你还没有向池中放入任何东西。您已经使用“alloc”创建了“der”,这意味着您“拥有”它用于内存管理目的,而不是将其放入自动释放池中,在那里它将被自动处理。
如果我所做的只是让你更加困惑,你可能应该看看一些关于 Objective-C 的入门书籍。他们都在某个时候涵盖了这个主题。或者您可以查看 Apple 有关内存管理的文档,但他们假设您已经了解某些事情。 (这很令人困惑,所以要有耐心......)
There's also some confusion in your memory management, here:
You create and drain autorelease pools in order to put things into them (after creation) and then dispose of them right away (as you drain them). But you haven't put anything into the pool. You've used "alloc" to create "der," which means you "own" it for memory management purposes, as opposed to its being put in an autorelease pool where it will be taken care of automatically.
If all I've done is confuse you more, you should probably check out some introductory book on Objective-C. They all cover this topic at some point. Or you could look at Apple's docs on memory management, but they assume you already know certain things. (And it IS confusing, so be patient...)