Objective-C程序设计第三章:类,对象和方法
来源:互联网 发布:linux查看cpu是几核 编辑:程序博客网 时间:2024/04/30 09:45
这一章简单介绍了如何书写Objective-C的类,对象和方法
类的声明:
@interface Computer: NSObject使用interface关键字,而不是传统面向对象的class。
在头文件中声明类,向其中加入方法和属性。
实例方法用 - 开头
类方法用 + 开头
俩个方法的区别:
1.类别符号不同
2.实例方法可以用实例变量, 类方法不可以用实例变量
#import <Foundation/Foundation.h>@interface Computer : NSObject{ NSString *kaka; int number;}- (void)setDoc: (NSString *)docContent;- (void)printDoc;- (NSString *)getDoc;- (void)setMovie: (NSString *)movieName;- (NSString *)getMovie;- (void)playMovie;+ (void)helloComputer;@end
类的定义(又叫实现):
#import "Computer.h"@implementation Computer{ NSString *doc; NSString *movie;}- (void)setDoc: (NSString *)docContent{ doc = docContent;}- (void)printDoc{ NSLog(@"The doc %@ is printing...", doc);}- (NSString *)getDoc{ return doc;}- (void)setMovie: (NSString *)movieName{ movie = movieName;}- (NSString *)getMovie{ return movie;}- (void)playMovie{ NSLog(@"We are watching movie %@", movie);}+ (void)helloComputer{ NSLog(@"Computer say 'hello' to you");}@end
因为无法直接访问类的实例变量。
类的创建和方法调用:
int main(int argc, const char * argv[]) { @autoreleasepool { [Computer helloComputer]; Computer *computer = [[Computer alloc] init]; [computer setDoc:@"真好玩"]; //[computer printDoc]; NSLog(@"I'm printing the doc %@", [computer getDoc]); [computer setMovie:@"异形大战铁血战士"]; // [computer playMovie]; NSLog(@"I'm watch movie %@", [computer getMovie]); } return 0;}
实例创建:类名 *实例 = [[类名 alloc] init];
类名 *实例 创建一个指针
[类名 alloc] 分配空间, init初始化内存空间
使用[实例名 方法名:参数]的方式调用实例方法。
0 0
- Objective-C程序设计第三章:类,对象和方法
- [学习笔记—Objective-C]《Objective-C 程序设计 第6版》第三章 类,对象和方法
- Objective-c第三章类、对象和方法
- [学习笔记—Objective-C]《Objective-C 程序设计 第6版》第三章 类,对象和方法 课后题解析 3-7
- Objective-C 程序设计 第三章
- [Objective-C] 类、对象和方法
- Objective-C面向对象-Objective-C 对象,实例和方法
- objective-c中的类方法和对象方法
- Objective-C 面向对象, 实例方法和类方法
- Objective-C类,对象,方法
- Objective-C:面向对象程序设计
- objective-c 基础语法和习题 :类、对象和方法
- Objective-C开发——类、对象和方法解析
- Objective-C学习笔记2:类、对象和方法
- Objective-C - 创建对象和调用方法
- Objective-C:类和对象
- Objective-C 类和对象
- Objective-C类和对象
- ios证书
- qt中的sleep
- iOS设备定位检测
- H - 算术表达式的转换
- 循环删除目录和文件函数
- Objective-C程序设计第三章:类,对象和方法
- magento incorrect ip format error
- Oracle 采集样本数据
- 自动识别页面上的所有控件是否被改变过
- iOS 消息推送原理及实现总结
- Qt 为何没有提供 Sleep
- Bellman-Ford算法
- 在Windows上安装Xamarin.iOS(下)
- Heron(二)—-系统架构