日期:2014-05-20  浏览次数:20754 次

父类应用指向子类对象,我这种写不知道哪里错了,请帮我看看
小弟初接触Java,请各位高手帮我修改下,
Java code
class Animals
{
    private String color;
    private int high;
    Animals(){
    }
    Animals(String color,int high){
        this.color = color;
        this.high = high;
    }
    
    public void info(Animals a){
        System.out.println("color="+a.color);
        if (a instanceof Bird)
        {
            Bird bird = (Bird) a;
            System.out.println("name="+bird.name+"  high="+bird.high);
        }
        else 
            System.out.println("I'd not konw who is this");
        }
}
class Bird extends Animals
{
    private String name;
    Bird(){
    }
    Bird(String name,String color,int high){
        super(color,high);
        this.name = name;
    }
    
}
public class FatherSub
{
    public static void main(String arg[]){
    Animals a = new Animals();
    Bird b = new Bird("john","blue",12);
    a.info(b);

}
}



错误提示:FatherSub.java:28: name has private access in Bird
System.out.println("name="+bird.name+" high="+bird.high);
  ^
FatherSub.java:28: high has private access in Animals
System.out.println("name="+bird.name+" high="+bird.high);
  ^
2 errors

------解决方案--------------------
因为name是Bird的,你想在Animal里面用了
可以在Bird定义一个public String getName() {return name}

然后在需要用到Bird的name的地方用getName返回就好
------解决方案--------------------
类中的private 属性 ,写上getter和 setter方法 ,使用的时候 用xxx.getName() ;
------解决方案--------------------
Java code

package test;

class Animals{
    private String color;
    private int high;
    Animals(){}
    Animals(String color,int high){
        this.color = color;
        this.setHigh(high);
    }
    
    public void info(Animals a){
        System.out.println("color="+a.color);
        if (a instanceof Bird){
            Bird bird = (Bird) a;
            System.out.println("name="+bird.getName()+"  high="+bird.getHigh());
        }else 
            System.out.println("I'd not konw who is this");
    }
    
    public void setHigh(int high) {
        this.high = high;
    }
    public int getHigh() {
        return high;
    }
}

class Bird extends Animals{
    private String name;
    Bird(){}
    Bird(String name,String color,int high){
        super(color,high);
        this.setName(name);
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    
}

public class FatherSub{
    public static void main(String arg[]){
        Animals a = new Animals();
        Bird b = new Bird("john","blue",12);
        a.info(b);
    }
}

------解决方案--------------------
class Animals {
private String color;
protected int high;

Animals() {
}

Animals(String color, int high) {
this.color = color;
this.high = high;
}

public void info(Animals a) {
System.out.println("color=" + a.color);
if (a instanceof Bird) {
Bird bird = (Bird) a;
System.out.println("name=" + bird.name + " high=" + bird.high);
} else
System.out.println("I'd not konw who is this");
}
}

class Bird extends Animals {
String name;

Bird() {
}

Bird(String name, String color, int high) {