When I run my program I get this:
当我运行我的程序时,我得到了这个:
error: name has private access in Animal
错误:名称在Animal中具有私人访问权限
The following are my classes:
以下是我的课程:
Animal class
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)) ;
}
}
Bird class
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));
}
}
Dog class
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));
}
}
Main class
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();
}
}
The first class compiles but the other three don't because it can't access private String from the parent class. Now I know this was to occur but is there a way of making it work where the parent class has one private String named name and only one constructor?
第一个类编译但其他三个不编译,因为它无法从父类访问私有String。现在我知道这是发生的但是有没有办法让它工作在父类有一个名为name的私有String而且只有一个构造函数?
2 个解决方案
#1
1
Can be fixed by adding a public getter in the Animal class (parent)
可以通过在Animal类(父级)中添加公共getter来修复
public String getName() {
return name;
}
#2
0
The baseclass Animal has a getter for the private variable "name". Use that function, getAnimalName(), in the subclasses that extend the Animal class wherever you would like to get the name. Like this:
基类Animal有一个私有变量“name”的getter。在扩展Animal类的子类中使用该函数getAnimalName(),无论您希望获得该名称。像这样:
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());
}
}
#1
1
Can be fixed by adding a public getter in the Animal class (parent)
可以通过在Animal类(父级)中添加公共getter来修复
public String getName() {
return name;
}
#2
0
The baseclass Animal has a getter for the private variable "name". Use that function, getAnimalName(), in the subclasses that extend the Animal class wherever you would like to get the name. Like this:
基类Animal有一个私有变量“name”的getter。在扩展Animal类的子类中使用该函数getAnimalName(),无论您希望获得该名称。像这样:
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());
}
}