即使我们无法创建抽象类的实例,为什么 Java 中的抽象类中有一个私有访问修饰符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3334380/
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 22:20:28  来源:igfitidea点击:

Why is there a private access modifier in an abstract class in Java, even though we cannot create an instance of an abstract class?

javaabstract-classprivate

提问by Srinivas M.V.

I know it is not a good coding practice to declare a method as privatein an abstractclass. Even though we cannot create an instance of an abstractclass, why is the privateaccess modifier available within an abstractclass, and what is the scope of it within an abstractclass? In which scenario is the privateaccess specifier used in an abstractclass?

我知道privateabstract类中声明方法不是一个好的编码习惯。即使我们不能创建abstract类的实例,为什么private访问修饰符在abstract类中可用,它在abstract类中的作用域是什么?在哪种情况下privateabstract类中使用访问说明符?

check out this code where Vehicleclass is abstract and Carextends Vehicle.

查看此代码,其中Vehicle类是抽象的并Car扩展了 Vehicle。

package com.vehicle;

abstract class Vehicle {

 // What is the scope of the private access modifier within an abstract class, even though  method below cannot be accessed??
  private void onLights(){
   System.out.println("Switch on Lights");
  }

  public void startEngine(){
   System.out.println("Start Engine");
  }

}

Within is the same package creating a Car class

里面是创建 Car 类的同一个包

package com.vehicle;
/*
 * Car class extends the abstract class Vehicle
 */
public class Car extends Vehicle {

 public static void main(String args[]){
  Car c =  new Car();
  c.startEngine();
  // Only startEngine() can be accessed 
 }

}

采纳答案by Andrei Fierbinteanu

Since an abstract class can contain functionality (as opposed to an interface) it can have private variables or methods.

由于抽象类可以包含功能(与接口相反),因此它可以具有私有变量或方法。

In your example you might do something like

在你的例子中,你可能会做类似的事情

 public void startEngine(){
   injectFuel();
   igniteSpark();
   // etc. my understanding of engines is limited at best
   System.out.println("Start Engine");
 }

 private void injectFuel() {}
 private void igniteSpark() {}

That way you can spread some of the work to other methods (so you don't have a 1000 line startEngine method), but you don't want the children to be able to call injectFuel separately since it doesn't make sense outside the context of startEngine (you want to make sure it's only used there).

这样您就可以将一些工作分配给其他方法(因此您没有 1000 行的 startEngine 方法),但是您不希望孩子们能够单独调用 injectFuel,因为它在外部没有意义startEngine 的上下文(你想确保它只在那里使用)。

Or even more you might have a private method that gets called in several other public methods, with different parameters. This way you avoid writing the same code twice or more in each of the public methods, and grouping the common code in a private method makes sure the children don't access it (like they couldn't just call part of the public method before). Something like this:

或者甚至更多,您可能有一个私有方法,它在其他几个公共方法中被调用,具有不同的参数。这样您就可以避免在每个公共方法中编写两次或更多次相同的代码,并且将公共代码分组到一个私有方法中可以确保孩子们不会访问它(就像他们之前不能只调用部分公共方法一样) )。像这样的东西:

 public void startEngine() {
   dishargeBattery(50);
   System.out.println("Start Engine");
 }

 public void startRadio() {
   dischargeBattery(20);
 }

 private void dischargeBattery(int value) {
   battery.energy -= value; //battery should probably be a private field.
 }

This way your methods can have access to the battery, but the children shouldn't mess with it, and you don't write the same line (battery.energy -= value) in both of them. Take note though, that these are very simple examples, but if dischargeBattery was a 500 line method, writing it in both the other methods would be a hassle.

这样您的方法就可以访问电池,但孩子们不应该弄乱它,并且您不要battery.energy -= value在它们中写相同的行 ( )。请注意,这些都是非常简单的示例,但如果放电电池是 500 行方法,那么用其他两种方法编写它都会很麻烦。

回答by Joachim Sauer

It's the same as in a non-abstract class, there's no difference.

它与非抽象类相同,没有区别。

Which means that if nothing in your abstract class calls the private method, then you can just as well remove it, as it won't be called (baring some evil reflection work).

这意味着如果您的抽象类中没有任何东西调用私有方法,那么您也可以将其删除,因为它不会被调用(除了一些邪恶的反射工作)。

Usually, private methods are only used as internal utility methods that have a very specific task that the other methods in the class use to do their work.

通常,私有方法仅用作内部实用方法,这些方法具有非常具体的任务,类中的其他方法使用该方法来完成其工作。

回答by Ronald Wildenberg

The method can be accessed only from within the abstract class. For example, you could have an abstract class with a public finalmethod that makes use of a private helper method.

该方法只能从抽象类中访问。例如,您可以有一个抽象类,其中的public final方法使用私有辅助方法。

回答by user207421

I know it is not a good coding practice to declare a method as private in an abstract class.

我知道在抽象类中将方法声明为私有不是一个好的编码习惯。

I don't. Where did you get that idea?

我不。你从哪里得到这个想法的?

what is the scope of it within an abstract class?

它在抽象类中的范围是什么?

The abstract class.

抽象类。