无法访问Java的父类中的私有字符串

时间:2018-08-02 17:01:06

标签: java inheritance private

运行程序时,我得到以下信息:

  

错误:姓名在Animal中具有私人访问权限

以下是我的课程:

动物课

public class Animal 
{
    private String name; 

    public Animal(String name)
    {
         this.name = name ;
         System.out.println("The Animal " + name + " has been created.") ;

    }
    public String getAnimalName()
    {
        return name ;
    }
    public String setAnimalName(String newName)
    {
        return name = newName ;
    }
    public void sleep()
    {
        System.out.println("The Animal" + name + " is asleep.");
    }
    public void noise()
    {
        System.out.println("The Animal " + name + " is making a noise.");
    }

    public String toString()
    {
        return "The Animal " + name ;
    }
    public boolean equals(Animal otherAnimal)
    {
        return (name.equals(otherAnimal.name)) ;
    }


}

鸟班

public class Bird extends Animal
{
    public Bird (String name)
    {
        super(name);
        System.out.println("A Bird " + name + " has been created.");

    }
    public void makesNoise() 
    {
        System.out.println("The Bird " + name + " is chirping.");
    }
    public String toString() 
    {
        System.out.println("The Bird " + name);
    }
    public boolean equals(Bird otherBird)
    {
        return (name.equals(otherBird.name));
    }

}

狗类

public class Dog extends Animal
{
    private String breed ;

    public Dog (String name, String breed)
    {
        super(name);
        this.breed = breed ;
        System.out.println("A Dog " + name + "of breed " + breed 
                           + " has been created.") ;
    }
    public String getDogBreed()
    {
        return breed ;
    }
    public String setDogBreed(String newBreed)
    {
        return breed = newBreed ;
    }   
    public void makeNoise() 
    {
        System.out.println("The  " + name + " is barking.");
    }
    public String toString() 
    {
        System.out.println("The Dog " + name + " of breed " + breed);
    }
    public boolean equals(Dog otherDog)
    {
        return (name.equals(otherDog.name) &&
                breed.equals(otherDog.breed));
    }

}

主班

public class AnimalMain 
{
    public static void main(String[] args) 
    {

    Animal animal = new Animal("Oscar");
    Bird bird = new Bird("Tweety");
    Dog dog = new Dog("Inka", "Mutt");

    animal.toString();
    bird.toString();
    dog.toString();

    }

}

第一个类可以编译,而其他三个则不能,因为它不能从父类访问私有String。现在我知道这会发生,但是有一种方法可以使它在父类具有一个名为name且只有一个构造函数的私有String的情况下起作用?

2 个答案:

答案 0 :(得分:1)

可以通过在Animal类(父类)中添加公共获取器来解决

public String getName() {
   return name;
}

答案 1 :(得分:0)

基类Animal具有专用变量“名称”的吸气剂。在子类中使用该函数getAnimalName()可以扩展Animal类的位置,无论您要获取名称的位置如何。像这样:

public class Animal 
{
    private String name; 

    public Animal(String name)
    {
        this.name = name ;
        System.out.println("The Animal " + name + " has been created.") ;
    }

    ...

    public String getAnimalName()
    {
        return name ;
    }
}


public class Bird extends Animal
{
    public Bird (String name)
    {
        super(name);
        System.out.println("A Bird " + name + " has been created.");

    }

    ...

    public String toString() 
    {
        System.out.println("The Bird " + getAnimalName());
    }

}