即使我们无法创建抽象类的实例,为什么 Java 中的抽象类中有一个私有访问修饰符? [英] Why is there a private access modifier in an abstract class in Java, even though we cannot create an instance of an abstract class?

查看:24
本文介绍了即使我们无法创建抽象类的实例,为什么 Java 中的抽象类中有一个私有访问修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

查看这段代码,其中Vehicle 类是抽象类,Car 扩展了 Vehicle.

check out this code where Vehicle class is abstract and Car extends 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");
  }

}

在同一个包中创建一个 Car 类

Within is the same package creating a Car class

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 
 }

}

推荐答案

由于抽象类可以包含功能(而不是接口),因此它可以具有私有变量或方法.

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() {}

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

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).

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

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.
 }

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

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.

这篇关于即使我们无法创建抽象类的实例,为什么 Java 中的抽象类中有一个私有访问修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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