OC面向对象 点语法 继承 重写练习
2014-05-31 08:14
344 查看
父类定义
@interface Vehicle :
NSObject
{
NSString* colour;
int Speed;
}
@property
NSString* colour;
@property int Speed;
-(void)run;
-(void)drive:(NSString *)name;
@end
父类实现方法
@implementation Vehicle
@synthesize colour;
@synthesize Speed;
-(void)run{
NSLog(@"the Vehicle is ruuning");
}
-(void)drive:(NSString *)name {
NSLog(@"%@在驾驶车辆",name);
}
@end
子类car 自有属性定义
@interface Car : Vehicle
{
int numberofwheels;
}
@property
int numberofwheels;
@end
子类car 方法重写
@implementation Car
@synthesize numberofwheels;
-(void)run{
NSLog(@"the %@ car is running at the speed of %d km/h",colour,Speed);
NSLog(@"the %@ car has %d wheels",colour,numberofwheels);
}
-(void)drive:(NSString *)name {
NSLog(@"驾驶员%@在驾驶小汽车",name);
}
@end
子类truck 自有属性定义
@interface Truck : Vehicle
{
int maxweight;}
@property
int maxweight;
-(void)unload;
@end
子类truck 自有方法重写
@implementation Truck
@synthesize maxweight;
-(void)unload{
NSLog(@"卡车卸货%d吨",maxweight);
}
-(void)run{
NSLog(@"the %@ truck is running at the speed of %d km/h",colour,Speed);
}
-(void)drive:(NSString *) name{
NSLog(@"驾驶员%@在驾驶卡车",name);
}
@end
驾驶员类
@interface Person :
Vehicle
{NSString * name;}
@property
NSString * name;
-(void)dodrive:(Vehicle *) vehicle and: (NSString *) name;
@end
驾驶方法实现
@implementation Person
@synthesize name;
-(void)dodrive:(Vehicle *) vehicle and: (NSString *) name
{
[vehicle
drive:name];
}
@end
@autoreleasepool {
Car *car1=[Car
alloc];
car1.colour =@"red";
car1.Speed =
100;
car1.numberofwheels =
4;
[car1
run];
Truck *truck1 =[Truck
alloc];
truck1.colour =@"black";
truck1.Speed =
10;
truck1.maxweight =4;
[truck1
unload];
[truck1
run];
Person *person1 = [Person
alloc];
person1.name =@"小李";
[person1
dodrive:car1 and:person1.name];
Person *person2 = [Person
alloc];
person2.name =@"小张";
[person2
dodrive:truck1 and:person2.name];
}
return
0;
输出结果
2014-05-31 08:07:03.240 OC02[1066:303] the red car is running at the speed of 100 km/h
2014-05-31 08:07:03.242 OC02[1066:303] the red car has 4 wheels
2014-05-31 08:07:03.243 OC02[1066:303]
卡车卸货4吨
2014-05-31 08:07:03.243 OC02[1066:303] the black truck is running at the speed of 10 km/h
2014-05-31 08:07:03.243 OC02[1066:303]
驾驶员小李在驾驶小汽车
2014-05-31 08:07:03.244 OC02[1066:303]
驾驶员小张在驾驶卡车
@interface Vehicle :
NSObject
{
NSString* colour;
int Speed;
}
@property
NSString* colour;
@property int Speed;
-(void)run;
-(void)drive:(NSString *)name;
@end
父类实现方法
@implementation Vehicle
@synthesize colour;
@synthesize Speed;
-(void)run{
NSLog(@"the Vehicle is ruuning");
}
-(void)drive:(NSString *)name {
NSLog(@"%@在驾驶车辆",name);
}
@end
子类car 自有属性定义
@interface Car : Vehicle
{
int numberofwheels;
}
@property
int numberofwheels;
@end
子类car 方法重写
@implementation Car
@synthesize numberofwheels;
-(void)run{
NSLog(@"the %@ car is running at the speed of %d km/h",colour,Speed);
NSLog(@"the %@ car has %d wheels",colour,numberofwheels);
}
-(void)drive:(NSString *)name {
NSLog(@"驾驶员%@在驾驶小汽车",name);
}
@end
子类truck 自有属性定义
@interface Truck : Vehicle
{
int maxweight;}
@property
int maxweight;
-(void)unload;
@end
子类truck 自有方法重写
@implementation Truck
@synthesize maxweight;
-(void)unload{
NSLog(@"卡车卸货%d吨",maxweight);
}
-(void)run{
NSLog(@"the %@ truck is running at the speed of %d km/h",colour,Speed);
}
-(void)drive:(NSString *) name{
NSLog(@"驾驶员%@在驾驶卡车",name);
}
@end
驾驶员类
@interface Person :
Vehicle
{NSString * name;}
@property
NSString * name;
-(void)dodrive:(Vehicle *) vehicle and: (NSString *) name;
@end
驾驶方法实现
@implementation Person
@synthesize name;
-(void)dodrive:(Vehicle *) vehicle and: (NSString *) name
{
[vehicle
drive:name];
}
@end
@autoreleasepool {
Car *car1=[Car
alloc];
car1.colour =@"red";
car1.Speed =
100;
car1.numberofwheels =
4;
[car1
run];
Truck *truck1 =[Truck
alloc];
truck1.colour =@"black";
truck1.Speed =
10;
truck1.maxweight =4;
[truck1
unload];
[truck1
run];
Person *person1 = [Person
alloc];
person1.name =@"小李";
[person1
dodrive:car1 and:person1.name];
Person *person2 = [Person
alloc];
person2.name =@"小张";
[person2
dodrive:truck1 and:person2.name];
}
return
0;
输出结果
2014-05-31 08:07:03.240 OC02[1066:303] the red car is running at the speed of 100 km/h
2014-05-31 08:07:03.242 OC02[1066:303] the red car has 4 wheels
2014-05-31 08:07:03.243 OC02[1066:303]
卡车卸货4吨
2014-05-31 08:07:03.243 OC02[1066:303] the black truck is running at the speed of 10 km/h
2014-05-31 08:07:03.243 OC02[1066:303]
驾驶员小李在驾驶小汽车
2014-05-31 08:07:03.244 OC02[1066:303]
驾驶员小张在驾驶卡车
相关文章推荐
- OC学习笔记02-OC中的面向对象语法表现 - 封装 继承与多态
- OC继承课后练习_打僵尸
- IOS学习之OC面向对象—继承
- [java语法]基本语法(6) 面向对象--类的继承
- 黑马程序员_OC语言之面向对象的三大特性(封装、继承、多态)
- OC面向对象—继承
- 黑马程序员_iOS_OC_继承_方法重写与覆盖
- IOS之OC面向对象—继承
- Day2 面向对象(封装·继承·多态·重载·重写)
- OC语言--继承、点语法、类别
- 黑马程序员_iOS开发之OC之面向对象之多文件操作、点语法、成员变量的作用域和关键字@property与@synthesize
- 黑马程序员_iOS开发之OC之面向对象之id语法、构造方法、@category分类、类本质、description方法和SEL数据类型
- OC面向对象—继承
- Java面向对象5——类的继承与权限控制,重写与super
- 黑马程序员——OC语言基础:面向对象三大特性,封装、继承、多态
- 【Go入门教程7】面向对象(method、指针作为receiver、method继承、method重写)
- OC面向对象—继承
- 转 python语法学习面向对象之继承
- 我的OC练习(三)- 类的继承实践练习
- Java基础语法(五)----面向对象(2) 继承