有这么一个奇怪的例子,不知道是我的程序有问题还是我的概念错了,
在这个例子中,width 和 height分别是private 和 protected 的属性,可是我的电脑一样能把它们的数值输出。
我原来的理解是一个对象只能用对象.public的属性或方法, 我不明白,为何私有和保护的属性也可以直接输出了?
谁能帮我解释一下不?public class Rectangle { 
    //受保护的member
    public int x;
    protected int y;
    private int width;
    protected int height;     public Rectangle() { 
    }     public Rectangle(int x, int y, 
                     int width, int height) { 
        this.x = x;  
        this.y = y; 
        this.width = width;   
        this.height = height; 
    }     public void setX(int x) { this.x = x; } 
    public void setY(int y) { this.y = y; } 
    public void setWidth(int width) { this.width = width; } 
    public void setHeight(int height) { this.height = height; }     public int getX() { return x; } 
    public int getY() { return y; } 
    public int getWidth() { return width; } 
    public int getHeight() { return height; }     public int getArea() { return width*height; } 
    
    public static void main(String[] args){
     Rectangle rect=new Rectangle(10,20,30,40);
     System.out.print(rect.width);
        System.out.print(rect.height);
     }
}

解决方案 »

  1.   

    个人觉得,因为你把main方法写到了同一个类中,所以rect.witdh属性还是可以点出来的。如果不在同一个类中就运行不了了。
      

  2.   

                 被本类访问          被子类访问     被同一包下的其它类访问    被所有类访问
    private       允许                  不允许              不允许                 不允许
    protected     允许                  允许               允许                    不允许package a;import b.Rectangle;public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO 自动生成方法存根
    Rectangle r=  new Rectangle(10,20,30,40);
    System.out.println(r.x);
    System.out.println(r.width);//private修饰  这里报错了
    System.out.println(r.height);//protected 修饰 这里报错了
    }}package b;public class Rectangle { 
        //受保护的member 
        public int x; 
        protected int y; 
        private int width; 
        protected int height;     public Rectangle() { 
        }     public Rectangle(int x, int y, 
                        int width, int height) { 
            this.x = x;  
            this.y = y; 
            this.width = width;  
            this.height = height; 
        }     public void setX(int x) { this.x = x; } 
        public void setY(int y) { this.y = y; } 
        public void setWidth(int width) { this.width = width; } 
        public void setHeight(int height) { this.height = height; }     public int getX() { return x; } 
        public int getY() { return y; } 
        public int getWidth() { return width; } 
        public int getHeight() { return height; }     public int getArea() { return width*height; } 
        
    //    public static void main(String[] args){ 
    //    Rectangle rect=new Rectangle(10,20,30,40); 
    //    System.out.print(rect.width); 
    //        System.out.print(rect.height); 
    //    } }
      

  3.   

    多看点基础的知识吧...
    你都把所有的东西全部放在一个类中了,所以private类型的变量是可以访问的.
      

  4.   

    private      允许                  不允许              不允许                不允许 
    protected    允许                  允许              允许                    不允许 
      

  5.   

            被本类访问          被子类访问    被同一包下的其它类访问    被所有类访问 
    private      允许                  不允许              不允许                不允许 
    protected    允许                  允许              允许                    不允许 
    你这个是在一个类呀
      

  6.   

    private 和 protected 在没有继承的关系都时候都是一样的2L的朋友说的很详细了,就不啰嗦了