Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;到Java转换-析构函数_Java_C++_Destructor - Fatal编程技术网

C++;到Java转换-析构函数

C++;到Java转换-析构函数,java,c++,destructor,Java,C++,Destructor,我需要把这个程序转换成java。除了破坏者,我还能处理其他一切。现在我已经通读了GC(垃圾收集器)及其不可靠性。我在想,如果没有办法的话,为什么要放一个析构函数让我转换呢 #include<iostream> using namespace std; class Timer{ public: Timer(); ~Timer(); }; Timer::Timer(){ cout

我需要把这个程序转换成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;
}
我知道这很直截了当

这是C语言中代码的输出++

Install a timer
Install a timer
Install a timer in bomb
Demolition of timer
Bomb exploded
Demolition of timer

您可以使用try with resources或使用
PreDestroy
注释。

C++需要析构函数来释放对象所持有的资源。最常见的需求是处置动态分配的内存

在Java中,动态分配的内存通过垃圾收集自动处理。为此,不需要析构函数

其他资源可以通过显式方法在中进行处理。您可以在
finally
块中调用此方法,也可以在Java7中使用。无论如何,考虑实现一个接口,比如“代码> java .IO。Cabelyaby//Cuth>,它指示需要显式处理,并提供一个这样的方法。

对于本程序,主要方法的近似翻译为:

public static void main(String test[])
{
    timer atimer = new timer();
    bomb abomb = new bomb();

    try {
       atimer.close();
       abomb.close();
    } catch ( java.io.IOException e ) {
        System.err.println( "An error occurred." );
    }
}

你可能想看看这个帖子——尽管仔细阅读,因为<>代码>终结()/>代码>与C++析构函数不一样!您不能假设它是否被调用,哪些对象仍然可用,等等

我不确定Java的
finalize
是否有害

对于考试中的短程序,GC不太可能调用它(如果您在Java版本中提供了
finalize()
方法)。但是,你可以演示你知道它是如何工作的,以及C++和java W.R.T对象破坏之间的区别。 编辑:

在C++程序中,最后三行输出与C++计时器对象的销毁有关,然后是C++炸弹对象,但是“代码< >定时器<代码>的拆卸出现两次-我认为您的教授可能试图证明C++中的,因为炸弹继承了定时器,析构函数的调用顺序是从大多数派生类到基类的顺序

在一个旁注中,C++析构函数应该是Virtual,但是将来你可能会知道。 作为使用

finalize()
功能的替代方法,您可以尝试以下操作:

public class mainting{

    public static void main(String test[])
    {
        timer atimer = new timer();
        bomb abomb = new bomb();

        atimer.destroy();  // since no destructor in Java, add a "destroy()" method
        abomb.destroy();

    }
}

public class bomb extends timer {
    public bomb(){
        System.out.println("Install a timer in bomb");

    }

    public void destroy(){
        System.out.println("Bomb exploded");
        super.destroy(); // destroy parent object
    }
}


public class timer {

    public timer()
    {System.out.println("Install a timer");}

    public void destroy() {
      System.out.println("Destruction of timer");
    }
}

Java不是这样工作的。相反,请使用try-with-resources。是否要我们转换此代码?你为什么不试着把你带来的东西贴出来呢!有时在语言翻译中没有直接的翻译。除了析构函数部分对我来说非常简单…没有?公共类计时器{public timer(){System.out.println(“Install a timer”);}}非常感谢,代码似乎是合法的。我仍然有一个问题……在C++中,代码给出输出…安装计时器安装计时器在炸弹中安装计时器定时炸弹爆炸拆除计时器你两次得到计时器构造函数和析构函数的输出,因为你已经声明炸弹是计时器。因此,建造/摧毁炸弹需要建造/摧毁计时器。顺便说一下,欢迎使用堆栈溢出。如果你认为这是最好的答案,点击左边的复选标记。你可以通过点击向左方点击向上箭头而找到有用的答案。非常感谢,是的,我认为你的答案是最好的,而且每个人都给了我一些关于这件事的洞察力。不幸的是,它说我没有足够的声誉。别担心,我绝对不会忘记我的第一个问题,因为编程的世界正在向我敞开大门。@Andythonas Cramer-我投票是为了保护你的代表;-)当人们在没有投票权的情况下投票时,我很烦explaination@Tom-谢谢。我很高兴被投票否决,但我想知道原因。这一个看起来很随意。谢谢,输出怎么样?我发布了C++代码的输出,因为它是在这个期中考试之后的下一章。希望这有帮助!
public class mainting{

    public static void main(String test[])
    {
        timer atimer = new timer();
        bomb abomb = new bomb();

        atimer.destroy();  // since no destructor in Java, add a "destroy()" method
        abomb.destroy();

    }
}

public class bomb extends timer {
    public bomb(){
        System.out.println("Install a timer in bomb");

    }

    public void destroy(){
        System.out.println("Bomb exploded");
        super.destroy(); // destroy parent object
    }
}


public class timer {

    public timer()
    {System.out.println("Install a timer");}

    public void destroy() {
      System.out.println("Destruction of timer");
    }
}