为什么在Java的抽象类中有一个私有访问修饰符,即使我们不能创建抽象类的实例?
2022-09-01 20:02:07
我知道在类中声明方法不是一个好的编码实践。即使我们无法创建类的实例,为什么访问修饰符在类中可用,它在类中的作用域是什么?在类中使用访问说明符的哪种方案?private
abstract
abstract
private
abstract
abstract
private
abstract
查看此代码,其中类是抽象的并扩展了 Vehicle。Vehicle
Car
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");
}
}
内部是创建汽车类的相同包
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
}
}