为什么我可以访问另一个包的其他子类中的finalize()方法? [英] Why I have access to finalize() method in other sub class of another package?

查看:140
本文介绍了为什么我可以访问另一个包的其他子类中的finalize()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手第一次尝试学习Java。
我的简单问题是java.lang.Object中的finalize()方法。为什么我在其他类中访问这个受保护的方法而不是其他受保护的方法。我的导师告诉我,protected只在其类,相同的包及其子类中具有范围。这里我读过这篇文章。

I new to Java First time I am trying to learn Java . My simple Question is on finalize () method in java.lang.Object. Why I have access to this only protected method in my other class not other protected method .My tutor told me that protected only have scope in its class, same package, and its subclass .Here I read this.

有人可以解释我是否有任何特殊情况与finalize()方法。我有一个答案不满意为什么finalize()受到保护这里
我的代码如下:

Can someone explain me is there any special case with finalize()method . I have an answer not satisfying why finalize () is protected here My Code is as Follows :

//Creating Package Foo
package Foo;
class A
{
    protected finalize() 
    { 
        System.out.println("This is finalize method of Class A");
    }
}

// Creating Another Pacakage Foo1
package Foo1;
import Foo.*;
class B extends A
{
}
class C extends B
{
}
class D extends C
{
    public void foo() {
        C ob = new C();
        ob = null;
        System.gc(); // Why class A finalize() is getting call
    }
}

只有在finalize()的情况下才会被调用,而不是在另一种情况下。
问我的导师,他拒绝回答他说你做错了我会看,但他没有回复我。

Only in case of finalize () it is getting called, not in another case . ask my tutor he refuse to answer he saying you are doing some mistake i will look but he is not replying me .

请想想我是谁java。也许我犯了一个大错误。

Please think of I am bew to java . Maybe I am doing some big mistake.

推荐答案

这可以按预期工作,我不认为 finalize()方法与Java中的任何其他方法的处理方式不同。可以认为有点不同的是 finalize()方法通常只由JVM垃圾收集器本身调用,如 JavaDoc

This works as expected, and I don't think the finalize() method is treated any differently to any other method in Java. What could be considered a bit different is that the finalize() method is normally only called by the JVM Garbage Collector itself, as outlined in the JavaDoc:


当垃圾收集确定没有对该对象的更多引用时,由对象上的垃圾收集器调用。

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

另请注意,Josh Bloch强烈警告不要在 Effective Java

Also note that Josh Bloch strongly cautions against the use of finalizers in Effective Java:


终结器是不可预测的,通常是危险的,而且通常是不必要的。它们的使用会导致行为不稳定,性能不佳和可移植性问题。终结器有一些有效用途......但根据经验,你应该避免使用终结器。

Finalizers are unpredictable, often dangerous, and generally unnecessary. Their use can cause erratic behaviour, poor performance, and portability problems. Finalizers have a few valid uses ... but as a rule of thumb you should avoid finalizers.

考虑下面的例子,它是与你类似:

Consider the following example, which is similar to yours:

一个覆盖 finalize()方法的基类。

public abstract class BaseClass {
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("BaseClass finalisation occured");
    }
}

不覆盖终结的子类: / p>

A sub-class which does not override finalize:

public class SubClass extends BaseClass {
    public void foo() {
        System.out.println("SubClass Foo'd");
    }
}

一个带有基本主要方法的驱动程序类一切:

And a driver class with a basic main method to run everything:

public class Driver {
    public static void main(String[] args) {
        SubClass sc = new SubClass();
        sc.foo();
        sc = null;
        System.gc();        
    }
}

我们得到的输出如下:

SubClass Foo'd
BaseClass finalisation occured

Java方法查找(用非常简单的术语)发生的事情是在当前类中查找任何方法,如果不是,则在找到所需方法之前爬上类层次结构。在上面的示例中,当在 SubClass 对象上调用 foo()方法时, SubClass class包含方法定义,以便使用实现,并且类层次结构不会更高。当调用 finalize()方法时(因为已请求 System.gc()),该方法将首先要在 SubClass 类中查找,但由于这不包含 finalize()的实现其父级(搜索 BaseClass )。 BaseClass 确实包含 finalize()的实现,以便使用,并将一行打印到stdout。

What happens with Java method lookup (in very simple terms) is that any method is looked for in the current class, and if not the class hierarchy is climbed until the required method is found. In the above example, when the foo() method is called on a SubClass object, the SubClass class contains the method definition so that implementation is used, and the class hierarchy is not climbed any higher. When the finalize() method is called (because a System.gc() has been requested), the method will first be looked for in the SubClass class, but since that does not contain an implementation of finalize() its parent (BaseClass) is searched. BaseClass does contain an implementation of finalize() so that is used, and a line is printed to stdout.

现在考虑再次覆盖 finalize()的子子类:

Now consider a sub-sub-class which overrides finalize() again:

public class OverridenSubClass extends SubClass {
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("Overriden finalize called, which calls super's finalize first");
    }
}

稍微修改后的驱动程序 class:

And a slightly modified Driver class:

public class Driver {

    public static void main(String[] args) {
        OverridenSubClass sc = new OverridenSubClass();
        sc.foo();
        System.out.println(sc.toString());
        sc = null;
        System.gc();
        System.exit(0);
    }
}

产生以下输出:

SubClass Foo'd
finalize.OverridenSubClass@7150bd4d
BaseClass finalisation occured
Overriden finalize called, which calls initial finalize first

希望这是预期的。这里唯一需要注意的有趣事项是:

Hopefully this is as expected. The only interesting things to note here are that:


  1. 我们不会覆盖 toString()在我们的任何类中都使用了 Object.toString()实现。

  2. 变量的类型 sc 不是决定使用的方法实现的原因 - 它是 sc 对象的类型>

  1. We don't override toString() in any of our classes so the Object.toString() implementation is used.
  2. The type of variable sc is not what determines the method implementation used - it is the type of the actual object referenced by sc

这篇关于为什么我可以访问另一个包的其他子类中的finalize()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆