我需要将此程序转换为java。我可以处理除析构函数以外的所有事情。现在我已经阅读了有关 GC(垃圾收集器)及其不可靠性的全部内容。我在想,如果没有办法或其他方法,为什么要为我放置一个析构函数来转换。
#include<iostream>
using namespace std;
class Timer{
public:
Timer();
~Timer();
};
Timer::Timer(){
cout<<"Install a timer"<<endl;
}
Timer::~Timer(){
cout<<"Demolition of timer"<<endl;
}
class Bomb: public Timer{
public:
Bomb();
~Bomb();
};
Bomb::Bomb(){
cout<<"Install a timer in bomb"<<endl;
}
Bomb::~Bomb(){
cout<<"Bomb explode..."<<endl;
}
int main()
{
Timer* aTimer = new Timer;
Bomb* aBomb = new Bomb;
delete aTimer;
delete aBomb;
system("pause");
return 0;
}
到目前为止,我想出的是使用 Eclipse 的这些东西......
public class mainting{
public static void main(String test[])
{
timer atimer = new timer();
bomb abomb = new bomb();
}
}
public class bomb extends timer {
public bomb(){
System.out.println("Install a timer in bomb");
}
}
public class timer {
public timer()
{System.out.println("Install a timer");}
}
我知道很直截了当。
这是 C++ 代码的输出
Install a timer
Install a timer
Install a timer in bomb
Demolition of timer
Bomb exploded
Demolition of timer