private和protected有什么区别啊,大伙能具体地和我讲讲吗?ps:可能把图片下载后看得清楚些(具体步骤:单击图片弹出大图后,再右键选择“图片另存为”)http://imm.io/7eiT

解决方案 »

  1.   

    private 只有本类可以使用
    protected 是子类和同一包内只是引用范围不一样
      

  2.   


    package com.py.test;public class TestOne extends TestTwo
    {
    public static void main(String[] args)
    {
    TestTwo two = new TestTwo();
    //跨类了,所以无法访问,这样写无法编译,就算是子类,依然无法访问
    String name = two.name;
    }
    }
    class TestTwo
    {
    private String name = "py";//私有变量
    }package com.py.test;public class Test extends Father
    {
    public static void main(String[] args)
    {
    Test t = new Test();
    String name = t.name;//虽然跨类,但是子类可以访问
    }
    }
    class Father
    {
    protected String name = "py";//受保护变量
    }