Order类
package com.atguigu.java1;
public class Order {
public int numberPublic;
int numberDefault;
protected int numberProtected;
private int numberPrivate;
public void methodPublic() {
System.out.println("methodPublic");
}
void methodDefault() {
System.out.println("methodDefault");
}
protected void methodProtected() {
System.out.println("methodProtected");
}
private void methodPrivate() {
System.out.println("methodPrivate");
}
}
SubOrder类(Order不同包下的子类)
package com.atguigu.java2;
import com.atguigu.java1.*;
public class SubOrder extends Order{
public static void main(String[] args) {
SubOrder s1 = new SubOrder();
s1.numberPublic = 1;
s1.numberProtected = 2; //不同包下的子类可以调用
//s1.numberDefault = 3; 报错:The field Order.numberDefault is not visible
//s1.numberPrivate = 4; 报错:The field Order.numberDefault is not visible
s1.methodPublic();
s1.methodProtected();
//s1.methodDefault(); 报错:The method methodDefault() from the type Order is not visible
//s1.methodPrivate(); 报错:The method methodDefault() from the type Order is not visible
}
}
网友评论