OC 30 day
第一次打开 objc 的文件,看到了一个关键字 @interface
先看看wiki怎麽说吧:
interface :
定义部分,清楚定义了类的名称、成员变数和方法。 以关键字@interface作为开始,@end作为结束。
[color=#d2ed28] In object-oriented programming, a protocol or interface is a common means for unrelated objects to communicate with each other. These are definitions of methods and values which the objects agree upon in order to cooperate.
接口约定了对象间交互的属性和方法,使得对象间无需了解对方就可以协作。
Objective-C中强制要求将类的定义(interface)与实现(implementation)分为两个部分。
类的定义档案遵循C语言之惯例以.h为字尾,实现档案以.m为字尾。
这也就是为什麽我们在Objc里看到.h .m 的文件。
对比@interface和@implementation
@interface 我们干过的事:
1、继承
2、声明协议
3、定义实例变量(@interface後面加大括号那种)
4、定义@property
5、声明方法
@implementation 我们干过的和可以干的事:
1、继承
2、定义实例变量
3、合成属性(@synthesize和@dynamic)
4、实现方法(包括协议方法)
定义部分,清楚定义了类的名称、成员变数和方法。 以关键字@interface作为开始,@end作为结束。
@interface MyObject : NSObject {
int memberVar1; // 实体变量
id memberVar2;
}
+(return_type) class_method; // 类方法
-(return_type) instance_method1; // 实例方法
-(return_type) instance_method2: (int) p1;
-(return_type) instance_method3: (int) p1 andPar: (int) p2;
@end
方法前面的 +/- 号代表函式的类型:加号(+)代表类别方法(class method),不需要实例就可以呼叫,与C++ 的静态函式(static member function)相似。减号(-)即是一般的实例方法(instance method)。
实现区段则包含了公开方法的实现,以及定义私有(private)变数及方法。 以关键字@implementation作为区段起头,@end结尾。
@implementation MyObject {
int memberVar3; //私有实体变数
}
+(return_type) class_method {
.... //method implementation
}
-(return_type) instance_method1 {
....
}
-(return_type) instance_method2: (int) p1 {
....
}
-(return_type) instance_method3: (int) p1 andPar: (int) p2 {
....
}
@end
值得一提的是不只Interface区段可定义实体变数,Implementation区段也可以定义实体变数,两者的差别在於存取权限的不同,Interface区段内的实体变数默认权限为protected,宣告於implementation区段的实体变数则默认为private,故在Implementation区段定义私有成员更符合物件导向之封装原则,因为如此类别之私有资讯就不需曝露於公开interface(.h档案)中。
是和调用者的一个protocol,没错,就是protocol。
看过不少代码,从@interface设计上多少就能看出作者的水平,分享下我对於这个问题的一些拙见。
// Sark.h
@interface SarkViewController : NSObject <NSXMLParserDelegate /*1*/, NSCopying> {
NSString *_name; // 2
IBOutlet UITextField *_nameTextField; // 2
}
@property (nonatomic, strong) NSXMLParser *parser; // 3
- (IBAction)nameChangedAction:(id)sender; // 4
@end
个interface出现的问题:
原文网址:https://kknews.cc/culture/n5zr2x3.html
>>: sql 用ASCII Control Character 去空白,去换行,去tab
CollectionView:Storyboard、Xib + Collection View + ...
本文目标 了解计算机如何储存资料 了解计算机如何处理负数以及减法 练习进制间的转换 进制系统 进位制...
今天要来做gotop按钮啦 我们先来看一下程序码 function gotoTop() { if (...
前情提要 经过上一回的测验,我发现了自己不是天选之人。 「哪尼,为什麽我不是天选之人,我不是有魔力吗...
团队成员的气氛,就应该是既竞争又是合作,而这个最高境界很大一部份要上面的PM推动。 因为人性是这样的...