H:/Objective-C/OC_day2/00-Xcode文档安装.m
H:/Objective-C/OC_day2/01-封装.m
H:/Objective-C/OC_day2/02-封装细节.m
H:/Objective-C/OC_day2/03-封装练习.m
H:/Objective-C/OC_day2/04-OC弱语法.m
H:/Objective-C/OC_day2/05-类方法.m
H:/Objective-C/OC_day2/06-类方法练习.m
H:/Objective-C/OC_day2/07-self.m
H:/Objective-C/OC_day2/08-self.m
H:/Objective-C/OC_day2/09-self.m
H:/Objective-C/OC_day2/10-self使用注意.m
H:/Objective-C/OC_day2/11-继承.m
H:/Objective-C/OC_day2/12-继承.m
H:/Objective-C/OC_day2/13.继承的使用场合.m
H:/Objective-C/OC_day2/14-super.m
H:/Objective-C/OC_day2/15-多态.m
H:/Objective-C/OC_day2/16-NSString.m
H:/Objective-C/OC_day2/17-改错题.m
H:/Objective-C/OC_day2/18-分析题.m
H:/Objective-C/OC_day2/19-类的设计-01-车.m
H:/Objective-C/OC_day2/19-类的设计-02-人.m
H:/Objective-C/OC_day2/19-类的设计-03-人书学生.m
H:/Objective-C/OC_day2/19-类的设计-04-Car.m
H:/Objective-C/OC_day2/19-类的设计-05-点.m
H:/Objective-C/OC_day2/19-类的设计-06-圆.m
1.直接放到Xcode中
/Applications/Xcode.app/Contents/Developer/Documentation/DocSets
2.放到硬盘的某个路径
/Users/用户名/Library/Developer/Shared/Documentation/DocSets
H:/Objective-C/OC_day2/01-封装.m
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
// 成员变量尽量不要用@public
// @public
int age;
//@public
// 只读(readonly):只允许外界访问我的no,不允许外界修改我的no
int no; // 只需要提供get方法
}
//
/*
set方法
1.作用: 提供一个方法给外界设置成员变量值,可以在方法里面对参数进行相应过滤
2.命名规范:
1> 方法名必须以set开头
2> set后面跟上成员变量的名称,成员变量的首字母必须大写
3> 返回值一定是void
4> 一定要接收一个参数,而且参数类型跟成员变量类型一致
5> 形参的名称不能跟成员变量名一样
*/
- (void)setAge:(int)newAge;
/*
get方法
1.作用:返回对象内部的成员变量
2.命名规范:
1> 肯定有返回值,返回值类型肯定与成员变量类型一致
2> 方法名跟成员变量名一样
3> 不需要接收任何参数
*/
- (int)age;
- (void)study;
@end
@implementation Student
// set方法的实现
- (void)setAge:(int)newAge
{
// 对传进来的参数进行过滤
if (newAge <= 0)
{
newAge = 1;
}
age = newAge;
}
- (int)age
{
return age;
}
- (void)study
{
NSLog(@"%d岁的学生在学习", age);
}
@end
int main()
{
Student *stu = [Student new];
//stu->age = -10;
//stu->age = 10;
[stu setAge:10];
NSLog(@"学生的年龄是%d岁", [stu age]);
//[stu study];
return 0;
}
H:/Objective-C/OC_day2/02-封装细节.m
#import <Foundation/Foundation.h>
typedef enum {
SexMan,
SexWoman
} Sex;
@interface Student : NSObject
{/*成员变量的命名规范:一定要以下划线 _ 开头
作用:
1.让成员变量和get方法的名称区分开
2.可以跟局部变量区分开,一看到下划线开头的变量,一般都是成员变量
*/
int _no;
Sex _sex;
}
// sex的set和get方法
- (void)setSex:(Sex)sex;
- (Sex)sex;
// no的set和get方法
- (void)setNo:(int)no;
- (int)no;
@end
@implementation Student
- (void)setSex:(Sex)sex
{
_sex = sex;
}
- (Sex)sex
{
return _sex;
}
- (void)setNo:(int)no
{
_no = no;
}
- (int)no
{
return _no;
}
@end
int main()
{
Student *stu = [Student new];
[stu setSex:SexMan];
[stu setNo:10];
[stu sex];
[stu no];
return 0;
}
H:/Objective-C/OC_day2/03-封装练习.m
/*
4.设计一个成绩类
* C语言成绩(可读可写)
* OC成绩(可读可写)
* 总分(只读)
* 平均分(只读)
*/
#import <Foundation/Foundation.h>
@interface Score : NSObject
{
int _cScore; // C语言成绩
int _ocScore; // OC成绩
int _totalScore;// 总分
int _averageScoe; // 平均分
}
- (void)setCScore:(int)cScore;
- (int)cScore;
- (void)setOcScore:(int)ocScore;
- (int)ocScore;
- (int)totalScore;
- (int)averageScore;
@end
@implementation Score
- (void)setCScore:(int)cScore
{
_cScore = cScore;
// 计算总分
_totalScore = _cScore + _ocScore;
_averageScoe = _totalScore/2;
}
- (int)cScore
{
return _cScore;
}
- (void)setOcScore:(int)ocScore
{
_ocScore = ocScore;
// 计算总分
_totalScore = _cScore + _ocScore;
_averageScoe = _totalScore/2;
}
// 监听成员变量的改变
- (int)ocScore
{
return _ocScore;
}
- (int)totalScore
{
return _totalScore;
}
- (int)averageScore
{
return _averageScoe;
}
@end
int main()
{
Score *s = [Score new];
[s setCScore:90];
[s setOcScore:100];
[s setCScore:80];
int a = [s totalScore];
NSLog(@"总分:%d", a);
return 0;
}
H:/Objective-C/OC_day2/04-OC弱语法.m
#import <Foundation/Foundation.h>
// 尽管编译器容错能力比较,但是写代码必须规范
@interface Person : NSObject
- (void)test;
@end
@implementation Person
- (void)test
{
NSLog(@"哈哈哈");
}
@end
// 一旦运行过程中出错,就会闪退
/*
-[Person test]: unrecognized selector sent to instance 0x7fd2ea4097c0
给Person对象发送了一个不能识别的消息:test
*/
int main()
{
Person *p = [Person new];
// OC是在运行过程中才会检测对象有没有实现相应的方法
[p test];
return 0;
}
H:/Objective-C/OC_day2/05-类方法.m
#import <Foundation/Foundation.h>
/*
对象方法
1> 减号 - 开头
2> 只能由对象来调用
3> 对象方法中能访问当前对象的成员变量(实例变量)
类方法
1> 加号 + 开头
2> 只能由类(名)来调用
3> 类方法中不能访问成员变量(实例变量)
类方法的好处和使用场合
1> 不依赖于对象,执行效率高
2> 能用类方法,尽量用类方法
3> 场合:当方法内部不需要使用到成员变量时,就可以改为类方法
可以允许类方法和对象方法同名
*/
@interface Person : NSObject
{
int age;
}
// 类方法都是以+开头
+ (void)printClassName;
- (void)test;
+ (void)test;
@end
@implementation Person
+ (void)printClassName
{
// error:instance variable 'age' accessed in class method
// 实例变量age不能在类方法中访问
//NSLog(@"这个类叫做Person-%d", age);
}
- (void)test
{
NSLog(@"111-%d", age);
//[Person test];
}
+ (void)test
{
// 会引发死循环
//[Person test];
NSLog(@"333");
// 会引发死循环
// /[Person test];
}
@end
int main()
{
//[Person printClassName];
[Person test];
//Person *p = [Person new];
//[p test];
/*
-[Person printClassName]: unrecognized selector sent to instance 0x7fa520c0b370
*/
// 系统会认为现在调用的printClassName是个对象方法
//[p printClassName];
return 0;
}
H:/Objective-C/OC_day2/06-类方法练习.m
/*
设计一个计算器类
* 求和
* 求平均值
*/
#import <Foundation/Foundation.h>
// 工具类:基本没有任何成员变量,里面的方法基本都是类方法
@interface JiSusnQi : NSObject
+ (int)sumOfNum1:(int)num1 andNum2:(int)num2;
+ (int)averageOfNum1:(int)num1 andNum2:(int)num2;
@end
@implementation JiSusnQi
+ (int)sumOfNum1:(int)num1 andNum2:(int)num2
{
return num1 + num2;
}
+ (int)averageOfNum1:(int)num1 andNum2:(int)num2
{
int sum = [JiSusnQi sumOfNum1:num1 andNum2:num2];
return sum / 2;
}
@end
int main()
{
int a = [JiSusnQi averageOfNum1:10 andNum2:12];
NSLog(@"a=%d", a);
// JiSusnQi *jsq = [JiSusnQi new];
//
//
// [jsq sumOfNum1:10 andNum2:13];
return 0;
}
H:/Objective-C/OC_day2/07-self.m
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age;
}
- (void)setAge:(int)age;
- (int)age;
- (void)test;
@end
@implementation Person
- (void)setAge:(int)age
{
// _age = age;
self->_age = age;
}
- (int)age
{
return self->_age;
}
- (void)test
{
// self:指向了方向调用者,代表着当期对象
int _age = 20;
NSLog(@"Person的年龄是%d岁", self->_age);
}
@end
int main()
{
Person *p = [Person new];
[p setAge:10];
[p test];
return 0;
}
H:/Objective-C/OC_day2/08-self.m
#import <Foundation/Foundation.h>
/*
self的用途:
1> 谁调用了当前方法,self就代表谁
* self出现在对象方法中,self就代表对象
* self出现在类方法中,self就代表类
2> 在对象方法利用"self->成员变量名"访问当前对象内部的成员变量
2> [self 方法名]可以调用其他对象方法\类方法
*/
@interface Dog : NSObject
- (void)bark;
- (void)run;
@end
@implementation Dog
- (void)bark
{
NSLog(@"汪汪汪");
}
- (void)run
{
[self bark];
//NSLog(@"汪汪汪");
NSLog(@"跑跑跑");
}
@end
int main()
{
Dog *d = [Dog new];
[d run];
return 0;
}
H:/Objective-C/OC_day2/09-self.m
/*
设计一个计算器类
* 求和
* 求平均值
*/
#import <Foundation/Foundation.h>
// 工具类:基本没有任何成员变量,里面的方法基本都是类方法
@interface JiSusnQi : NSObject
+ (int)sumOfNum1:(int)num1 andNum2:(int)num2;
+ (int)averageOfNum1:(int)num1 andNum2:(int)num2;
@end
@implementation JiSusnQi
+ (int)sumOfNum1:(int)num1 andNum2:(int)num2
{
return num1 + num2;
}
+ (int)averageOfNum1:(int)num1 andNum2:(int)num2
{
// 在这种情况下,self代表类
int sum = [self sumOfNum1:num1 andNum2:num2];
return sum / 2;
}
@end
int main()
{
int a = [JiSusnQi averageOfNum1:10 andNum2:12];
NSLog(@"a=%d", a);
// JiSusnQi *jsq = [JiSusnQi new];
//
//
// [jsq sumOfNum1:10 andNum2:13];
return 0;
}
H:/Objective-C/OC_day2/10-self使用注意.m
#import <Foundation/Foundation.h>
@interface Person : NSObject
- (void)test;
+ (void)test;
- (void)test1;
+ (void)test2;
- (void)haha1;
+ (void)haha2;
@end
@implementation Person
- (void)test
{
NSLog(@"调用了-test方法");
// 会引发死循环
//[self test];
}
+ (void)test
{
NSLog(@"调用了+test方法");
// 会引发死循环
//[self test];
}
- (void)test1
{
[self test]; // -test
}
+ (void)test2
{
[self test]; // +test
}
- (void)haha1
{
NSLog(@"haha1-----");
}
void haha3()
{
}
+ (void)haha2
{
// haha3();
[self haha3];
// [self haha1];
}
@end
int main()
{
[Person haha2];
//Person *p = [Person new];
//[p test1];
return 0;
}
H:/Objective-C/OC_day2/11-继承.m
#import <Foundation/Foundation.h>
/*
1.继承的好处:
1> 抽取重复代码
2> 建立了类之间的关系
3> 子类可以拥有父类中的所有成员变量和方法
2.注意点
1> 基本上所有类的根类是NSObject
*/
/********Animal的声明*******/
@interface Animal : NSObject
{
int _age;
double _weight;
}
- (void)setAge:(int)age;
- (int)age;
- (void)setWeight:(double)weight;
- (double)weight;
@end
/********Animal的实现*******/
@implementation Animal
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
- (void)setWeight:(double)weight
{
_weight = weight;
}
- (double)weight
{
return _weight;
}
@end
/********Dog*******/
// : Animal 继承了Animal,相当于拥有了Animal里面的所有成员变量和方法
// Animal称为Dog的父类
// Dog称为Animal的子类
@interface Dog : Animal
@end
@implementation Dog
@end
/********Cat*******/
@interface Cat : Animal
@end
@implementation Cat
@end
int main()
{
Dog *d = [Dog new];
[d setAge:10];
NSLog(@"age=%d", [d age]);
return 0;
}
H:/Objective-C/OC_day2/12-继承.m
/*
1.重写:子类重新实现父类中的某个方法,覆盖父类以前的做法
2.注意
1> 父类必须声明在子类的前面
2> 子类不能拥有和父类相同的成员变量
3> 调用某个方法时,优先去当前类中找,如果找不到,去父类中找
2.坏处:耦合性太强
*/
#import <Foundation/Foundation.h>
// Person
@interface Person : NSObject
{
int _age;
}
- (void)setAge:(int)age;
- (int)age;
- (void)run;
+ (void)test;
@end
@implementation Person
+ (void)test
{
NSLog(@"Person+test");
}
- (void)run
{
NSLog(@"person---跑");
}
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
@end
// 不允许子类和父类拥有相同名称的成员变量
// Student
@interface Student : Person
{
int _no;
// int _age;
}
+ (void)test2;
@end
@implementation Student
// 重写:子类重新实现父类中的某个方法,覆盖父类以前的做法
- (void)run
{
NSLog(@"student---跑");
}
+ (void)test2
{
[self test];
}
@end
int main()
{
[Student test2];
// Student *s = [Student new];
//
// [s run];
return 0;
}
H:/Objective-C/OC_day2/13.继承的使用场合.m
/*
1.继承的使用场合
1> 当两个类拥有相同属性和方法的时候,就可以将相同的东西抽取到一个父类中
2> 当A类完全拥有B类中的部分属性和方法时,可以考虑让B类继承A类
A
{
int _age;
int _no;
}
B : A
{
int _weight;
}
// 继承:xx 是 xxx
// 组合:xxx 拥有 xxx
2.组合
A
{
int _age;
int _no;
}
B
{
A *_a;
int _weight;
}
*/
@interface Score : NSObject
{
int _cScore;
int _ocScore;
}
@end
@implementation Score
@end
@interface Student : NSObject
{
// 组合
Score *_score;
// int _cScore;
// int _ocScore;
int _age;
}
@end
@implementation Student
@end
H:/Objective-C/OC_day2/14-super.m
/*
僵尸
跳跃僵尸、舞王僵尸、铁桶僵尸
*/
#import <Foundation/Foundation.h>
/*
super的作用
1.直接调用父类中的某个方法
2.super处在对象方法中,那么就会调用父类的对象方法
super处在类方法中,那么就会调用父类的类方法
3.使用场合:子类重写父类的方法时想保留父类的一些行为
*/
// 僵尸
@interface Zoombie : NSObject
- (void)walk;
+ (void)test;
- (void)test;
@end
@implementation Zoombie
- (void)walk
{
NSLog(@"往前挪两步******");
}
+ (void)test
{
NSLog(@"Zoombie+test");
}
- (void)test
{
NSLog(@"Zoombie-test");
}
@end
// 跳跃僵尸
@interface JumpZoombie : Zoombie
+ (void)haha;
- (void)haha2;
@end
@implementation JumpZoombie
+ (void)haha
{
[super test];
}
- (void)haha2
{
[super test];
}
- (void)walk
{
// 跳两下
NSLog(@"跳两下");
// 走两下(直接调用父类的walk方法)
[super walk];
//NSLog(@"往前挪两步----");
}
@end
int main()
{
//[JumpZoombie haha];
JumpZoombie *jz = [JumpZoombie new];
[jz haha2];
return 0;
}
H:/Objective-C/OC_day2/15-多态.m
#import <Foundation/Foundation.h>
/*
多态
1.没有继承就没有多态
2.代码的体现:父类类型的指针指向子类对象
3.好处:如果函数\方法参数中使用的是父类类型,可以传入父类、子类对象
4.局限性:
1> 父类类型的变量 不能 直接调用子类特有的方法。必须强转为子类类型变量后,才能直接调用子类特有的方法
*/
// 动物
@interface Animal : NSObject
- (void)eat;
@end
@implementation Animal
- (void)eat
{
NSLog(@"Animal-吃东西----");
}
@end
// 狗
@interface Dog : Animal
- (void)run;
@end
@implementation Dog
- (void)run
{
NSLog(@"Dog---跑起来");
}
- (void)eat
{
NSLog(@"Dog-吃东西----");
}
@end
// 猫
@interface Cat : Animal
@end
@implementation Cat
- (void)eat
{
NSLog(@"Cat-吃东西----");
}
@end
// 这个函数是专门用来喂动画
//void feed(Dog *d)
//{
// [d eat];
//}
//
//void feed2(Cat *c)
//{
// [c eat];
//}
//
// 如果参数中使用的是父类类型,可以传入父类、子类对象
void feed(Animal *a)
{
[a eat];
}
int main()
{
// NSString *d = [Cat new];
// [d eat];
/*
Animal *aa = [Dog new];
// 多态的局限性:父类类型的变量 不能 用来调用子类的方法
//[aa run];
// 将aa转为Dog *类型的变量
Dog *dd = (Dog *)aa;
[dd run];
*/
//Dog *d = [Dog new];
//[d run];
/*
Animal *aa = [Animal new];
feed(aa);
Dog *dd = [Dog new];
feed(dd);
Cat *cc = [Cat new];
feed(cc);
*/
/*
// NSString *s = [Cat new];
Animal *c = [Cat new];
NSObject *n = [Dog new];
NSObject *n2 = [Animal new];
// 多种形态
//Dog *d = [Dog new]; // Dog类型
// 多态:父类指针指向子类对象
Animal *a = [Dog new];
// 调用方法时会检测对象的真实形象
[a eat];
*/
return 0;
}
H:/Objective-C/OC_day2/16-NSString.m
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
//char *_name;
NSString *_name;
}
@end
int main()
{
/*
// 最简单的创建字符串的方式
NSString *str = @"itcast";
char *name = "itcast";
NSLog(@"我在%@上课", str);
//NSLog(@"%s", name);
*/
int age = 15;
int no = 5;
NSString *name = @"哈哈jack";
// length方法算的是字数
int size = [name length];
NSLog(@"%d", size);
// 创建OC字符串的另一种方式
NSString *newStr = [NSString stringWithFormat:@"My age is %d and no is %d and name is %@", age, no, name];
NSLog(@"---- %ld", [newStr length]);
return 0;
}
H:/Objective-C/OC_day2/17-改错题.m
1.对象方法类方法
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age;
}
- (void)test1;
+ (void)test2;
@end
@implementation Person
- (void)test1
{
NSLog(@"调用了test1方法");
}
+ (void)test2
{
// 在类方法中,self代表的是类。不能用类调用对象方法test1
// [self test1];
// 类方法中不能访问成员变量
// NSLog(@"调用了test2方法-%d", _age);
}
@end
int main()
{
Person *p = [Person new];
// 对象不能调用类方法
// [p test2];
// 类不能调用对象方法
// [Person test1];
}
// 4处错误
2.继承
#import <Foundation/Foundation.h>
// 父类要声明在子类的前面
@interface Animal : NSObject
{
int _age;
double _weight;
}
- (void)test1;
+ (void)test2;
@end
@interface Dog : Animal
{
// 子类不能拥有和父类一样的成员变量
// int _age;
double _height;
}
@end
@implementation Dog
- (void)test1
{
NSLog(@"test1----");
// 这里的super是用来调用父类对象方法的,而test2是个类方法
// [super test2];
}
@end
@implementation Animal
- (void)test1
{
NSLog(@"test1----");
}
+ (void)test2
{
NSLog(@"test2----");
}
@end
// 3处错误
H:/Objective-C/OC_day2/18-分析题.m
1.self的使用
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age;
}
- (void)test1;
- (void)test2;
+ (void)test2;
+ (void)test3;
+ (void)test4;
- (void)test4;
@end
@implementation Person
- (void)test1
{
_age = 20;
int _age = 10;
NSLog(@"调用了-test1方法-%d", _age);
[self test2];
}
- (void)test2
{
int _age = 10;
NSLog(@"调用了-test2方法-%d", self->_age);
}
+ (void)test2
{
int _age = 20;
NSLog(@"调用了+test2方法-%d", _age);
}
+ (void)test3
{
NSLog(@"调用了+test3方法");
[self test4];
}
+ (void)test4
{
NSLog(@"调用了+test4方法");
}
- (void)test4
{
NSLog(@"调用了-test4方法");
}
@end
int main()
{
[Person test3];
Person *p = [Person new];
[p test1];
return 0;
}
2007-01-01 21:59:43.956 testOc[11962:303] 调用了+test3方法
2007-01-01 21:59:43.957 testOc[11962:303] 调用了+test4方法
2007-01-01 21:59:43.958 testOc[11962:303] 调用了-test1方法-10
2007-01-01 21:59:43.959 testOc[11962:303] 调用了-test2方法-20
2.继承
/*
#import <Foundation/Foundation.h>
@interface Animal : NSObject
- (void)test1;
+ (void)test1;
@end
@implementation Animal
- (void)test1
{
NSLog(@"--test1");
}
+ (void)test1
{
NSLog(@"++test1");
}
@end
@interface Dog : Animal
- (void)test3;
@end
@implementation Dog
- (void)test3
{
NSLog(@"--test3");
[super test1];
}
@end
int main()
{
Dog *dog = [Dog new];
[dog test1];
[dog test3];
return 0;
}
*/
2007-01-01 22:00:14.535 testOc[11971:303] Dog-test1
2007-01-01 22:00:14.537 testOc[11971:303] Animal-test1
2007-01-01 22:00:14.537 testOc[11971:303] Dog-test3
3.继承
#import <Foundation/Foundation.h>
@interface Animal : NSObject
- (void)test1;
+ (void)test1;
@end
@implementation Animal
- (void)test1
{
NSLog(@"--test1");
}
+ (void)test1
{
NSLog(@"++test2");
}
@end
@interface Dog : Animal
- (void)test3;
@end
@implementation Dog
- (void)test3
{
NSLog(@"--test3");
[super test1];
}
@end
int main()
{
Dog *dog = [Dog new];
[dog test1];
[dog test2];
[dog test3];
return 0;
}
2007-01-01 22:01:09.177 testOc[11978:303] --test1
2007-01-01 22:01:09.179 testOc[11978:303] --test3
2007-01-01 22:01:09.179 testOc[11978:303] --test1
4.多态
#import <Foundation/Foundation.h>
@interface Animal : NSObject
- (void)test1;
@end
@implementation Animal
- (void)test1
{
NSLog(@"Animal-test1");
}
@end
@interface Dog : Animal
@end
@implementation Dog
- (void)test1
{
NSLog(@"Dog-test1");
}
@end
int main()
{
Animal *a = [Dog new];
[a test1];
Animal *a2 = [Animal new];
[a2 test1];
Dog *a3 = [Dog new];
[a3 test1];
return 0;
}
2007-01-01 22:01:52.630 testOc[11985:303] Dog-test1
2007-01-01 22:01:52.632 testOc[11985:303] Animal-test1
2007-01-01 22:01:52.633 testOc[11985:303] Dog-test1
H:/Objective-C/OC_day2/19-类的设计-01-车.m
/*
1.设计2个类,类之间的关系自拟(比如继承、组合)
1> 车
(1)属性
* 轮子数
* 速度
(2)方法
* 属性相应的set和get方法
2> 客车
(1)属性
* 轮子数
* 速度
* 座位数
(2)方法
* 属性相应的set和get方法
*/
// 客车 是一种 车,因此用继承关系
#import <Foundation/Foundation.h>
// 车
@interface Car : NSObject
{
int _wheels; // 轮子数
int _speed; // 速度
}
// 速度的getter和setter
- (int)speed;
- (void)setSpeed:(int)speed;
// 轮子数的getter和setter
- (int)wheels;
- (void)setWheels:(int)wheels;
@end
@implementation Car
// 速度的getter和setter
- (int)speed
{
return _speed;
}
- (void)setSpeed:(int)speed
{
_speed = speed;
}
// 轮子数的getter和setter
- (int)wheels
{
return _wheels;
}
- (void)setWheels:(int)wheels
{
_wheels = wheels;
}
@end
// 客车
@interface Bus : Car
{
int _seats; // 座位数
}
// 座位数的getter和setter
- (int)seats;
- (void)setSeats:(int)seats;
@end
@implementation Bus
// 座位数的getter和setter
- (int)seats
{
return _seats;
}
- (void)setSeats:(int)seats
{
_seats = seats;
}
@end
H:/Objective-C/OC_day2/19-类的设计-02-人.m
/*
2.设计2个类,类之间的关系自拟(比如继承、组合)
1> 身材数据
(1)属性
* 身高
* 体重
* 手长
* 脚长
(2)方法
* 属性相应的set和get方法
2> 人
(1)属性
* 年龄
* 身高
* 体重
* 手长
* 脚长
(2)方法
* 属性相应的set和get方法
*/
// 人 拥有一份 身材数据 ,所以用组合关系
// 身材数据
@interface BodyData : NSObject
{
int _height; // 身高
int _weight; //体重
int _handLength; // 手长
int _legLength; // 腿长
}
// 身高的getter和setter
- (int)height;
- (void)setHeight:(int)height;
// 体重的getter和setter
- (int)weight;
- (void)setWeight:(int)weight;
// 手长的getter和setter
- (int)handLength;
- (void)setHandLength:(int)handLength;
// 腿长的getter和setter
- (int)legLength;
- (void)setLegLength:(int)legLength;
@end
@implementation BodyData
// 身高的getter和setter
- (int)height
{
return _height;
}
- (void)setHeight:(int)height
{
_height = height;
}
// 体重的getter和setter
- (int)weight
{
return _weight;
}
- (void)setWeight:(int)weight
{
_weight = weight;
}
// 手长的getter和setter
- (int)handLength
{
return _handLength;
}
- (void)setHandLength:(int)handLength
{
_handLength = handLength;
}
// 腿长的getter和setter
- (int)legLength
{
return _legLength;
}
- (void)setLegLength:(int)legLength
{
_legLength = legLength;
}
@end
// 人
@interface Person : NSObject
{
int _age; // 年龄
BodyData *_bodyData; // 身材数据
}
// _age的setter和getter
- (void)setAge:(int)age;
- (int)age;
// _bodyData的setter和getter
- (void)setBodyData:(BodyData *)bodyData;
- (BodyData *)bodyData;
@end
//实现
@implementation Person
// _age的setter和getter
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
// _bodyData的setter和getter
- (void)setBodyData:(BodyData *)bodyData
{
_bodyData = bodyData;
}
- (BodyData *)bodyData
{
return _bodyData;
}
@end
int main()
{
Person *p = [Person new];
// 设置年龄
[p setAge:20];
// 设置身材数据
BodyData *b = [BodyData new];
[b setWeight:60];
[b setHeight:170];
[p setBodyData:b];
return 0;
}
H:/Objective-C/OC_day2/19-类的设计-03-人书学生.m
/*
3.设计3个类,类之间的关系自拟(比如继承、组合)
1> 人
(1)属性
* 姓名
* 年龄
(2)方法
* 属性相应的set和get方法
* 设计一个对象方法同时设置姓名和年龄
2> 书
(1)属性
* 书名
* 出版社名称
* 作者(包含姓名和年龄)
(2)方法
* 属性相应的set和get方法
3> 学生
* 姓名
* 年龄
* 学号
* 书(随身带着一本书)
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法-study:输出书名
*/
// 书 拥有一个 作者 --> 组合
// 学生 是一个 人 --> 继承
// 学生 拥有一本 书 --> 组合
#import <Foundation/Foundation.h>
//人
@interface Person : NSObject
{
NSString *_name; // 姓名
int _age; // 年龄
}
// 姓名的getter和setter
- (void)setName:(NSString *)name;
- (NSString *)name;
// 年龄的getter和setter
- (void)setAge:(int)age;
- (int)age;
// 同时设置姓名和年龄
- (void)setName:(NSString *)name andAge:(int)age;
@end
@implementation Person
// 姓名的getter和setter
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
// 年龄的getter和setter
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
// 同时设置姓名和年龄
- (void)setName:(NSString *)name andAge:(int)age
{
_name = name;
_age = age;
/*
[self setName:name];
[self setAge:age];
*/
}
@end
// 书
@interface Book : NSObject
{
NSString *_name; // 书名
NSString *_publisher; // 出版社名称
Person *_author; // 作者
}
// 书名的getter和setter
- (void)setName:(NSString *)name;
- (NSString *)name;
// 出版社名称的getter和setter
- (void)setPublisher:(NSString *)publisher;
- (NSString *)publisher;
// 作者的getter和setter
- (void)setAuthor:(Person *)author;
- (Person *)author;
@end
@implementation Book
// 书名的getter和setter
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
// 出版社名称的getter和setter
- (void)setPublisher:(NSString *)publisher
{
_publisher = publisher;
}
- (NSString *)publisher
{
return _publisher;
}
// 作者的getter和setter
- (void)setAuthor:(Person *)author
{
_author = author;
}
- (Person *)author
{
return _author;
}
@end
// 学生
@interface Student : Person
{
int _no; //学号
Book *_book; //书
}
// 学号的getter和setter
- (void)setNo:(int)no;
- (int)no;
// 书的getter和setter
- (void)setBook:(Book *)book;
- (Book *)book;
// 学习
- (void)study;
@end
@implementation Student
// 学号的getter和setter
- (void)setNo:(int)no
{
_no = no;
}
- (int)no
{
return _no;
}
// 书的getter和setter
- (void)setBook:(Book *)book
{
_book = book;
}
- (Book *)book
{
return _book;
}
// 学习
- (void)study
{
NSLog(@"现在学习的书是:%@", [_book name]);
}
@end
H:/Objective-C/OC_day2/19-类的设计-04-Car.m
/**
4.设计Car类
1> 属性
* 速度
2> 方法
* 属性相应的set和get方法
* 一个对象方法跟其他车子比较车速,返回速度差
* 一个类方法比较两辆车的车速,返回速度差
*/
#import <Foundation/Foundation.h>
// 车
@interface Car : NSObject
{
int _speed; // 速度
}
// 速度的getter和setter
- (void)setSpeed:(int)speed;
- (int)speed;
// 跟其他车子比较车速,返回速度差
- (int)compareSpeedWithOther:(Car *)car;
// 比较两辆车的车速,返回速度差
+ (int)compareSpeedBetweenCar1:(Car *)car1 andCar2:(Car *)car2;
@end
@implementation Car
// 速度的getter和setter
- (void)setSpeed:(int)speed
{
_speed = speed;
}
- (int)speed
{
return _speed;
}
// 跟其他车子比较车速,返回速度差
- (int)compareSpeedWithOther:(Car *)car
{
// 第1种思路
// return _speed - [car speed];
// 第2种思路
return [Car compareSpeedBetweenCar1:self andCar2:car];
}
// 比较两辆车的车速,返回速度差
+ (int)compareSpeedBetweenCar1:(Car *)car1 andCar2:(Car *)car2
{
return [car1 speed] - [car2 speed];
}
@end
H:/Objective-C/OC_day2/19-类的设计-05-点.m
/**
5.设计一个类Point2D,用来表示二维平面中某个点
1> 属性
* double x
* double y
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法同时设置x和y
* 设计一个对象方法计算跟其他点的距离
* 设计一个类方法计算两个点之间的距离
3> 提示
* C语言的math.h中有个函数:double pow(double n, double m); 计算n的m次方
* C语言的math.h中有个函数:double sqrt(double n); 计算根号n的值(对n进行开根)
*/
#import <Foundation/Foundation.h>
#import <math.h>
// 点
@interface Point2D : NSObject
{
double _x; // x值
double _y; // y值
}
// x值的getter和setter
- (void)setX:(double)x;
- (double)x;
// y值的getter和setter
- (void)setY:(double)y;
- (double)y;
// 同时设置x和y
- (void)setX:(double)x andY:(double)y;
// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other;
// 计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
@end
@implementation Point2D
// x值的getter和setter
- (void)setX:(double)x
{
_x = x;
}
- (double)x
{
return _x;
}
// y值的getter和setter
- (void)setY:(double)y
{
_y = y;
}
- (double)y
{
return _y;
}
// 同时设置x和y
- (void)setX:(double)x andY:(double)y
{
// 第1种思路
// _x = x;
// _y = y;
// 第2种思路
[self setX:x];
[self setY:y];
}
// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other
{
// 不要再傻乎乎算一遍了,直接调用类方法即可
return [Point2D distanceBetweenPoint1:self andPoint2:other];
}
// 计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
// 两点距离公式:( (x1-x2)的平方 + (y1-y2)的平方 )开根
// x1-x2
double xDelta = [p1 x] - [p2 x];
// (x1-x2)的平方
double xDeltaPingFang = pow(xDelta, 2);
// y1-y2
double yDelta = [p1 y] - [p2 y];
// (y1-y2)的平方
double yDeltaPingFang = pow(yDelta, 2);
return sqrt(xDeltaPingFang + yDeltaPingFang);
}
@end
int main()
{
Point2D *p1 = [Point2D new];
[p1 setX:10 andY:10];
Point2D *p2 = [Point2D new];
[p2 setX:13 andY:14];
double d1 = [p1 distanceWithOther:p2];
double d2 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];
NSLog(@"d1=%f, d2=%f", d1, d2);
return 0;
}
H:/Objective-C/OC_day2/19-类的设计-06-圆.m
/**
6.设计一个类Circle,用来表示二维平面中的圆
1> 属性
* double radius (半径)
* Point2D *point (圆心)
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)
* 设计一个类方法判断两个圆是否相交(重叠返回YES,否则返回NO)
*/
#import <Foundation/Foundation.h>
#import <math.h>
// 点
@interface Point2D : NSObject
{
double _x; // x值
double _y; // y值
}
// x值的getter和setter
- (void)setX:(double)x;
- (double)x;
// y值的getter和setter
- (void)setY:(double)y;
- (double)y;
// 同时设置x和y
- (void)setX:(double)x andY:(double)y;
// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other;
// 计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2;
@end
@implementation Point2D
// x值的getter和setter
- (void)setX:(double)x
{
_x = x;
}
- (double)x
{
return _x;
}
// y值的getter和setter
- (void)setY:(double)y
{
_y = y;
}
- (double)y
{
return _y;
}
// 同时设置x和y
- (void)setX:(double)x andY:(double)y
{
// 第1种思路
// _x = x;
// _y = y;
// 第2种思路
[self setX:x];
[self setY:y];
}
// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other
{
// 不要再傻乎乎算一遍了,直接调用类方法即可
return [Point2D distanceBetweenPoint1:self andPoint2:other];
}
// 计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPoint2:(Point2D *)p2
{
// 两点距离公式:( (x1-x2)的平方 + (y1-y2)的平方 )开根
// x1-x2
double xDelta = [p1 x] - [p2 x];
// (x1-x2)的平方
double xDeltaPingFang = pow(xDelta, 2);
// y1-y2
double yDelta = [p1 y] - [p2 y];
// (y1-y2)的平方
double yDeltaPingFang = pow(yDelta, 2);
return sqrt(xDeltaPingFang + yDeltaPingFang);
}
@end
// 圆
@interface Circle : NSObject
{
double _radius; // 半径
Point2D *_point; // 圆心
}
// 半径的getter和setter
- (void)setRadius:(double)radius;
- (double)radius;
// 圆心的getter和setter
- (void)setPoint:(Point2D *)point;
- (Point2D *)point;
// 跟其他圆是否重叠(重叠返回YES,否则返回NO)
- (BOOL)isInteractWithOther:(Circle *)other;
// 判断两个圆是否重叠(重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenCircle1:(Circle *)circle1 andCircle2:(Circle *)circle2;
@end
@implementation Circle
// 半径的getter和setter
- (void)setRadius:(double)radius
{
_radius = radius;
}
- (double)radius
{
return _radius;
}
// 圆心的getter和setter
- (void)setPoint:(Point2D *)point
{
_point = point;
}
- (Point2D *)point
{
return _point;
}
// 跟其他圆是否重叠(重叠返回YES,否则返回NO)
- (BOOL)isInteractWithOther:(Circle *)other
{
return [Circle isInteractBetweenCircle1:self andCircle2:other];
}
// 判断两个圆是否重叠(重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenCircle1:(Circle *)circle1 andCircle2:(Circle *)circle2
{
// 如果两个圆心的距离 >= 两个圆的半径和,就不重叠
// 如果两个圆心的距离 < 两个圆的半径和,就重叠
// 两个圆心
Point2D *point1 = [circle1 point];
Point2D *point2 = [circle2 point];
// 两个圆心的距离
double distance = [point1 distanceWithOther:point2];
// 半径和
double radiusSum = [circle1 radius] + [circle2 radius];
return distance < radiusSum;
}
@end
int main()
{
Circle *c1 = [Circle new];
// 设置半径
[c1 setRadius:2];
// 设置圆心
Point2D *p1 = [Point2D new];
[p1 setX:10 andY:10];
[c1 setPoint:p1];
Circle *c2 = [Circle new];
// 设置半径
[c2 setRadius:2];
// 设置圆心
Point2D *p2 = [Point2D new];
[p2 setX:13 andY:14];
[c2 setPoint:p2];
// 圆心距离是5 半径和是4 所以不重叠
BOOL b1 = [c1 isInteractWithOther:c2];
BOOL b2 = [Circle isInteractBetweenCircle1:c1 andCircle2:c2];
NSLog(@"%d %d", b1, b2);
return 0;
}