clone()定义为受保护的具体原因是java.lang.Object什么?
我在理解java中的受保护访问修饰符(或其背后的设计)时遇到了一些麻烦.我认为这意味着包访问和访问通过继承包含抽象成员的类的对象.
我写了以下示例代码.我看到如果取消注释,注释掉的行会产生编译错误.为什么我可以通过Second中的Second对象访问pro,但不能通过Second中的First对象访问pro?
package first;
public class First {
protected void pro(){
System.out.println("Can see protected method");
}
}
Run Code Online (Sandbox Code Playgroud)
package first;
public class InFirst {
public static void main(String[] args){
First fst = new First();
fst.pro();
}
}
Run Code Online (Sandbox Code Playgroud)
package second;
import first.First;
public class Second extends First {
public static void main(String[] args){
First fst = new First();
// fst.pro();
Second sec = new Second();
sec.pro();
}
}
Run Code Online (Sandbox Code Playgroud)