Objective-C语言_面向对象(封装)
来源:互联网 发布:linux启动mysql客户端 编辑:程序博客网 时间:2024/06/05 05:47
main.m
#import <Foundation/Foundation.h> #import "Student.h" int main(int argc, const charchar * 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 ; @end
Student.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
0 0
- 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(OC)面向对象三大特性之:封装
- Objective-c语言_基本数据类型封装和拆包
- Objective-C语言_基本数据类型封装和拆包
- Objective C面向对象
- objective-c面向对象
- js转c++小记
- 使用TeamCity对项目进行可持续集成管理(一)
- js模拟百度主页搜索功能
- ecplise报错:ClassNotFound:ApplicationContext;
- JAVA随堂笔记课【四】:静态
- Objective-C语言_面向对象(封装)
- 【特效】js打造黑客帝国文字特效
- 网站中被误解的用户体验设计
- new()与newInstance() 的参数版本与无参数版本详解
- PowerPC 32位汇编入门讲解
- Android中Parcelable接口用法
- 操作系统环境变量LANG和NLS_LANG的关系
- 在github上写个人简历——最简单却又不容易的内容罗列
- [VBS] Working with DOS Command Line