Java 不能将抽象方法声明为私有

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2874350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:55:19  来源:igfitidea点击:

Can't declare an abstract method private

javaoop

提问by Zombies

I want to do this, yet I can't. Here is my scenario and rational. I have an abstract class for test cases that has an abstract method called test(). The test() method is to be defined by the subclass; it is to be implemented with logic for a certain application, such as CRMAppTestCase extends CompanyTestCase. I don't want the test() method to be invoked directly, I want the super class to call the test() method while the sub class can call a method which calls this (and does other work too, such as setting a current date-time right before the test is executed for example). Example code:

我想这样做,但我不能。这是我的场景和理性。我有一个用于测试用例的抽象类,它有一个名为 test() 的抽象方法。test() 方法由子类定义;它将通过特定应用程序的逻辑实现,例如CRMAppTestCase extends CompanyTestCase. 我不希望直接调用 test() 方法,我希望超类调用 test() 方法,而子类可以调用调用此方法的方法(也可以执行其他工作,例如设置当前例如,在执行测试之前的日期时间)。示例代码:

public abstract class CompanyTestCase {
    //I wish this would compile, but it cannot be declared private
    private abstract void test();

    public TestCaseResult performTest() {
        //do some work which must be done and should be invoked whenever 
        //this method is called (it would be improper to expect the caller
        // to perform initialization)
       TestCaseResult result = new TestCaseResult();
       result.setBeginTime(new Date());
       long time = System.currentTimeMillis();
       test(); //invoke test logic
       result.setDuration(System.currentTimeMillis() - time);
       return result;
    }
}

Then to extend this....

然后扩展这个......

public class CRMAppTestCase extends CompanyTestCase {

    public void test() {
        //test logic here
    }

}

Then to call it....

然后调用它......

TestCaseResult result = new CRMAppTestCase().performTest();

采纳答案by Jesper

Private methods are not polymorphic (you cannot inherit them), so it makes no sense to make a private method abstract. Making a method abstract means you'd have to override and implement it in a subclass, but since you can't override private methods, you can't make them abstract either.

私有方法不是多态的(您不能继承它们),因此将私有方法抽象化是没有意义的。使方法抽象意味着您必须在子类中覆盖和实现它,但由于您不能覆盖私有方法,因此也不能使它们抽象。

You should make it protectedinstead of private.

你应该让它protected而不是private.

Private really means private to the class you've defined the method in; even subclasses don't see private methods.

Private 真正意味着对您在其中定义方法的类是私有的;甚至子类也看不到私有方法。

回答by duffymo

I want to do this, yet I can't

我想这样做,但我不能

Changing this will require rewriting the compiler, which isn't likely.

改变这将需要重写编译器,这不太可能。

You realize why this can never work, right? A subclass can't override a private method, yet, it's abstract which says they must. Catch-22.

你明白为什么这永远行不通,对吧?子类不能覆盖私有方法,但是,它是抽象的,表示必须覆盖。第 22 条军规。

回答by Maxime ARNSTAMM

abstract means you'll want to define the method in a child class

抽象意味着你要在子类中定义方法

private means the child classes won't see the method

私有意味着子类不会看到该方法

How could it compile ?

怎么可能编译?

If the method should end up public, declare it public abstract, that's all.

如果该方法最终应该是 public,则将其声明为 public abstract,仅此而已。

回答by Vitaly Bogdanov

You should declare your "test" method as "protected" to achieve your goal. But it still will be accessible from the package of the class.

您应该将“测试”方法声明为“受保护”以实现您的目标。但它仍然可以从类的包中访问。

回答by dbyrne

You cannot have a private abstractmethod because subclasses can't see private members of a superclass. You need to make your testmethod protected.

您不能拥有private abstract方法,因为子类无法看到超类的私有成员。你需要制定你的test方法protected

回答by Viktor Tóth

I'd like to point out that it is the choice of Javanot to let abstract private methods implemented (e.g. in C++ you can and is advisable). Saying that private methods are not polymorphic in general is just ignorant of the fact that calling and defining a method is two different mechanism - although subclasses can never call a private method of a superclass, they could be able to define it. Mixing the two is a common misconception, please be aware.

我想指出,Java选择是不让抽象私有方法实现(例如,在 C++ 中,您可以并且是可取的)。说私有方法通常不是多态的只是不知道调用和定义方法是两种不同的机制 - 尽管子类永远不能调用超类的私有方法,但他们可以定义它。将两者混用是一种常见的误解,请注意。

I don't have enough reputation to comment, that's why I'm writing an answer.

我没有足够的声誉来发表评论,这就是我写答案的原因。