惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
T
Threatpost
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - Franky
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
S
Security Affairs
P
Proofpoint News Feed
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
S
Security @ Cisco Blogs
H
Hacker News: Front Page
Security Archives - TechRepublic
Security Archives - TechRepublic
Vercel News
Vercel News
Engineering at Meta
Engineering at Meta
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
月光博客
月光博客
量子位
博客园_首页
The Last Watchdog
The Last Watchdog
D
DataBreaches.Net
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
The Register - Security
The Register - Security
Schneier on Security
Schneier on Security
H
Help Net Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
L
Lohrmann on Cybersecurity
S
Secure Thoughts
Stack Overflow Blog
Stack Overflow Blog
Cloudbric
Cloudbric
Microsoft Security Blog
Microsoft Security Blog

博客园 - 似水流年-johnhuo

架构师向左,项目经理向右?? 今天迈出第一步,开始了解iphone开发 信息发布系统 Jquery+MVC架构开发(8)DAL层的补充 信息发布系统 Jquery+MVC架构开发(7) Controller层 . 信息发布系统 Jquery+MVC架构开发(6)BLL层提供WCF 服务 . 信息发布系统 Jquery+MVC架构开发(5) DAL层 信息发布系统 Jquery+MVC架构开发(4) Model 层 . 信息发布系统 Jquery+MVC架构开发(3) 解决方案创建 信息发布系统 Jquery+MVC架构开发(2) 在EA下进行数据库设计 信息发布系统 Jquery+MVC架构开发(1) 需求分析和架构构思 c# 设计模式(三)行为型 c# 设计模式(二)结构型 c# 设计模式(一)创建型 .NET平台上的Model-View-Presenter模式实践 关于cmmi的一些理解及资料 iphone 开发学习 C语言指针总结 Android 开发网站,记录一下 使用 TRY/CATCH 语句解决 SQL Server 2005 死锁
学习objective-c的一些特殊的地方
似水流年-johnhuo · 2011-12-17 · via 博客园 - 似水流年-johnhuo

objective-c 这种语言虽然说是基于c,但是跟c的衍生语言 c++,java,c#都区别不小,看着也让我很是不爽

首先是方法调用

[circle setFillColor:kRedColor]

cricle是类名,setFillColor是方法名,kRedColor是参数值,用惯了c#的人,估计用的郁闷。

重要的两个点:

(1)objective-c 多参数方法的定义与调用

方法定义举例:

-(void)insertObject:(id)anObject atIndex:(NSInteger)index

各部分解释:

1.方法修饰符

  - 代表此方法是实体方法,必须先生成类实例,通过实例才能调用该方法。

  + 代表此方法是类的静态方法,可以直接调用,而不用生成类实例。

2.参数类型

  id 与 NSInteger 分别是两个参数 anObject 和 index的类型。

3.方法签名

  本例中,insertObject 和 atIndex组成了该方法的签名关键字。

此处举例如下:

-(void) setTo: (int) n over: (int) d

{
numerator = n;
denominator = d;
}

[aFraction setTo: 100 over: 200];//调用

注:objective-c 的方法参数名有些怪异,第一个参数是没有参数名的,如果硬要说有,那就是方法名,

统一说来,见到冒号,冒号前面那个就是参数名。

再举例,没有参数名的方法定义与调用:

-(int) set: (int) n: (int) d;

[aFraction set: 1 : 3];//调用

在C#中,我们用接口来实现多态。比如接口IOb,定义了1个方法F; 有两个类A,B都实现了IOb接口。

IOb item = new A();

item.F();//执行的是A.F();

item = new B();

item.F();//执行的B.F();

在objective-c中,interface 的含义和C#有了很大的不同,不能这样使用。

那么如何实现类似的效果呢。那就是特殊类型id,看如下代码段,注释:Fraction 和 Complex都包含print 方法。

#import “Fraction.h”
#import “Complex.h”
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id dataValue;//定义了一个id 类型变量
Fraction *f1 = [[Fraction alloc] init];
Complex *c1 = [[Complex alloc] init];
[f1 setTo: 2 over: 5];
[c1 setReal: 10.0 andImaginary: 2.5];
// first dataValue gets a fraction
dataValue = f1;
[dataValue print]; //调用Fraction的 print方法
// now dataValue gets a complex number
dataValue = c1;
[dataValue print]; //调用Complex的 print方法

[c1 release];
[f1 release];
[pool drain];
return 0;
}