繼承練習0322


作業內容是將下列C++程式碼改成Java的。

C++
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>

using namespace std;

class Object{
protected:
int dmg;
public:
Object(){
dmg = 0;
}
Object(int x){
dmg = x;
}
void Action(){
cout<<"Do nothing......"<<endl;
}
void MoveTo(int x, int y){
cout<<"Walking to location("<<x<<","<<y<<")"<<endl;
}
void Attack(int dmg){
cout<<"Attack......."<<endl;
cout<<"Damage: "<<dmg<<endl;
}
};

class NPC: public Object{

};

class Player: public Object{
private:
int magicDmg;
public:
Player(){
Object(10);
magicDmg = 10;
cout<<"dmg = "<<dmg<<endl;
cout<<"magicDmg = "<<magicDmg<<endl;
}
void Action(){
MoveTo(10,20);
}
void Attack(){
cout<<"Casting magic......"<<endl;
Object::Attack(dmg+magicDmg);
}
};

class Monster: public Object{
public:
Monster(){
Object(20);
cout<<"dmg = "<<dmg<<endl;
}
void MoveTo(int x, int y){
cout<<"Flying to location("<<x<<","<<y<<")"<<endl;
}
void Action(){
MoveTo(10,20);
Attack(dmg);
}
};

class Building: public Object{
};

int main(){

cout<<"NPC: "<<endl;
NPC npc;
npc.Action();

cout<<"---------------------------"<<endl;
cout<<"Player: "<<endl;
Player player;
player.Action();

cout<<"---------------------------"<<endl;
cout<<"Monster: "<<endl;
Monster monster;
monster.Action();

cout<<"---------------------------"<<endl;
cout<<"Player: fight back...."<<endl;
player.Attack();

cout<<"---------------------------"<<endl;
cout<<"Building: "<<endl;
Building building;
building.Action();


return 0;
}

其實這兩種語法感覺上並沒有差太多,大部分時間都花在把 cout 改成 System.out.print(汗,需要注意的是 Java 的繼承是用 extends,還有在子類別呼叫父類別建構子時要使用一個特別的函數 super(),不能直接用父類別名稱呼叫,修改完成的程式碼如下。

Java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package homework0322;

class Object{
protected int dmg;
public Object(){
dmg = 0;
}
public Object(int x){
dmg = x;
}
public void Action(){
System.out.print("Do nothing......\n");
}
public void MoveTo(int x, int y){
System.out.print("Walking to location(" + x + "," + y + ")\n");
}
public static void Attack(int dmg){
System.out.print("Attack.......\n");
System.out.print("Damage: " + dmg + "\n");
}
};
class NPC extends Object{

};

class Player extends Object{
private int magicDmg;
public Player(){
super(10);
magicDmg = 10;
System.out.print("dmg = " + dmg + "\n");
System.out.print("magicDmg = " + magicDmg + "\n");
}
public void Action(){
MoveTo(10,20);
}
public void Attack(){
System.out.print("Casting magic......\n");
Object.Attack(dmg+magicDmg);
}
};
class Monster extends Object{
public Monster(){
super(20);
System.out.print("dmg = " + dmg + "\n");
}
public void MoveTo(int x, int y){
System.out.print("Flying to location(" + x + "," + y + ")\n");
}
public void Action(){
MoveTo(10,20);
Attack(dmg);
}
};
class Building extends Object{
};

public class HW0322 {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("NPC: \n");
NPC npc = new NPC();
npc.Action();
System.out.print("---------------------------\n");
System.out.print("Player: \n");
Player player = new Player();
player.Action();

System.out.print("---------------------------\n");
System.out.print("Monster: \n");
Monster monster = new Monster();
monster.Action();

System.out.print("---------------------------\n");
System.out.print("Player: fight back....\n");
player.Attack();

System.out.print("---------------------------\n");
System.out.print("Building: \n");
Building building = new Building();
building.Action();

}

}

結果