测试Java中调用是属性/方法的规律

1、对属性测试:

1-1、直接访问属性

MyFoo1.java
public class MyFoo1 {
    private String parm1 = "1-1";
    String parm2 = "1-2";
    protected String parm3 = "1-3";
    public String parm4 = "1-4";
}

MyFoo2.java
public class MyFoo2 extends MyFoo1 {
    private String parm1 = "2-1";
    String parm2 = "2-2";
    protected String parm3 = "2-3";
    public String parm4 = "2-4";

    public static void main(String[] args) {
        MyFoo2 mf2 = new MyFoo2();
        System.out.println(mf2.parm1);
        System.out.println(mf2.parm2);
        System.out.println(mf2.parm3);
        System.out.println(mf2.parm4);
        MyFoo1 mf = new MyFoo2();
//        System.out.println(mf.parm1);
        System.out.println(mf.parm2);
        System.out.println(mf.parm3);
        System.out.println(mf.parm4);
    }
}

输出
2-1
2-2
2-3
2-4
1-2
1-3
1-4

结论:obj.parm取的是定义类声明的属性,不论该属性的可见性。


1-2、通过方法访问:

测试1:

MyFoo1.java增加如下方法

    private String getParm1_1(){
        return parm1;
    }
    String getParm1_2(){
        return parm1;
    }
    protected String getParm1_3(){
        return parm1;
    }
    public String getParm1_4(){
        return parm1;
    }

    private String getParm2_1(){
        return parm2;
    }
    String getParm2_2(){
        return parm2;
    }
    protected String getParm2_3(){
        return parm2;
    }
    public String getParm2_4(){
        return parm2;
    }

    private String getParm3_1(){
        return parm3;
    }
    String getParm3_2(){
        return parm3;
    }
    protected String getParm3_3(){
        return parm3;
    }
    public String getParm3_4(){
        return parm3;
    }

    private String getParm4_1(){
        return parm4;
    }
    String getParm4_2(){
        return parm4;
    }
    protected String getParm4_3(){
        return parm4;
    }
    public String getParm4_4(){
        return parm4;
    }
MyFoo2.java的main改为

        MyFoo2 mf2 = new MyFoo2();
//        System.out.println(mf2.getParm1_1());
        System.out.println(mf2.getParm1_2());
        System.out.println(mf2.getParm1_3());
        System.out.println(mf2.getParm1_4());

//        System.out.println(mf2.getParm2_1());
        System.out.println(mf2.getParm2_2());
        System.out.println(mf2.getParm2_3());
        System.out.println(mf2.getParm2_4());

//        System.out.println(mf2.getParm3_1());
        System.out.println(mf2.getParm3_2());
        System.out.println(mf2.getParm3_3());
        System.out.println(mf2.getParm3_4());

//        System.out.println(mf2.getParm4_1());
        System.out.println(mf2.getParm4_2());
        System.out.println(mf2.getParm4_3());
        System.out.println(mf2.getParm4_4());
        
        System.out.println();
        
        MyFoo1 mf = new MyFoo2();
//        System.out.println(mf.getParm1_1());
        System.out.println(mf.getParm1_2());
        System.out.println(mf.getParm1_3());
        System.out.println(mf.getParm1_4());

//        System.out.println(mf.getParm2_1());
        System.out.println(mf.getParm2_2());
        System.out.println(mf.getParm2_3());
        System.out.println(mf.getParm2_4());

//        System.out.println(mf.getParm3_1());
        System.out.println(mf.getParm3_2());
        System.out.println(mf.getParm3_3());
        System.out.println(mf.getParm3_4());

//        System.out.println(mf.getParm4_1());
        System.out.println(mf.getParm4_2());
        System.out.println(mf.getParm4_3());
        System.out.println(mf.getParm4_4());
输出
1-1
1-1
1-1
1-2
1-2
1-2
1-3
1-3
1-3
1-4
1-4
1-4

1-1
1-1
1-1
1-2
1-2
1-2
1-3
1-3
1-3
1-4
1-4
1-4
测试2:MyFoo2中添加与MyFoo1一样的方法

输出
2-1
2-1
2-1
2-2
2-2
2-2
2-3
2-3
2-3
2-4
2-4
2-4

2-1
2-1
2-1
2-2
2-2
2-2
2-3
2-3
2-3
2-4
2-4
2-4


结论:obj.method()中存在this.parm取的是定义该方法的类声明的属性,不论该方法/属性的可见性。

2、对方法测试

2-1、直接调用方法

在1的基础上,给MyFoo1添加如下代码

    private String getParm1_1_1(){
        return getParm1_1();
    }
    String getParm1_2_1(){
        return getParm1_1();
    }
    protected String getParm1_3_1(){
        return getParm1_1();
    }
    public String getParm1_4_1(){
        return getParm1_1();
    }

    private String getParm1_1_2(){
        return getParm1_2();
    }
    String getParm1_2_2(){
        return getParm1_2();
    }
    protected String getParm1_3_2(){
        return getParm1_2();
    }
    public String getParm1_4_2(){
        return getParm1_2();
    }

    private String getParm1_1_3(){
        return getParm1_3();
    }
    String getParm1_2_3(){
        return getParm1_3();
    }
    protected String getParm1_3_3(){
        return getParm1_3();
    }
    public String getParm1_4_3(){
        return getParm1_3();
    }

    private String getParm1_1_4(){
        return getParm1_4();
    }
    String getParm1_2_4(){
        return getParm1_4();
    }
    protected String getParm1_3_4(){
        return getParm1_4();
    }
    public String getParm1_4_4(){
        return getParm1_4();
    }
MyFoo2.java的main改为

        MyFoo2 mf2 = new MyFoo2();
//        System.out.println(mf2.getParm1_1_1());
        System.out.println(mf2.getParm1_2_1());
        System.out.println(mf2.getParm1_3_1());
        System.out.println(mf2.getParm1_4_1());
//        System.out.println(mf2.getParm1_1_2());
        System.out.println(mf2.getParm1_2_2());
        System.out.println(mf2.getParm1_3_2());
        System.out.println(mf2.getParm1_4_2());
//        System.out.println(mf2.getParm1_1_3());
        System.out.println(mf2.getParm1_2_3());
        System.out.println(mf2.getParm1_3_3());
        System.out.println(mf2.getParm1_4_3());
//        System.out.println(mf2.getParm1_1_4());
        System.out.println(mf2.getParm1_2_4());
        System.out.println(mf2.getParm1_3_4());
        System.out.println(mf2.getParm1_4_4());

        System.out.println();

        MyFoo1 mf = new MyFoo2();
//        System.out.println(mf.getParm1_1_1());
        System.out.println(mf.getParm1_2_1());
        System.out.println(mf.getParm1_3_1());
        System.out.println(mf.getParm1_4_1());
//        System.out.println(mf.getParm1_1_2());
        System.out.println(mf.getParm1_2_2());
        System.out.println(mf.getParm1_3_2());
        System.out.println(mf.getParm1_4_2());
//        System.out.println(mf.getParm1_1_3());
        System.out.println(mf.getParm1_2_3());
        System.out.println(mf.getParm1_3_3());
        System.out.println(mf.getParm1_4_3());
//        System.out.println(mf.getParm1_1_4());
        System.out.println(mf.getParm1_2_4());
        System.out.println(mf.getParm1_3_4());
        System.out.println(mf.getParm1_4_4());
输出
1-1
1-1
1-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1

1-1
1-1
1-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
2-1
结论:调用方法是动态绑定的。

最终结论:属性&(静态|私有方法)是前期绑定的;其他方法是动态绑定的




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值