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

发布于 2024-09-11 22:57:54 字数 976 浏览 7 评论 0原文

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

查看此代码,其中 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");
  }

}

在同一个包中创建了一个 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 
 }

}

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?

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");
  }

}

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 
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

离去的眼神 2024-09-18 22:57:54

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

在您的示例中,您可能会执行类似的操作

 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 方法),但您不希望子级能够单独调用jectFuel,因为它在 startEngine 的上下文之外没有意义(你想确保它只在那里使用)。

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

 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)他们俩。但请注意,这些都是非常简单的示例,但如果 dischargeBattery 是一个 500 行的方法,那么用其他方法编写它会很麻烦。

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

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.

旧伤慢歌 2024-09-18 22:57:54

与非抽象类中的情况相同,没有区别。

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

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

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.

掀纱窥君容 2024-09-18 22:57:54

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

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

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

抽象类。

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.

鹊巢 2024-09-18 22:57:54

该方法只能从抽象类内部访问。例如,您可以有一个带有 public final 方法的抽象类,该方法使用私有帮助器方法。

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

自我难过 2024-09-18 22:57:54
package arrayafter;

public abstract class Abstract_Demo {

    abstract void display();

    private void display1() {
        System.out.println("Private Method");
    }

    final void display2() {
        System.out.println("final Method");
        display1();
    }

    public static void display3() {

        System.out.println("Static methods");
    }

}
package arrayafter;

import java.util.Scanner;

public class Practice extends Abstract_Demo{

    public static void main(String[] args) {
        
        Practice pr=new Practice();
        pr.display();
        pr.display2();
        Abstract_Demo.display3();
    }

    @Override
    void display() {
        // TODO Auto-generated method stub
        System.out.println("Abstract method");
        
    }

}
package arrayafter;

public abstract class Abstract_Demo {

    abstract void display();

    private void display1() {
        System.out.println("Private Method");
    }

    final void display2() {
        System.out.println("final Method");
        display1();
    }

    public static void display3() {

        System.out.println("Static methods");
    }

}
package arrayafter;

import java.util.Scanner;

public class Practice extends Abstract_Demo{

    public static void main(String[] args) {
        
        Practice pr=new Practice();
        pr.display();
        pr.display2();
        Abstract_Demo.display3();
    }

    @Override
    void display() {
        // TODO Auto-generated method stub
        System.out.println("Abstract method");
        
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文