为什么Java中的抽象类中有一个私有访问修饰符,尽管我们无法创建抽象类的实例?
我知道在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于抽象类可以包含功能(与接口相反),因此它可以具有私有变量或方法。
在您的示例中,您可能会执行类似的操作
,这样您就可以将一些工作分散到其他方法(因此您没有 1000 行 startEngine 方法),但您不希望子级能够单独调用jectFuel,因为它在 startEngine 的上下文之外没有意义(你想确保它只在那里使用)。
或者甚至更多,您可能有一个私有方法,该方法在其他几个公共方法中使用不同的参数进行调用。这样,您就可以避免在每个公共方法中编写相同的代码两次或多次,并将公共代码分组到私有方法中,以确保子级不会访问它(就像他们不能在之前调用部分公共方法一样) )。像这样的事情:
这样你的方法就可以访问电池,但孩子们不应该弄乱它,并且你不会在中写入相同的行(
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
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:
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.与非抽象类中的情况相同,没有区别。
这意味着,如果抽象类中没有任何内容调用私有方法,那么您也可以删除它,因为它不会被调用(除非有一些邪恶的反射工作)。
通常,私有方法仅用作内部实用方法,这些方法具有非常具体的任务,类中的其他方法使用该方法来完成其工作。
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.
我不知道。你从哪里得到这个想法的?
抽象类。
I don't. Where did you get that idea?
The abstract class.
该方法只能从抽象类内部访问。例如,您可以有一个带有
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.