//此处是可以的,继承父类的main方法
// 此处也是可以的,重写了父类的main方法
public class SupperCls {
private String str="private sup str";
public String str1="public sup str1";
protected String str2="protected sup str2";
String str3="default sup str3";
static String str4="static sup str4";
public static void kk(){
System.out.println("sup kk");
}
public void dd(){
System.out.println("sup dd");
}
}
public class Subcls extends SupperCls{
private String str="private sub str";
public String str1="public sub str1";
protected String str2="protected sub str2";
String str3="default sub str3";
static String str4="static sub str4";
public static void kk(){
System.out.println("sub kk");
}
public void dd(){
System.out.println("sub dd");
}
}
public class TTT {
public static void main(String[] args) {
SupperCls s1=new Subcls();
System.out.println(s1.str1);
System.out.println(s1.str2);
System.out.println(s1.str3);
System.out.println(s1.str4);
s1.kk();
s1.dd();
System.out.println("--------------------------");//以上输出全是父类的
/**
* 预想结果
* private sup str//私有不能被调用,此行不输出
* public sup str1
* protected sup str2
* default sup str3
* static sup str4
* sup kk
* sup dd//此行输出的是子类的方法
* 正解key:
* public sup str1
protected sup str2
default sup str3
static sup str4
sup kk
sub dd
* */
Subcls s2=(Subcls)s1;
System.out.println(s2.str1);
System.out.println(s2.str2);
System.out.println(s2.str3);
System.out.println(s2.str4);
s2.kk();
s2.dd();
System.out.println("__________________________");//以上输出只有父类的static方法和父类的static属性
/**
*预想 结果
* public sub str1
* protected sup str2
* default sub str3
* static sub str4
* sup kk
* sup dd
* 正解key:
* public sub str1
protected sub str2
default sub str3
static sub str4
sub kk
sub dd
* */
Subcls s3=new Subcls();
System.out.println(s3.str1);
System.out.println(s3.str2);
System.out.println(s3.str3);
System.out.println(s3.str4);
s3.kk();
s3.dd();
System.out.println("_____________________________");//输出同S2
/**
* 预想 结果 同s2
*正解key
* public sub str1
protected sub str2
default sub str3
static sub str4
sub kk
sub dd
* */
/*小结 动态绑定与静态绑定
* java当中的方法只有final,static,private和构造方法是前期绑定
* java的实例变量也是静态绑定的
* 其余的都是动态绑定。
* 用法:就是在向上转型的的时候,如果是静态绑定是从上向下的查找,现实父亲,再是儿子,如果是动态的则先是儿子,如果没有,才找父亲
* */ }
}