oc 可见度,方法
// main.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "AudiCar.h"
// OC里的枚举
NS_ENUM(NSUInteger, NSSeason){
Spring,
Summer,
Autumn = 1<<4 ,
Winter = 1<<3
};
int main(int argc, const char * argv[]) {
// 创建一个对象
// Student *stu = [[Student alloc] init];
// [stu sayHi];
//
// NSLog(@"%@",stu->_stuHobby);
// 方法一共分为两种,一种是+号方法,称为类方法,这种方法一般类来进行使用,比如alloc
// 第二种方法是 - 号方法,称为实例方法,一般对象来调用,比如我们自己写的sayHi,init
// Student *stu =[[Student alloc] init];
// [stu setStuName:@"刘山山" setstuAge:29 setStuScore:77.5 setStuHobby:@"妹子" setStuSex:@"男"];
// [stu sayHi];
//
// [stu setStuHobby:@"玩"];
// NSLog(@"%@",[stu stuHobby]);
//
//
// AudiCar *car =[[AudiCar alloc] init];
// [car setColor:@"红" setSize:@"中" setMoney:10];
// [car sayHi];
// NSLog(@"%@ ,%@, %ld", [car color],[car size], [car money]);
// [car setColor:@"红" setSize:@"中" setMoney:10];
// NSLog(@"%@ ,%@, %ld", [car color],[car size], [car money]);
//
// Student *stu=[[Student alloc]initWithStuName:@"上帅" stuAge:20 stuScore:90 stuSex:@"男" stuHobby:@"臭美"];
// [stu sayHi];
//
// AudiCar *car =[[AudiCar alloc]initWithColor:@"黄色" size:@"中" money:100];
// [car sayHi];
// 消息机制
// [消息的接受者 具体要做的事]
NSLog(@"%ld",Winter);
NSLog(@"%ld",Autumn | Winter);
NSInteger a = 10, b =20;
a = a | b;
NSLog(@"%ld",a);
return 0;
}
// Person.h
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
// 特征
{
@public
NSString *_name;
NSString *_Sex;
@protected
NSInteger _age;
@private
NSString *_hobby;
}
//设置器
-(void)setName:(NSString *)name;
// 访问器
-(NSString *)name;
// 赋值语句
-(void)setName:(NSString *)name
setAge:(NSInteger)age;
// 自定义的初始化方法
@end
// Person.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import "Person.h"
@implementation Person
//设置器
-(void)setName:(NSString *)name{
_name =name;
}
// 访问器
-(NSString *)name{
return _name;
}
// 如果写声明,但是没有实现,会在.m文件中出现警告,告诉我们应该把方法补全
- (id)initWithName:(NSString *)name
age:(NSInteger)age
{
_name =name;
_age =age;
return self;
}
@end
// AudiCar.h
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface AudiCar : NSObject
// 三个成员变量,默认可见度
{
NSString *_color;
NSString *_size;
NSInteger _money;
}
-(void)sayHi;
//对三个成员变量进行统一赋值的赋值语句
-(void)setColor:(NSString *)color
setSize:(NSString *)size
setMoney:(NSInteger)money;
//三个成员变量的设置器
-(void)setColor:(NSString *)color;
-(void)setSize:(NSString *)size;
-(void)setMoney:(NSInteger)money;
//访问器
-(NSString *)color;
-(NSString *)size;
-(NSInteger)money;
// 自定义初始化方法
-(id)initWithColor:(NSString *)color
size:(NSString *)size
money:(NSInteger)money;
@end
// AudiCar.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import "AudiCar.h"
@implementation AudiCar
-(void)sayHi{
NSLog(@"%@, %@, %ld", _color ,_size ,_money);
}
-(void)setColor:(NSString *)color
setSize:(NSString *)size
setMoney:(NSInteger)money
{
_color =color;
_size =size;
_money=money;
}
//设置器
-(void)setColor:(NSString *)color{
_color =color;
}
-(void)setSize:(NSString *)size{
_size =size;
}
-(void)setMoney:(NSInteger)money{
_money =money;
}
//访问器
-(NSString *)color{
return _color;
}
-(NSString *)size{
return _size;
}
-(NSInteger)money{
return _money;
}
-(id)initWithColor:(NSString *)color
size:(NSString *)size
money:(NSInteger)money
{
_color =color;
_size =size;
_money = money;
return self;
}
@end
// Student.h
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
// 特征
// 成员变量的可见度
// @public的级别是最低的,都可以用的对象->的方式找到成员变量,不安全
// 如果没有写可见度,默认是被保护的,被保护的成员变量可以在当前类及其子类被使用
// @private 私有的:只能当前的类可以使用,使用范围更小
// @package 这个是系统一些文件来使用,系统来用,如果以后看见了,直接跳过去就行了
{
NSString *_stuName;
NSString *_stuSex;
NSString *_stuHobby;
CGFloat _stuScore;;
NSInteger _stuAge;
}
-(void)sayHi;
-(void)eat;
-(void)play;
// 对所有成员变量的赋值语句
// 只要有参数,就一定要见冒号,冒号的作用相当于标示参数
// 在调用方法的时候我们通过参数形容词知道对应要填的参数,星灿明是在方法名实现的时候用的
- (void)setStuName:(NSString *)stuName
setstuAge:(NSInteger)stuAge
setStuScore:(CGFloat)stuScore
setStuHobby:(NSString *)stuHobby
setStuSex:(NSString *)stuSex;
// 设置器和访问器都是针对一个成员变量进行的储存和读取,当多个成员变量进行操作的时候,称为赋值语句
// 设置器
-(void)setStuName:(NSString *)stuName;
-(void)setStuSex:(NSString *)stuSex;
-(void)setStuHobby:(NSString *)stuHobby;
-(void)setStuAge:(NSInteger )stuAge;
-(void)setStuScore:(CGFloat)stuScore;
// 访问器
-(NSString *)stuName;
-(NSString *)stuSex;
-(NSString *)stuHobby;
-(NSInteger)stuAge;
-(CGFloat)stuScore;
// 自定义的初始化方法
// 方法必须以init作为开头,后面跟一个with,W必须大写
-(id)initWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuSex:(NSString *)stuSex
stuHobby:(NSString *)stuHobby;
@end
// Student.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import "Student.h"
@implementation Student
-(void)sayHi{
NSLog(@"%@, %@, %@, %g, %ld",_stuName ,_stuHobby ,_stuSex, _stuScore, _stuAge);
}
-(void)eat{
NSLog(@"能吃");
}
-(void)play{
NSLog(@"要玩");
}
// 设置器
-(void)setStuName:(NSString *)stuName{
_stuName =stuName;
}
-(void)setStuSex:(NSString *)stuSex{
_stuSex =stuSex;
}
-(void)setStuHobby:(NSString *)stuHobby{
_stuHobby =stuHobby;
}
-(void)setStuAge:(NSInteger )stuAge{
_stuAge =stuAge;
}
-(void)setStuScore:(CGFloat)stuScore{
_stuScore =stuScore;
}
// 访问器
-(NSString *)stuName{
return _stuName;
}
-(NSString *)stuSex{
return _stuSex;
}
-(NSString *)stuHobby{
return _stuHobby;
}
-(NSInteger)stuAge{
return _stuAge;
}
-(CGFloat)stuScore{
return _stuScore;
}
-(id)init
{
_stuName =@"张三";
_stuScore = 90;
_stuHobby =@"男";
_stuSex =@"女";
_stuAge =23;
return self;
}
- (void)setStuName:(NSString *)stuName
setstuAge:(NSInteger)stuAge
setStuScore:(CGFloat)stuScore
setStuHobby:(NSString *)stuHobby
setStuSex:(NSString *)stuSex
{
_stuName =stuName ;
_stuAge = stuAge;
_stuScore = stuScore;
_stuSex = stuSex;
_stuHobby = stuHobby;
}
-(id)initWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuSex:(NSString *)stuSex
stuHobby:(NSString *)stuHobby
{
_stuAge =stuAge;
_stuName =stuName;
_stuScore =stuScore;
_stuSex =stuSex;
_stuHobby =stuHobby;
return self;
}
@end