objective-c等价于java匿名接口实现
我希望标题足够准确。 我想知道,如何用objc语言将接口实现传递给对象 在java中,它看起来像:objective-c等价于java匿名接口实现,objective-c,interface,anonymous-class,Objective C,Interface,Anonymous Class,我希望标题足够准确。 我想知道,如何用objc语言将接口实现传递给对象 在java中,它看起来像: public interface MyInterface { void onData(); } 实现类 public class ImplementMyInterface { // ... // other methods /// void registerInterface(){ MyInterface myInterface =
public interface MyInterface {
void onData();
}
实现类
public class ImplementMyInterface {
// ...
// other methods
///
void registerInterface(){
MyInterface myInterface = new MyInterface(){
@Override
public void onData(){
// process data here within the class
}
};
}
}
在objc中呢?
id myinterface
如何在课堂上实现它?
是否只有让类继承接口的可能性?
像
以及实现类
MyImplementingClass : MyInterface
或者还有其他可能性吗?
提前感谢Objective-C有匿名函数(块),但没有匿名类。因此,实现协议(接口的objective-c术语)的唯一方法是使某个类符合该协议(使用您的术语,使该类“继承”该协议),并在该类的实现中添加协议实现。我能够解决我的问题。 我只能在ImplementMyInterface.m文件中导入MyInterface头文件,而只能在ImplementMyInterface.h文件中导入。 所以我所能做的一切都在ImplementMyInterface.m文件中
// ImplementMyInterface.m
#import "MyInterface.h"
// inner class
@interface MyInternalInterface : NSObject<MyInterface>
@property (retain) ImplementMyInterface * implementation;
@end
// the actual class
@implementation ImplementMyInterface
MyInternalInterface * _internalInterface;
+(instancetype) build {
// construct myself
ImplementMyInterface * implementatMyInterface = [[ImplementMyInterface alloc] init];
// init inner class object
_internalInterface = [[MyInternalInterface alloc] init];
// register myself
[_internalInterface setImplementation:implementatMyInterface];
return implementatMyInterface;
}
- (NSString *) theActualData {
return @"The actual data";
}
// end of implementation class
@end
// implementation of inner class
@implementation MyInternalInterface
@synthesize implementation;
- (NSString *) onData {
if(implementation != nil)
return [implementation theActualData];
return @"";
}
// end of ImplementMyInterface.m
//ImplementMyInterface.m
#导入“MyInterface.h”
//内部阶级
@接口MyInternalInterface:NSObject
@属性(保留)实现MyInterface*实现;
@结束
//实际班级
@实现接口
MyInternalInterface*\u internalInterface;
+(instancetype)生成{
//构造自己
ImplementMyInterface*implementatMyInterface=[[ImplementMyInterface alloc]init];
//初始化内部类对象
_internalInterface=[[MyInternalInterface alloc]init];
//登记
[\u internalInterface setImplementation:implementatMyInterface];
返回实现MyInterface;
}
-(NSString*)实际数据{
返回@“实际数据”;
}
//实现类结束
@结束
//内部类的实现
@实现MyInternalInterface
@综合实施;
-(NSString*)onData{
if(实现!=nil)
返回[实现实际数据];
返回@”;
}
//ImplementMyInterface.m结束
Hi Free昵称,Hi@Johan,恐怕你评论的第二部分不见了:)这不是匿名界面。它是实现该接口的匿名类。Objective-C不允许任何匿名类。此外,由于Objective-C没有GC,因此实际上必须以某种方式保留委托。您不能只传递它,而不将它实际保存到某个局部变量。@Sulthan您能看看我发布的答案吗。它是否符合您对代表的提示?如果没有,请你解释一下。非常感谢。
// ImplementMyInterface.m
#import "MyInterface.h"
// inner class
@interface MyInternalInterface : NSObject<MyInterface>
@property (retain) ImplementMyInterface * implementation;
@end
// the actual class
@implementation ImplementMyInterface
MyInternalInterface * _internalInterface;
+(instancetype) build {
// construct myself
ImplementMyInterface * implementatMyInterface = [[ImplementMyInterface alloc] init];
// init inner class object
_internalInterface = [[MyInternalInterface alloc] init];
// register myself
[_internalInterface setImplementation:implementatMyInterface];
return implementatMyInterface;
}
- (NSString *) theActualData {
return @"The actual data";
}
// end of implementation class
@end
// implementation of inner class
@implementation MyInternalInterface
@synthesize implementation;
- (NSString *) onData {
if(implementation != nil)
return [implementation theActualData];
return @"";
}
// end of ImplementMyInterface.m