java 动态绑定和静态绑定例子分析

 关于java 动态绑定和静态绑定

//此处是可以的,继承父类的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的实例变量也是静态绑定的

     * 其余的都是动态绑定。

     * 用法:就是在向上转型的的时候,如果是静态绑定是从上向下的查找,现实父亲,再是儿子,如果是动态的则先是儿子,如果没有,才找父亲

     * */  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值