面向对象三大特性
来源:互联网 发布:网络用语古是什么意思 编辑:程序博客网 时间:2024/05/24 07:21
@interface Student : NSObject
{
//成员变量不能使用@public
int _age;//年龄
int _no;//学号,只允许外界访问我的no,不允许外界修改我的no,所以只提供get方法
}
- (void)setAge:(int)newAge;
- (int)age;
- (int)no;
- (void)study;
@end
@implementation Student
- (void)setAge:(int)age
{
//对传进来才参数进行过滤
if(age<=0)
{
age=1;
}
_age=age;
}
- (int)age
{
return _age;
}
- (int)no
{
return _no;
}
- (void)study
{
NSLog(@"%d岁的学生在学习”,_age);
}
@end
int main()
{
Student *stu = [Student new];
[stu setAge:10];
NSLog(@"学生的年龄是%d岁",[stu age]);
return 0;
}
封装的细节:
成员变量的命名规范:一定要以_下划线开头
作用:
1.让成员变量和get方法的名称区分开来
2.可以跟局部变量区分开,一看到下划线开头的变量,一般都是成员变量
对象方法:
1.对象方法只能以减号 - 开头
2.对象方法只能由对象来进行调用
3.对象方法中能访问当前对象的成员变量(实例变量)
类方法:
1.类方法以+号开头
2.只能由类名来进行调用
3.类方法不能访问成员变量(实例变量)
类方法的好处和使用场合:
1.不依赖于对象,执行效率高
2.能用类的方法尽量使用类的方法,节省内存
3.场合,当方法内部不需要使用到成员变量的时候,就可以改为使用类方法
#import <Foundation/Foundation.h>
/*动物类的声明*/
@interface Animal : NSObject
{
int _age;
double _weight;
}
- (void)setAge:(int)age;
- (int)age;
- (void)setWeight:(double)weight;
- (double)weight;
@end
/*动物类的实现*/
@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里面的所有成员变量和方法*/
@interface Dog : Animal
@end
@implementation Dog
@end
/*Car类的声明和实现,继承于Animal,相当于拥有了Animal里面的所有成员变量和方法*/
@interface Car : Animal
@end
@implementation Car
@end
int main()
{
Dog *d = [Dog new];
[d setAge:10];
NSLog(@"age=%d",[d age]);
return 0;
}
重写:子类重新实现父类中的某个方法,覆盖父类以前的做法
#import<Foundation/Foundation.h>
@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(Animal *a)
{
[a eat];
}
int main()
{
Animal *aa = [Dog new];
Dog *dd=(Dog *)aa;//将aa强制转换为Dog类型
[dd run];//这里会出现一个警告,因为Animal没有这个对象方法,需要将Animal转换为Dog类型
[aa eat];//调用方法的时候会检测对象的真实形态,在调用对应的方法
return 0;
}
多文件组合开发:
每个类有两个文件,.h文件是包含这个类的所有声明,.m文件是包含这个类的所有视线,每个类的.m文件必须包含自己的.h文件,如果两个类是组合关系,组合类的.h文件中需要包含被组合的类的.h文件
在main方法中需要包含所有类的声明(.h)文件
例如:
Point2D.h:
#import<Foundation/Foundation.h>
//Point2D类的声明
@interface Point2D : NSObject
{
double _x;// 横坐标 x属性
double _y;// 纵坐标 y属性
}
// x的geter和seter方法
- (void)setX : (double)x;
- (double)x;
// y的geter和seter方法
- (void)setY : (double)Y;
- (double)y;
// 设计一个对象方法同时设置x和y
- (void)setX : (double)x andsetY :(double)y;
// 设置一个对象方法计算和其他点的距离
- (double)getdistance:(Point2D *)point2d;
// 设置一个类方法计算两点之间的距离
+ (double)getdistance:(Point2D *)point2d andotherpoint2d : (Point2D *)otherpoint2d;
@end
Point2D.m:
#import "Point2D.h"
#import<math.h>
@implementation Point2D
// x的geter和seter方法
- (void)setX : (double)x
{
_x = x;
}
- (double)x
{
return _x;
}
// y的geter和seter方法
- (void)setY : (double)y
{
_y = y;
}
- (double)y
{
return _y;
}
// 设计一个对象方法同时设置x和y
- (void)setX : (double)x andsetY :(double)y
{
[self setX : x];
[self setY : y];
}
// 设置一个对象方法计算和其他点的距离
- (double)getdistance:(Point2D *)point2d
{
// 先计算两点x的距离
double width=[self x]-[point2d x];
// 在计算两点y的距离
double height=[self y]-[point2d y];
// 通过勾股定理算出两点的距离
return sqrt(pow(width,2)+pow(height,2));
}
// 设置一个类方法计算两点之间的距离
+ (double)getdistance:(Point2D *)point2d andotherpoint2d : (Point2D *)otherpoint2d;
{
return [point2d getdistance : otherpoint2d];
}
@end
Circle.h:
#import<Foundation/Foundation.h>
#import "Point2D.h"
/**
6.设计一个类Circle,用来表示二维平面中的圆
1> 属性
* double radius (半径)
* Point2D *point (圆心)
2> 方法
* 属性相应的set和get方法
* 设计一个对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)
* 设计一个类方法判断两个圆是否相交(重叠返回YES,否则返回NO)
*/
@interface Circle : NSObject
{
double _radius; // 半径
Point2D *_point; // 员心
}
// 半径的geter和seter方法
- (void)setRadius : (double)radius;
- (double)radius;
//圆心的geter和seter方法
- (void)setPoint : (Point2D *)point;
- (Point2D *)point;
//设计一个对象方法判断跟其他圆是否相交(重叠返回YES,否则返回NO)判断对象是否一般以is开头
- (BOOL)isInteractWithOther: (Circle *)other;
//设计一个类方法判断两个圆是否相交(重叠返回YES,否则返回NO)
+ (BOOL)isInteractBetweenCircle1 : (Circle *)circle1 andCircle2 : (Circle *)circle2;
@end
Circle.m:
#import "Circle.h"
@implementation Circle
// 半径的geter和seter方法
- (void)setRadius : (double)radius
{
_radius = radius;
}
- (double)radius
{
return _radius;
}
//圆心的geter和seter方法
- (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 getdistance : point2];
NSLog(@"%f",distance);
// 半径和
double radiusSum = [circle1 radius] + [circle2 radius];
return distance < radiusSum;
}
@end
#import<Foundation/Foundation.h>
#import "Point2D.h"
#import "Circle.h"
int main()
{
Point2D *p=[Point2D new];
[p setX : 15 andsetY : 15];
Point2D *p2=[Point2D new];
[p2 setX : 15 andsetY : 15];
double width = [p getdistance : p2];
double width2=[Point2D getdistance:p andotherpoint2d : p2];
NSLog(@"点之间的距离%f,%f",width,width2);
Circle *c=[Circle new];
[c setPoint : p];
[c setRadius : 5];
Circle *c2=[Circle new];
[c2 setPoint : p2];
[c2 setRadius : 2];
BOOL b = [c isInteractWithOther : c2];
NSLog(@"%d",b);
}
- 面向对象三大特性
- 面向对象-三大特性
- 面向对象三大特性
- 面向对象三大特性
- 面向对象三大特性
- 面向对象三大特性
- 面向对象三大特性
- 面向对象三大特性
- 面向对象三大特性
- 面向对象三大特性
- 面向对象三大特性
- 面向对象的三大特性
- 面向对象的三大特性
- 详解面向对象的三大特性
- 面向对象三大特性分析
- 面向对象三大特性之多态
- 面向对象的三大特性
- 面向对象的三大特性
- 斐波那契数列
- 如何让ubuntu开机进入纯命令模式
- Linux C判断两个IPv6地址是否相等的方法
- 手机版同步html几点注意使用
- Java NIO 同步非阻塞Socket DEMO
- 面向对象三大特性
- 在Ubuntu上安装Wine
- CodeForces 630 E. A rectangle(水~)
- 【Linux 基础篇】之常用命令
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(离散化+线段树)
- C++ Primer阅读心得(第十六章)
- HDOJ 5630Rikka with Chess(游戏)
- [LeetCode] Same Tree
- Struts2 路径问题