您的位置:首页 > 其它

第七周作业

2017-11-08 13:22 253 查看
题目: 
课堂练习: 

–在包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文件

//
//  Vehicle.h
//
//  Created by Zb_tjw on 2017/10/11.
//  Copyright © 2017年 Zb_tjw. All rights reserved.
//

#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文件

//
//  Vehicle.m
//
//  Created by Zb_tjw on 2017/10/11.
//  Copyright © 2017年 Zb_tjw. All rights reserved.
//

#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文件

//
//  Car.h
//
//  Created by Zb_tjw on 2017/10/11.
//  Copyright © 2017年 Zb_tjw. All rights reserved.
//

#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文件

//
//  Car.m
//
//  Created by Zb_tjw on 2017/10/11.
//  Copyright © 2017年 Zb_tjw. All rights reserved.
//

#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文件

//
//  Bus.h
//
//  Created by Zb_tjw on 2017/10/11.
//  Copyright © 2017年 Zb_tjw. All rights reserved.
//

#import "Car.h"

const @interface Bus : Car
{
//    int capacity;//Duplicate member 'capacity'
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文件

//
//  Bus.m
//
//  Created by Zb_tjw on 2017/10/11.
//  Copyright © 2017年 Zb_tjw. All rights reserved.
//

#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文件

//
//  main.m
//
//  Created by Zb_tjw on 2017/10/11.
//  Copyright © 2017年 Zb_tjw. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Vehicle.h"
#import "Car.h"
#import "Bus.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
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() {
// TODO Auto-generated constructor stub
speed = 0;
System.out.println("执行汽车类的无参构造方法。");
}

public Car(int speed,int capacity) {
super(capacity);
this.speed = speed;
// TODO Auto-generated constructor stub
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() {
// TODO Auto-generated method stub
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() {
// TODO Auto-generated constructor stub
capacity = 20;
System.out.println("执行公交车类的无参构造方法。");
}

public Bus(int speed, int capacity) {
super(speed, capacity);
this.capacity = capacity;
System.out.println("执行公交车类的有参构造方法。");
// TODO Auto-generated constructor stub
}

public void print() {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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
运行截图: 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: