本帖最后由 Freedam 于 2014-6-10 12:07 编辑
代码如下:
apple.h
- #import <Foundation/Foundation.h>
- @interface Apple : NSObject
- @property int num;
- @property int size;
- - (void)test;
- - (int)appleSize;
- @end
复制代码
apple.m
- #import "Apple.h"
- @implementation Apple
- - (void)test
- {
- NSLog(@"apple");
- }
- - (int)appleSize
- {
- return _size;
- }
- @end
复制代码
plum.h
- #import <Foundation/Foundation.h>
- @interface Plum : NSObject
- @property int num;
- @property int size;
- - (void)test;
- - (int)plumSize;
- @end
复制代码
plum.m
- #import "Plum.h"
- @implementation Plum
- - (void)test
- {
- NSLog(@"plum");
- }
- - (int)plumSize
- {
- return _size;
- }
- @end
复制代码
main.m
- #import <Foundation/Foundation.h>
- #import "Apple.h"
- #import "Plum.h"
- int main()
- {
- Apple *a = [Apple new];
- a.num = 5;
- NSLog(@"num = %d",a.num);
-
- Plum *p = [Plum new];
- p.num = 8;
- NSLog(@"num = %d",p.num);
-
- id data=a;
- [data test];
- int size = [data appleSize];
- NSLog(@"size = %d",[data appleSize]);
-
- data = p;
- [data test];
- size = [data plumSize];
- NSLog(@"size = %d",size);
-
- return 0;
- }
复制代码 代码运行时报错如下:
问一下这样得怎样修改,为什么id类型转化成了别的类型。为什么可以准确调用appleSize方法,就是不能调用test方法
|