–在包bzu.aa中定义一个交通工具类(Vehicle):
属性——载客量(capacity)
方法
无参构造方法(给capacity初始化值为2,并输出“执行交通工具类的无参构造方法。”)
有参构造方法(传参给capacity初始化,并输出“执行交通工具的有参构造方法。”)
capacity的set、get方法
print方法:输出capacity
–在包bzu.aa中定义一个汽车类(Car)继承交通工具类:
属性——speed
方法
无参构造方法(给speed初始化值为0,并输出“执行汽车类的无参构造方法。”)
有参构造方法(用super关键字调用父类的有参构造方法,传参给speed初始化,并输出“执行汽车类的有参构造方法。”)
加速(speedup):speed+10并返回speed;
减速(speeddown):speed-15并返回speed;
重写print方法:输出speed和capacity。
–在包bzu.bb中定义一个final的公交车类(Bus),继承汽车类:
属性——载客量(capacity)<变量隐藏>
方法
无参构造方法(给capacity初始化值为20,并输出“执行公交车类的无参构造方法。”)
有参构造方法(用super关键字调用父类的有参构造方法,传参给capacity初始化,并输出“执行公交车类的有参构造方法。”)
重写print方法:输出speed、 capacity及父类的capacity。
–在包bzu.bb中编写一个主类Test:
主函数
调用无参构造方法创建一个Car的对象car;调用加速方法将速度加至50,调用print方法;调用减速方法,将速度减至20,调用print方法。
调用有参构造方法创建一个Bus的对象bus;调用print方法。
OC:Bus定义capacity覆盖父类变量的时候会提示重复定义,改为了_capacity,文件中的capacity来自父类。
Vehicle.h文件
#import <Foundation/Foundation.h>
@interface Vehicle : NSObject
{
int capacity;
}
-(id) init;
-(instancetype) initWithCapacity:(int)newCapacity;
-(void) setCapacity:(int)newCapacity;
-(int) capacity;
-(void) print;
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
Vehicle.m文件
#import "Vehicle.h"
@implementation Vehicle
-(id) init{
if(self = [super init]){
NSLog(@"执行交通工具类的无参构造方法");
}
return self;
}
-(instancetype) initWithCapacity:(int)newCapacity{
self = [super init];
if (self) {
capacity = newCapacity;
NSLog(@"执行交通工具的有参构造方法");
}
return self;
}
-(void) setCapacity:(int)newCapacity{
capacity = newCapacity;
}
-(int) capacity{
return capacity;
}
-(void) print{
NSLog(@"capacity=%d",capacity);
}
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
Car.h文件
#import "Vehicle.h"
@interface Car : Vehicle
{
int speed;
}
-(id) init;
-(instancetype) initWithSpeed:(int)newSpeed andCapacity:(int)newCapacity;
-(NSString *) speedUp;
-(NSString *) speedDown;
-(int) speed;
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
Car.m文件
#import "Car.h"
@implementation Car
-(id) init{
if (self = [super init]) {
speed = 0;
NSLog(@"执行汽车类的无参构造方法");
}
return self;
}
-(instancetype) initWithSpeed:(int)newSpeed andCapacity:(int)newCapacity{
if (self = [super initWithCapacity:newCapacity]) {
speed = newSpeed;
NSLog(@"执行汽车类的有参构造方法");
}
return self;
}
-(NSString *) speedUp{
speed += 10;
NSString *newString = [NSString stringWithFormat:@"当前速度=%d",speed];
return newString;
}
-(NSString *) speedDown{
speed -= 15;
NSString *newString = [NSString stringWithFormat:@"当前速度=%d",speed];
return newString;
}
-(int) speed{
return speed;
}
-(void) print{
NSLog(@"speed=%d,capacity=%d",speed,capacity);
}
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
Bus.h文件
#import "Car.h"
const @interface Bus : Car
{
int _capacity;
}
-(id) init;
-(instancetype) initWithCapacity:(int)newCapacity;
-(void) print;
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Bus.m文件
#import "Bus.h"
@implementation Bus
-(id) init{
if (self = [super init]) {
_capacity = 20;
NSLog(@"执行公交车类的无参构造方法");
}
return self;
}
-(instancetype) initWithCapacity:(int)newCapacity{
if (self = [super initWithSpeed:200 andCapacity:25]) {
_capacity = newCapacity;
NSLog(@"执行公交车类的有参构造方法");
}
return self;
}
-(void) print{
NSLog(@"speed=%d,capacity=%d,_capacity=%d",speed,capacity,_capacity);
}
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
main.m文件
#import <Foundation/Foundation.h>
#import "Vehicle.h"
#import "Car.h"
#import "Bus.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car *car = [[Car alloc] init];
while([car speed]!=50){
NSLog(@"%@",[car speedUp]);
}
while ([car speed]!=20) {
NSLog(@"%@",[car speedDown]);
}
Bus *bus = [[Bus alloc] initWithCapacity:25];
[bus print];
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
运行截图:
Java:还是老本行写着简单舒服,看起来也清晰
首先包结构:
Vehicle.java文件
package bzu.aa;
public class Vehicle {
public int capacity;
public Vehicle() {
capacity = 2;
System.out.println("执行交通工具类的无参构造方法。");
}
public Vehicle(int capacity) {
this.capacity = capacity;
System.out.println("执行交通工具类的无参构造方法。");
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public void print(){
System.out.println("capacity=="+capacity);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
Car.java文件
package bzu.aa;
public class Car extends Vehicle {
public int speed;
public Car() {
speed = 0;
System.out.println("执行汽车类的无参构造方法。");
}
public Car(int speed,int capacity) {
super(capacity);
this.speed = speed;
System.out.println("执行汽车类的有参构造方法。");
}
public void speedUp(){
speed += 10;
System.out.println("speed=="+speed);
}
public void speedDown(){
speed -= 15;
System.out.println("speed=="+speed);
}
public void print() {
System.out.print("speed=="+speed+" ");
System.out.println("capacity=="+capacity);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
Bus.java文件
package bzu.bb;
import bzu.aa.Car;
final class Bus extends Car {
int capacity;
public Bus() {
capacity = 20;
System.out.println("执行公交车类的无参构造方法。");
}
public Bus(int speed, int capacity) {
super(speed, capacity);
this.capacity = capacity;
System.out.println("执行公交车类的有参构造方法。");
}
public void print() {
System.out.println("speed=="+speed);
System.out.println("父类capacity=="+super.getCapacity());
System.out.println("子类capacity=="+capacity);
System.out.println("父类capacity=="+super.capacity);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
Test.java文件
package bzu.bb;
import bzu.aa.Car;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Car car = new Car();
while (car.speed!=50) {
car.speedUp();
}
while (car.speed!=20) {
car.speedDown();
}
car.print();
Bus bus = new Bus(200,25);
bus.print();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
运行截图: