ios自定义构造方法
2015-08-25 14:26
405 查看
ios中,自定义构造方法
Student.h
#improt<Foundation/Foundation.h>
@interface Student:Object{
int _age;
}
-(void)setAge:(int)age;
-(int)age;
@end
Student.m
#import "Student.h"
@implement Student
-(void)setAge:(int)age{
_age = age;
}
-(void)age{
return age;
}
@end
在main文件中,只需要实例化它就可以进行使用了
//核心
Student *student = [[Student all]init];//为其分配内存也可以说是实例化
student.age = 12;
以上是在外部文件进行自定义函数
在文件内部进行函数自定义:
调用:
[self initWithTitle:@"Hello World" initWithAuthor:@"Lin" initWithAge:10]
//方法体
-(instancetype)initWithTitle:(NSString*)title initWithAuthor:(NSString*)author initWithAge:(NSInteger*)age{
self.title = title;
self.author = author;
self.age = age;
}
跟JAVA 的不同点:
在java中写个构造方法(函数)直接写就行了
public setAge( int age){
return age;
}
Student.h
#improt<Foundation/Foundation.h>
@interface Student:Object{
int _age;
}
-(void)setAge:(int)age;
-(int)age;
@end
Student.m
#import "Student.h"
@implement Student
-(void)setAge:(int)age{
_age = age;
}
-(void)age{
return age;
}
@end
在main文件中,只需要实例化它就可以进行使用了
//核心
Student *student = [[Student all]init];//为其分配内存也可以说是实例化
student.age = 12;
以上是在外部文件进行自定义函数
在文件内部进行函数自定义:
调用:
[self initWithTitle:@"Hello World" initWithAuthor:@"Lin" initWithAge:10]
//方法体
-(instancetype)initWithTitle:(NSString*)title initWithAuthor:(NSString*)author initWithAge:(NSInteger*)age{
self.title = title;
self.author = author;
self.age = age;
}
跟JAVA 的不同点:
在java中写个构造方法(函数)直接写就行了
public setAge( int age){
return age;
}
相关文章推荐
- Object-C基础-04-自定义构造方法和description方法
- 自定义构造方法
- ios中 input 焦点光标不垂直居中
- OC中重写构造方法及自定义构造方法
- iOS利用通知(NSNotification)进行传值
- iOS 辛格尔顿
- ios语法——委托和协议
- 条形码、二维码扫描
- ios日期格式转换
- iO7以上CIFilter生成二维码(备忘) 此博文包含图片 (2015-04-24 14:48:25)转载
- iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
- 使用Nagios搭建监控服务器
- iOS中Foundation的常见知识点
- IOS 多线程(4) --线程通讯
- iOS中属性与成员变量的区别
- 自学 iOS 开发的一些经验
- GT随身调 for ios
- IOS 多线程(3) --线程安全
- iOS闪退bug修复
- IOS 多线程(2) --NSThread