这个问题在这里已经有答案了:
多态性继承不重写基类方法(3个答案)
什么是对象切片(18个答案)
上个月关门了。
我正在处理一个将java转换为c的赋值,我已经完成了所有的工作,但是在c版本中,重写函数不能正常工作。。。
以下是java版本:
abstract class ForceBeing {
private int power_level;
private String name;
private boolean evil;
protected ForceBeing(int level) {
power_level = level;
}
protected void setPowerLevel(int level) {
power_level = level;
}
public int getPowerLevel() {
return power_level;
}
protected void setName(String characterName) {
name = characterName;
}
public String getName() {
return name;
}
public void setEvil() {
evil = true;
}
public void setGood() {
evil = false;
}
}
class Jedi extends ForceBeing {
public Jedi(String jediName, int level) {
super(level);
setName(jediName);
setGood();
}
public int getPowerLevel() {
return super.getPowerLevel() + 1;
}
}
class Sith extends ForceBeing {
public Sith(String sithName, int level) {
super(level);
setName(sithName);
setEvil();
}
public int getPowerLevel() {
return super.getPowerLevel() - 1;
}
public String getName() {
return "Sith Lord: " + super.getName();
}
}
public class StarWars {
public static String getWinner(ForceBeing player1, ForceBeing player2) {
return player1.getPowerLevel() > player2.getPowerLevel() ? player1.getName() : player2.getName();
}
public static void printWinner(String winner) {
System.out.println("The winner is: " + winner);
}
public static void main(String[] args) {
Sith darthVader = new Sith("Darth Vader",100);
Jedi lukeSkyWalker = new Jedi("Luke Skywalker",99);
printWinner(getWinner(darthVader, lukeSkyWalker));
}
}
输出:
The winner is: Luke Skywalker
就像它应该的那样。
这是我转换的c++:
# include <iostream>
# include <string>
using namespace std;
class ForceBeing
{
private:
int power_level;
string name;
bool evil;
protected:
ForceBeing(int level)
{
power_level = level;
}
void setPowerLevel(int level)
{
power_level = level;
}
void setName(string characterName)
{
name = characterName;
}
public:
int getPowerLevel()
{
return power_level;
}
string getName()
{
return name;
}
void setEvil()
{
evil = true;
}
void setGood()
{
evil = false;
}
};
class Jedi : public ForceBeing
{
public:
Jedi(string jediName, int level) : ForceBeing(level)
{
setName(jediName);
setGood();
}
int getPowerLevel()
{
return ForceBeing::getPowerLevel() + 1;
}
};
class Sith : public ForceBeing
{
public:
Sith(string sithName, int level) : ForceBeing(level)
{
setName(sithName);
setEvil();
}
int getPowerLevel()
{
return ForceBeing::getPowerLevel() - 1;
}
string getName()
{
return "Sith Lord: " + ForceBeing::getName();
}
};
static string getWinner(ForceBeing player1, ForceBeing player2)
{
// return player1.getPowerLevel() > player2.getPowerLevel() ? player1.getName() : player2.getName();
cout << "Player 1 Name: " << player1.getName() << " PowerLevel: " << player1.getPowerLevel() << endl;
cout << "Player 2 Name: " << player2.getName() << " PowerLevel: " << player2.getPowerLevel() << endl;
if (player1.getPowerLevel() > player2.getPowerLevel())
{
return player1.getName();
}
else
{
return player2.getName();
}
};
static void printWinner(string winner)
{
cout << "The winner is: " << winner << endl;
};
int main()
{
Sith darthVader("Darth Vader", 100);
Jedi lukeSkyWalker("Luke Skywalker", 99);
printWinner(getWinner(lukeSkyWalker, darthVader));
return 0;
}
输出:
Player 1 Name: Luke Skywalker PowerLevel: 99
Player 2 Name: Darth Vader PowerLevel: 100
The winner is: Darth Vader
当我期望它输出:
Player 1 Name: Luke Skywalker PowerLevel: 100
Player 2 Name: Sith Lord: Darth Vader PowerLevel: 99
The winner is: Luke Skywalker
我盯着这个看了一个小时都不知道。。。有人有更好的线索吗?提前感谢您的帮助!
暂无答案!
目前还没有任何答案,快来回答吧!