课堂笔记03
2015-07-27 10:07
253 查看
课堂笔记03
这一节将自定义初始化方法//
// main.m
// OC_03
//
// Created by Ibokan on 15/7/24.
// Copyright (c) 2015年 Law. All rights reserved.
//
#import <Foundation/Foundation.h> #import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *st1 = [[Student alloc] init]; NSLog(@"st的地址为%p",st1); //st1.sID = 2013; st1.name = @"大头"; st1.age = 23; Student *st2 = [[Student alloc] initWithID:@"2013306" SexChoice:@"女"]; st2.name = @"小头"; st2.age = 25; } return 0;
}
//
// Student.h
// OC_03
//
// Created by Ibokan on 15/7/24.
// Copyright (c) 2015年 Law. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
// @public
// NSString *_hero;
// NSString *_name;
// NSString *_name2;
NSString *_sexChoice;
}
//-(void)move;
@property (readonly,nonatomic,retain)NSString *sID;
@property (nonatomic,retain)NSString *name;
@property (nonatomic,assign)NSInteger age;
//自定义初始化方法
/*
initWith//固定写法
*/
-(id)initWithID:(NSString )sID SexChoice:(NSString )sexChoice;
@end
//
// Student.m
// OC_03
//
// Created by Ibokan on 15/7/24.
// Copyright (c) 2015年 Law. All rights reserved.
//
#import "Student.h"
@implementation Student
/*
self:表示指向当前实例对象的指针;
super:表示指向父类对象的指针;
super init:调用父类的init指针;
*/
-(id)init{
self = [super init];
NSLog(@”self的地址为%p”,self);
if (self) {
_sID = @”2013306”;
_sexChoice = @”女”;
}
return self;
}//若没写,系统默认写了,若写了,系统调用该方法
-(id)initWithID:(NSString )sID SexChoice:(NSString )sexChoice{
if (self = [super init]) {
_sID = sID;
_sexChoice = sexChoice;
}
return self;
}
-(void)setAge:(NSInteger)age{
if (age < 0) {
_age = 18;
}
else {
_age = age;
}
}
@end
相关文章推荐
- 快搜搜:2015年应届生怎么找工作?
- atoi (String to Integer) leetcode
- 【转】windows c++获取文件信息——_stat函数的使用
- Google跨域配置
- 并查集详解(转)
- 机房收费之上机、结账分析
- Android模拟Http POST 请求
- Android中Activity间通信 -----intent & SharedPreferences
- 2.14-if的使用注意
- Android控件开发之EditView
- Android 控件开发之ToggleButton
- Windows下Python包和模块的安装方法
- 南阳oj 表达式求值 题目305 数据结构
- 小技巧
- spark遇到的一些问题及其解决办法
- mysqldump导出数据库命令及mysql导入数据库命令
- c++里,输入输出方式
- 2.15-if练习
- 自学Swift-斯坦福笔记整理(七)
- Android控件开发之TextView