Java 我如何能够从一个不';你不能从那个阶级继承吗?
我在manager类中重载了Employee类的getSalary方法。Employee类的getSalaray(int)方法具有受保护的访问权限,因此只能从Manager类访问。但是当我调用m1.getSalary(25000)时,为什么编译器没有像我声明方法私有时那样抱怨“employee类中的受保护访问”?或者受保护的访问修饰符不是我假设的那样?受保护的访问修饰符允许比私有访问修饰符更多的访问形式。因为该方法在同一个包或子类中,所以允许访问Java 我如何能够从一个不';你不能从那个阶级继承吗?,java,methods,overloading,protected,Java,Methods,Overloading,Protected,我在manager类中重载了Employee类的getSalary方法。Employee类的getSalaray(int)方法具有受保护的访问权限,因此只能从Manager类访问。但是当我调用m1.getSalary(25000)时,为什么编译器没有像我声明方法私有时那样抱怨“employee类中的受保护访问”?或者受保护的访问修饰符不是我假设的那样?受保护的访问修饰符允许比私有访问修饰符更多的访问形式。因为该方法在同一个包或子类中,所以允许访问 此处的更多信息受保护的访问修饰符允许比私有的更多
此处的更多信息受保护的
访问修饰符允许比私有的更多形式的访问。因为该方法在同一个包或子类中,所以允许访问
此处的更多信息受保护
方法允许同一包
的其他成员访问不在同一包中的子类
访问级别图表:
public class Doubt1{
public static void main(String[] args){
Manager m1 = new Manager(25000);
Manager m2 = new Manager(25000);``
System.out.println(m1.getSalary(250000) + " " + m2.getSalary()); //how is m1.getSalary working when getSalary(int) is protected in Employee class
}
}
class Employee{
protected int salary;
public Employee(int s){
salary = s;
}
protected int getSalary(int s){
return salary + s;
}
}
class Manager extends Employee{
public Manager(int s){
super(s);
}
public int getSalary(){
return salary;
}
}
请阅读《受保护的方法》中的详细信息,该方法允许同一包
的其他成员访问不在同一包中的子类
访问级别图表:
public class Doubt1{
public static void main(String[] args){
Manager m1 = new Manager(25000);
Manager m2 = new Manager(25000);``
System.out.println(m1.getSalary(250000) + " " + m2.getSalary()); //how is m1.getSalary working when getSalary(int) is protected in Employee class
}
}
class Employee{
protected int salary;
public Employee(int s){
salary = s;
}
protected int getSalary(int s){
return salary + s;
}
}
class Manager extends Employee{
public Manager(int s){
super(s);
}
public int getSalary(){
return salary;
}
}
在的可能副本中阅读更多信息