说实在的,写这第5篇的时候十分纠结,代码老是不能动态绑定,在编译时就会出错,最后发现这是开了ARC的原因。开了ARC obj-c就不能动态绑定了吗?这个问题还不清楚哦。把ARC关闭后,虽然会有警告,但运行是正确的。下面上代码:

#import <Foundation/Foundation.h>

@interface A:NSObject{
	double i;
}
	@property double i;
	-(double)mul:(double)x;
	-(void)show;
@end

@implementation A
	@synthesize i;

	-(double)mul:(double)x{
		return i * x;
	}

	-(void)show{
		NSLog(@"[A obj]i : %f",i);
	}
@end

@interface B:NSObject{
	int i;
}
	@property int i;

	-(int)mul:(int)x;
	-(void)show;
@end

@implementation B
	@synthesize i;

	-(int)mul:(int)x{
		return i * x;
	}

	-(void)show{
		NSLog(@"[B obj]i : %d",i);
	}
@end

int main(int argc,char *argv[])
{
	@autoreleasepool{
		NSLog(@"hello obj-c!");

		//A *a = [[A alloc] init];
		//id obj = a;
		id obj = [[A alloc] init];
		[obj setI:123.123];
		//[obj mul:123];
		[obj show];
		NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);
	}
	return 0;
}

这是开了ARC之后的编译,无法动态绑定鸟:

apple@kissAir: objc_src$clang -fobjc-arc -framework Foundation 2.m -o 2

2.m:52:3: error: multiple methods named 'setI:' found with mismatched result,

parameter type or attributes

[obj setI:123.123];

^~~~~~~~~~~~~~~~~~

2.m:6:19: note:
one possibility

@property double i;

^

2.m:26:16: note: also found

@property int i;

^

2.m:55:26: error: multiple methods named 'i' found with mismatched result,

parameter type or attributes

NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);

^~~~~~~

2.m:6:19: note:
one possibility

@property double i;

^

2.m:26:16: note: also found

@property int i;

^

2.m:55:40: error: multiple methods named 'mul:' found with mismatched result,

parameter type or attributes

NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);

^~~~~~~~~~~~~~~

2.m:7:2: note:
one possibility

-(double)mul:(double)x;

^~~~~~~~~~~~~~~~~~~~~~~

2.m:28:2: note:
also found

-(int)mul:(int)x;

^~~~~~~~~~~~~~~~~

3 errors generated.


这是关了ARC之后的编译和运行:

apple@kissAir: objc_src$clang -framework Foundation 2.m -o 2

2.m:52:3: warning: multiple methods named 'setI:' found

[obj setI:123.123];

^~~~~~~~~~~~~~~~~~

2.m:6:19: note: using

@property double i;

^

2.m:26:16: note: also found

@property int i;

^

2.m:55:26: warning: multiple methods named 'i' found

NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);

^~~~~~~

2.m:6:19: note: using

@property double i;

^

2.m:26:16: note: also found

@property int i;

^

2.m:55:40: warning: multiple methods named 'mul:' found

NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);

^~~~~~~~~~~~~~~

2.m:7:2: note: using

-(double)mul:(double)x;

^~~~~~~~~~~~~~~~~~~~~~~

2.m:28:2: note:
also found

-(int)mul:(int)x;

^~~~~~~~~~~~~~~~~

3 warnings generated.

apple@kissAir: objc_src$./2

2014-06-30 08:51:28.972 2[828:507] hello obj-c!

2014-06-30 08:51:28.974 2[828:507] [A obj]i : 123.123000

2014-06-30 08:51:28.974 2[828:507] 123.123000 * 99.990000 is 12311.068770

这个问题还没得到解决,希望哪位高人给予解答一下,在这之前我先跳过往后学鸟,毕竟时间不等笨猫啊!

[2014-07-05 第1次新增]:多态的第二个例子

我们来看看一个简单的代码,其正确实现了对象的多态功能,虽然编译时会有警告:

int main(int argc, char *argv[]){
	@autoreleasepool {
		msg(@"Hello World!");
		NSString *str = @"hello apple";
		NSObject *obj = str;
		id item = str;

		msg(@"%lu %lu",[obj length],[item length]);
	}
	return 0;
}

编译运行如下:

wisy@wisy-ThinkPad-X61:~/src/objc_src$ clang -objc-arc -O3 -g0 $OBJC_OPTS -lobjc -lgnustep-base -o 1 1.m
1.m:12:23: warning: 'NSObject' may not respond to 'length'
                msg(@"%lu %lu",[obj length],[item length]);
                                ~~~ ^
1.m:3:24: note: expanded from macro 'msg'
#define msg(...) NSLog(__VA_ARGS__)
                       ^
1 warning generated.
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./1
2014-07-05 09:45:58.092 1[3309] Hello World!
2014-07-05 09:45:58.095 1[3309] 11 11

前面一个问题答案貌似也清楚了,类B没有从A继承啊!你从NSObject继承,当运行时动态绑定的对象自然不知道到底是调用A还是B对象的方法鸟.

obj-c编程05:类的多态与id动态绑定的更多相关文章

  1. 第三章:Python高级编程-深入类和对象

    第三章:Python高级编程-深入类和对象 Python3高级核心技术97讲 笔记 3.1 鸭子类型和多态 """ 当看到一直鸟走起来像鸭子.游泳起来像鸭子.叫起来像鸭子 ...

  2. 并发编程 05—— Callable和Future

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

  3. C# 类的多态、结构、接口、抽象、虚函数总结

    多态: 类的多态是通过在子类(派生类)中重载基类的虚方法或成员函数来实现的. 可见,重载和虚函数的重写,并在调用时用父类装箱子类对象,是实现多态的一种重要的编程方式. 接口: 接口是一种用来定义程序的 ...

  4. day21-5 类的多态与多态性

    类的多态与多态性 多态 多态指的是一类事物有多种形态,如动物有多种形态:人.狗.猪 import abc class Animal(metaclass=abc.ABCMeta): # 同一类事物:动物 ...

  5. Python全栈开发之7、面向对象编程进阶-类属性和方法、异常处理和反射

    一.类的属性 1.@property属性 作用就是通过@property把一个方法变成一个静态属性 class Room: def __init__(self,name,length,width,he ...

  6. day18-Python运维开发基础(单继承 / 多继承 / 菱形继承、类的多态)

    1. 单继承 / 多继承 / 菱形继承 # ### 继承 : 一个类除了自身所拥有的属性方法之外,还获取了另外一个类的成员属性和方法 """ 一个类可以继承另外一个类,那 ...

  7. Python类的多态的例子

    1 # -*- coding: utf-8 -*- 2 # 类的多态 3 4 # 定义Person父类 5 class Person(object): 6 def __init__(self, nam ...

  8. 第二十九节:Java基础知识-类,多态,Object,数组和字符串

    前言 Java基础知识-类,多态,Object,数组和字符串,回顾,继承,类的多态性,多态,向上转型和向下转型,Object,数组,多维数组,字符串,字符串比较. 回顾 类的定义格式: [类的修饰符] ...

  9. Python 类的多态的运用

    #类的多态的运用 #汽车类 class Car(object): def move(self): print("move ...") #汽车商店类 class CarStore(o ...

随机推荐

  1. 17 ContentProvider

    1 Loader 转载器 Android3.0以后出来的 它可以使Activity和Fragment 异步加载数据 变得简单(Loader里封装了AsyncTask) 2 Loader特点: 对每一个 ...

  2. Dynamics CRM 将实体从高级查找列表中移除不可见

    有时我们不需要将某个实体显示给一般用户比如配置实体,但是这种类型的实体有时候又需要给一般用户读权限ODATA的时候得能读,站点地图上的隐藏比较容易用工具配置下权限即可.其实做到这步一般就可以了但有的客 ...

  3. 错误:One or more post-processing actions failed. Consult the OPP service log for details

     今天在做采购出入库明细报表的时候,有的时候能正常打印,有的时候报 One or more post-processing actions failed. Consult the OPP serv ...

  4. Android日历视图(CalendarView)讲解-android学习之旅(三十六)

    CalendarView简介 CalendarView用于显示和选择日期,如果希望监听事件的改变可以用setOnDateChangeListener()方法. CalendarView属性介绍 代码示 ...

  5. Java学习从菜鸟变大鸟之二 输入输出流(IO)

    在软件开发中,数据流和数据库操作占据了一个很重要的位置,所以,熟悉操作数据流和数据库,对于每一个开发者来说都是很重要的,今天就来总结一下JavaI/O. 流 流是一个很形象的概念,当程序需要读取数据的 ...

  6. Mybatis接口编程原理分析(三)

    前面两篇博客Mybatis接口编程原理分析(一)和Mybatis接口编程原理分析(二)我们介绍了MapperProxyFactory.MapperProxy和MapperMethod的操作及源码分析, ...

  7. μC/OS-II与RT-Thread对比——任务调度

           在任务调度器的实现上,μC/OS-II和RT-Thread都采用了位图调度(bitmap scheduling),任务优先级的值越小则代表具有越高的优先级,主要区别在于实现形式,是采用多 ...

  8. python的sorted

    读入后,要进行组内排序,按groupseq字段排序后,然后统计前后两个项的个数,累加到全局. sorted函数使用如下: def sortlist(alllist):     sorted_key1_ ...

  9. Linux Shell -- 无网不利

    这篇文章中我介绍几个非常实用的和网络相关的命令 一.ifconfig 这个命令在Windows下被"翻译为ipconfig",它用于显示网络接口,子网掩码等详细信息. 注:在每个系 ...

  10. AngularJS进阶(三十)AngularJS项目开发技巧之图片预加载

    AngularJS项目开发技巧之图片预加载 绪 项目(移动端采用Ionic 框架)开发完毕,测试阶段发现移动APP首页的广告图片(图片由服务器端返回相应url地址)很难加载,主要原因还是网速.如下图左 ...