Objective-c语言_面向对象(封装)
2015-12-11 19:01
471 查看
1.封装
main.m
main.m
#import <Foundation/Foundation.h> #import "Student.h" int main(int argc, const char * argv[]) { @autoreleasepool { #pragma mark ----------封装--------------- /* 封装:隐藏内部的实现,稳定外部接口 好处: 使用起来更加简单 变量更加安全 可以隐藏内部实现细节 开发速度更加快捷 作用: 方法封装了具体实现的代码 属性封装实例变量 类封装了属性和方法 */ NSString *name = @"Rick"; NSInteger age = 25; NSString *homeAddress = @"GZ"; NSLog(@"Teacher names %@,%ld years old,living in %@",name,age,homeAddress); Student *student = [[Student alloc]init]; //方法封装了具体实现的代码 [student helloWorld]; // [student hiGuys]; //私有方法不被外界所调用 } return 0; }Student.h
#import <Foundation/Foundation.h> //在OC中,几乎所有的类都继承于NSObject,剩下的都继承于NSProxy @interface Student : NSObject { //使用类来封装变量 // NSString *_name; // NSInteger _age; // NSString *_homeAddress ; } //使用@property封装成员变量,实现变量的安全 @property (nonatomic,strong)NSString *name ; @property (nonatomic,assign)NSInteger age ; @property (nonatomic,strong)NSString *homeAddress; -(void)helloWorld ; @endStudent.m
#import "Student.h" @implementation Student -(id)init { if (self = [super init]) { _name = @"Rick"; _age = 25; _homeAddress = @"GZ"; } return self; } //使用类来封装功能代码 -(void)helloWorld { //打印哪个类里面的哪个方法 NSLog(@"%s",__FUNCTION__); NSLog(@"helloWorld!"); NSLog(@"Teacher names %@,%ld years old,living in %@",_name,_age,_homeAddress); [self hiGuys]; } //私有方法:在@interface中无相应声明方法,可以把他们看做私有方法,仅在类的实现中使用 -(void)hiGuys { NSLog(@"%s",__FUNCTION__); NSLog(@"我是私有方法"); } @end
相关文章推荐
- javascript:用object对象模拟java的map集合
- java list<Map<String,Object>>排序
- JSON使用JsonConstructorAttribute,JsonConverterAttribute,JsonObjectAttribute
- Objective-C运行时消息派发机制
- Objective-C总Runtime的那点事儿
- OC中 instancetype、id、NSObject的区别
- Objective-C instancetype关键字
- typedef void (^ SuccesBlock)(id object);
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object
- Scala:object(单例)
- 追求Masonry(看过最好的masonry中文博文)
- 理解 Objective-C 的 ARC
- Swift学习: 从Objective-C到Swift
- object-c开发规范
- firefox-Developer开发者站点——关于Object.create()新方法的介绍
- SQL Server中 sysobjects、syscolumns、systypes
- Object-C NSLog 常用输出基本数据类型格式
- BING++: A Fast High Quality Object Proposal Generator at 100fps
- flash object实现视频播放效果
- Xcode7使用MJ的NSObject+Value出现Too many arguments to function call,expected 0,have 3