oc构造函数和@property属性
2015-11-17 17:50
281 查看
oop是永恒不变的主题,无任是java还是obJect-c,只不过是语法不同而已,实质的东西相差不大,类和对象是最基础的,类事对象的组成,类由方法,属性等组成
一:java和oc创建对象:
java创建对象
无参数构造函数
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
A a=new A();
java默认有一个无参数构造函数
有参数构造函数
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
A a=new A(100,100);
oc创建对象
无参数构造函数
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
A* a=[[A alloc]init];
oc默认有一个init无参数构造函数
有参数构造函数
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
A* a =[[Aalloc] initWidthno:(100) andPhone:(100)];
伦理片 http://www.dotdy.com/
二:java和oc构造函数的创建 java默认构造函数
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
public A(){
}
java有参数构造函数
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
int _no;
int _phone;
public A(int _no,int _phone){
this._no-_no;
}
oc构造函数
默认构造函数
在.h文件中定义
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
-(id)init;//默认构造函数
在.m文件中实现
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
-(id)init{
if(self=[super init]){
}
return self;
}
有参构造函数
.h文件中定义参数
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
//定义构造函数 oc默认的构造函数时init();
//由于oc中的构造函数默认事init+大写字母 例如initWidthno
-(id)initWidthno:(int)no andPhone:(int)phone;
.m文件中实现.h中定义的方法
Oc代码
![](http://baihe747.iteye.com/images/icon_star.png)
//报错解决
//有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Cannot assign to 'self' outside of a method in the init family
//原因:只能在init方法中给self赋值,Xcode判断是否为init方法规则:方法返回id,并且名字以init+大写字母开头+其他 为准则。例如:- (id) initWithXXX;
//实现构造函数
-(id)initWidthno:(int)no andPhone:(int)phone{
// id是oc的动态加载类型,在运行时判断该类型是否存在
// self 代表自己,类似于java的this
// super表示父类,终于和java一样了
// 的默认构造函数时init开头,java是修饰符+类名
if(self=[super init]){
_phone= phone;
_no = no;
}
return self;
}
二:@property属性的使用,个人对@property的理解是,主要用来赋值和取值的
.h文件中定义
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
@interface MyStudent:NSObject{
//定义参数
int _no;
int _phone;
}
//使用@property来赋值和取参数
@property int _no;
@property int _phone;
.m文件中实现.h文件定义的参数
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
@synthesize _no;
@synthesize _phone;
mian文件中创建对象及调用
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
int main(int argc, const char * argv[]) {
@autoreleasepool {//自动回收池
//构造函数的使用
MyStudent* ms =[[MyStudent alloc] initWidthno:(1111) andPhone:(2222)];
NSLog(@"手机号码是=%d",[ms _phone]);
ms._no=9999;赋值
NSLog(@"%d",[ms _no]);
}
运行结果代码
![](http://baihe747.iteye.com/images/icon_star.png)
2015-11-02 22:54:47.075 test_01[883:59225] 手机号码是=2222
2015-11-02 22:54:47.076 test_01[883:59225] 9999
下面是全部完成代码:
.h文件定义方法属性
Oc代码
![](http://baihe747.iteye.com/images/icon_star.png)
//
// MyStudent.h
// test_01
//
// Created by wang on 15/11/2.
// Copyright © 2015年 wang. All rights reserved.
//
#ifndef MyStudent_h
#define MyStudent_h
@interface MyStudent:NSObject{
int _no;
int _phone;
}
//使用@property来赋值和取参数
@property int _no;
@property int _phone;
-(int)_no;
-(int)_phone;
//-(void)set_no:(int) newNo ;
//-(void)set_phone:(int) newPhone;
//定义构造函数 oc默认的构造函数时init();
-(id)initWidthno:(int)no andPhone:(int)phone;
@end
#endif /* MyStudent_h */
影音先锋电影 http://www.iskdy.com/
.m文件实现方法和属性
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
//
// MyStudent.m
// test_01
//
// Created by wang on 15/11/2.
// Copyright © 2015年 wang. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyStudent.h"
@implementation MyStudent
@synthesize _no;
@synthesize _phone;
////由于使用了proprety属性,所以不需要使用下面赋值方法
//-(int)_no{
// return _no;
//}
//
//-(int)_phone{
// return _phone;
//}
//报错解决
//有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Cannot assign to 'self' outside of a method in the init family
//原因:只能在init方法中给self赋值,Xcode判断是否为init方法规则:方法返回id,并且名字以init+大写字母开头+其他 为准则。例如:- (id) initWithXXX;
//实现构造函数
-(id)initWidthno:(int)no andPhone:(int)phone{
// id是oc的动态加载类型,在运行时判断该类型是否存在
// self 代表自己,类似于java的this
// super表示父类,终于和java一样了
// 的默认构造函数时init开头,java是修饰符+类名
if(self=[super init]){
_phone= phone;
_no = no;
}
return self;
}
@end
main文件
Java代码
![](http://baihe747.iteye.com/images/icon_star.png)
//
// main.m
// test_01
//
// Created by wang on 15/11/1.
// Copyright © 2015年 wang. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "MyStudent.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {//自动回收池
//默认构造函数创建对象
//MyStudent* ms=[[MyStudent alloc]init];
//构造函数的使用
MyStudent* ms =[[MyStudent alloc] initWidthno:(1111) andPhone:(2222)];
NSLog(@"手机号码是=%d",[ms _phone]);
ms._no=9999;
NSLog(@"%d",[ms _no]);
}
return 0;
}
相关文章推荐
- 淘宝架构框架
- linux 查看用户上次修改密码的日期
- tomcat 内存溢出问题
- mesos + marathon + docker部署
- Android系统运行架构之--生产-消费者模型详解
- linux系统编译安装软件的通用步骤
- linux系统编译安装软件的通用步骤
- hadoop估算π
- Yahoo开源的Apache Kafka管理工具:Kafka Manager
- Linux下安装nginx和php
- Linux下安装nginx和php
- AVAudioPlayer的简单使用
- tomcat 8.0 进程没有全部杀死
- 同时启动多个 tomcat 服务
- NSRunLoop事件回路及多线程
- Notes on Understanding the Linux Kernel
- 总结一下学习android开发的网站
- freopen ()函数
- Tomcat下,用MyEclipse建立web应用发布两种方法
- 设备树(五)linux内核主线booting-without-of.txt