java - 面试题:请问,以下哪些修饰符可以使其修饰的变量只能对同包类或子类有效?
天蓬老师
天蓬老师 2017-04-18 10:50:56
0
1
1977

如题这个答案正确吗?

package com;

public class TestParent {
    protected String protectedStr="protected";
    String defaultStr="default";

    private void method() {

    }
    void abm(){
        
    }

    protected void proabc(){
        
    }
}
package com;

public class TestSon extends TestParent{

    private void test(){
    
        
    }
}
package com;

class Main {

    static public void main(String[] args) {

        TestParent parent = new TestParent();
        TestSon son = new TestSon();
        parent.abm();//default method 
        son.abm();//default method 
        System.out.println(parent.protectedStr);
        System.out.println(parent.defaultStr);

        // -------------
        parent.proabc(); //protected method 
        son.proabc(); //protected method 
        System.out.println(son.protectedStr);
        System.out.println(son.defaultStr);

    }

}
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(1)
Ty80
Scope Current class Current package Descendants Other packages
public
protected ×
friendly × ×
private × × ×

If you don’t write it, the default is friendly.

There is nothing wrong with choosing C

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template