//
// main.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "AudiCar.h"
#import "Person.h"
// OC里的枚举
NS_ENUM(NSUInteger, NSSeason){
NSSpring,
NSSummer,
NSAutumn,
NSWinter
};
NS_ENUM(NSUInteger, NSSeason2){
Spring =1 << 0,
Summer =1 << 1,
Autumn =1 << 2,
Winter =1 << 3,
Test =1 <<4
}; // 安位枚举,=后面必须是1
// NSUInteger 无符号正数 NSInteger正负都可以
int main(int argc,const char * argv[]) {
// 创建一个对象
// Student *stu = [[Student alloc] init];
// [stu sayHi];
// NSLog(@"%@", stu->_stuName);
// stu->_stuAge = 19;
//方法一共分为两种,一种是+号方法,称为类方法,这种方法一般类来进行使用,比如alloc
//第二种是-号方法,称为实例方法,一般对象来调用,比如我们自己写的SayHi,init
//有参数加冒号,无参数就不用加
// [stu setStuName:@"刘珊瑚" stuAge:29 stuSex:@"女" stuScore:0 stuHobby:@"女"];
// [stu sayHi];
// [stu setStuHobby:@"玩"];
// NSLog(@"%@", [stu stuHobby]);
// [stu setStuName:@"商帅"];
// NSLog(@"%@", [stu stuName]);
//
// AudiCar *car = [[AudiCar alloc] init];
// [car setColor:@"白色" Speed:200 Price:9998];
// [car sayHi];
// [car setColor:@"黑色"];
// NSLog(@"%@",[car color]);
// [car setSpeed:300];
// NSLog(@"%g", [car speed]);
// [car setPrice:998];
// NSLog(@"%g",[car price]);
Student *stu = [[Studentalloc] initWithStuName:@"姓名:商帅"stuAge:68stuScore:0stuHobby:@"不要脸"stuSex:@"女"];
[stusayHi];
AudiCar *car = [[AudiCaralloc] initWithColor:@"红色"Speed:200 Price:998];
[carsayHi];
[stusayHi];
// 消息机制
// [消息的接受者 具体要做的事]
NSLog(@"%ld",Spring);
NSLog(@"%ld",Summer);
NSLog(@"%ld",Autumn);
NSLog(@"%ld",Winter);
NSLog(@"%ld",Autumn | Winter);// |按位或
return 0;
}
//
// Person.h
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. 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
Age:(NSInteger)age;
// 自定义的初始化方法
- (id)initWithName:(NSString *)name
age:(NSInteger)age;
@end
//
// Person.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)setName:(NSString *)name
{
_name = name;
}
// 访问器
- (NSString *)name
{
return _name;
}
// 如果写声明,但是没有实现,会在.m文件中出现警告,告诉我们应该把方法补全
- (void)setName:(NSString *)name
Age:(NSInteger)age
{
_name = name;
_age = age;
}
- (id)initWithName:(NSString *)name
age:(NSInteger)age
{
_name = name;
_age = age;
return self;
}
@end
//
// B.h
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@class A;
// 交叉引入头文件
// @class 用@class可以防止交叉引入头文件的问题,系统会把A当成是一个已经存在的类来使用,不会把A.h里的内容进行拷贝,就是用了一下A类的名,其他不做任何的检测
@interface B : NSObject
{
A *_a;
}
@end
//
// B.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "B.h"
@implementation B
@end
//
// A.h
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "B.h"
@interface A : NSObject
{
B *_b;
}
@end
//
// A.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "A.h"
@implementation A
@end
//
// AudiCar.h
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface AudiCar :NSObject
// 三个成员变量,默认可见度
{
NSString *_color;
CGFloat _speed;
CGFloat _price;
}
- (void)sayHi;
// 对三个成员变量进行统一赋值的赋值语句
- (void)setColor:(NSString *)color
Speed:(CGFloat)speed
Price:(CGFloat)price;
// 三个成员变量的设置器
- (void)setColor:(NSString *)color;
- (void)setSpeed:(CGFloat)speed;
- (void)setPrice:(CGFloat)price;
// 访问器
- (NSString *)color;
- (CGFloat)speed;
- (CGFloat)price;
- (id)initWithColor:(NSString *)color
Speed:(CGFloat)speed
Price:(CGFloat)price;
@end
//
// AudiCar.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "AudiCar.h"
@implementation AudiCar
// 对三个成员变量进行统一赋值的赋值语句
- (void)setColor:(NSString *)color
Speed:(CGFloat)speed
Price:(CGFloat)price
{
_color = color;
_speed = speed;
_price = price;
}
- (void)sayHi{
NSLog(@"%@, %g, %g",_color, _speed,_price);
}
// 三个成员变量的设置器
- (void)setColor:(NSString *)color{
_color = color;
}
- (void)setSpeed:(CGFloat)speed{
_speed = speed;
}
- (void)setPrice:(CGFloat)price{
_price = price;
}
// 访问器
- (NSString *)color{
return _color;
}
- (CGFloat)speed{
return _speed;
}
- (CGFloat)price{
return _price;
}
- (id)initWithColor:(NSString *)color
Speed:(CGFloat)speed
Price:(CGFloat)price{
_color = color;
_speed = speed;
_price = price;
return self;
}
@end
//
// Student.h
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student :NSObject
// 特征
// 成员变量的可见度
// @public的级别最低,都可以用通过对象->的方式找到成员变量,不安全
// 如果没写可见度,默认是被保护的,被保护的成员变量可以在当前类及其子类来使用@protected
// @private 私有的:只能当前的类可以使用,使用的范围更小
// @package 这个是系统的一些文件来使用,系统来用,如果以后看见了,直接跳过去就行
{
@public
NSString *_stuName;
NSString *_stuSex;
@protected
NSString *_stuHobby;
@private
CGFloat _stuScore;
NSInteger _stuAge;
}
// 行为
- (void)sayHi;
- (void)eat;
- (void)play;
- (id)init;
// 对所有成员变量赋值的语句
// 只要是有参数,就一定要加冒号,冒号的作用相当于标识参数
// 在调用方法的时候我们通过参数形容词知道对应要填的参数,形参名是在方法实现的时候用的
- (void)setStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuSex:(NSString *)stuSex
stuScore:(CGFloat)stuScore
stuHobby:(NSString *)stuHobby;
// 设置器和访问器都是针对一个成员变量进行值的存储和读取,当对多个成员变量进行操作的时候,称为赋值语句
// 设置器 setter
- (void)setStuName:(NSString *)stuName;
- (void)setStuAge:(NSInteger)stuAge;
- (void)setStuSex:(NSString *)stuSex;
- (void)setStuScore:(CGFloat)stuScore;
- (void)setStuHobby:(NSString *)stuHobby;
// 访问器
- (NSString *)stuName;
- (NSInteger)stuAge;
- (NSString *)stuSex;
- (CGFloat)stuScore;
- (NSString *)stuHobby;
// 自定义的初始化方法
// 方法必须以init作为开头,后面跟一个with,W必须大写
- (id)initWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuHobby:(NSString *)stuHobby
stuSex:(NSString *)stuSex;
@end
//
// Student.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Student.h"
@implementation Student
- (void)sayHi{
NSLog(@"%@, %@, %@, %g, %ld",_stuName, _stuSex,_stuHobby, _stuScore,_stuAge);
}
- (void)eat{
NSLog(@"要吃饭");
}
- (void)play{
NSLog(@"要玩");
}
- (void)setStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuSex:(NSString *)stuSex
stuScore:(CGFloat)stuScore
stuHobby:(NSString *)stuHobby
{
_stuName = stuName;
_stuAge = stuAge;
_stuHobby = stuHobby;
_stuScore = stuScore;
_stuSex = stuSex;
}
- (void)setStuName:(NSString *)stuName{
_stuName = stuName;
}
- (void)setStuAge:(NSInteger)stuAge{
_stuAge = stuAge;
}
- (void)setStuSex:(NSString *)stuSex{
_stuSex = stuSex;
}
- (void)setStuScore:(CGFloat)stuScore{
_stuScore = stuScore;
}
- (void)setStuHobby:(NSString *)stuHobby{
_stuHobby = stuHobby;
}
- (NSString *)stuName{
return_stuName;
}
- (NSInteger)stuAge{
return_stuAge;
}
- (NSString *)stuSex{
return_stuSex;
}
- (CGFloat)stuScore{
return_stuScore;
}
- (NSString *)stuHobby{
return_stuHobby;
}
- (id)init{
_stuAge = 20;
_stuHobby = @"男";
_stuName = @"张三";
_stuScore = 75.5;
_stuSex = @"女";
return self;
}
- (id)initWithStuName:(NSString *)stuName
stuAge:(NSInteger)stuAge
stuScore:(CGFloat)stuScore
stuHobby:(NSString *)stuHobby
stuSex:(NSString *)stuSex{
_stuName = stuName;
_stuAge = stuAge;
_stuScore = stuScore;
_stuHobby = stuHobby;
_stuSex = stuSex;
return self;
}
@end
//
// test.h
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Student.h"
@interface test : Student
- (void)test;
@end
//
// test.m
// OC02_可见度,方法
//
// Created by dllo on 15/7/16.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "test.h"
@implementation test
- (void)test
{
NSLog(@"%@",_stuName);
}
@end