我编写了一个应该使用驱动程序让狗说话的程序,但我在第8行的DogTest类中遇到错误,它说导入Dog无法解析。我该如何解决这个问题?
// ****************************************************************
// Dog.java
//
// A class that holds a dog's name and can make it speak.
//
// ****************************************************************
public class Dog
{
protected String name;
// ------------------------------------------------------------
// Constructor -- store name
// ------------------------------------------------------------
public Dog(String name)
{
this.name = name;
}
// ------------------------------------------------------------
// Returns the dog's name
// ------------------------------------------------------------
public String getName()
{
return name;
}
// ------------------------------------------------------------
// Returns a string with the dog's comments
// ------------------------------------------------------------
public String speak()
{
return "Woof";
}
}
// ****************************************************************
// DogTest.java
//
// A simple test class that creates a Dog and makes it speak.
//
// ****************************************************************
import Dog;
public class DogTest
{
public static void main(String[] args)
{
Dog dog = new Dog("Spike");
System.out.println(dog.getName() + " says " + dog.speak());
}
}
public class Labrador extends Dog
{
private String color; //black, yellow, or chocolate?
private int breedWeight = 75;
public Labrador(String name, String color)
{
super(name);
this.color = color;
}
// ------------------------------------------------------------
// Big bark -- overrides speak method in Dog
// ------------------------------------------------------------
public String speak()
{
return "WOOF";
}
// ------------------------------------------------------------
// Returns weight
// ------------------------------------------------------------
public int avgBreedWeight()
{
return breedWeight;
}
}
// ****************************************************************
// Yorkshire.java
//
// A class derived from Dog that holds information about
// a Yorkshire terrier. Overrides Dog speak method.
//
// ****************************************************************
public class Yorkshire extends Dog
{
public Yorkshire(String name)
{
super(name);
}
// ------------------------------------------------------------
// Small bark -- overrides speak method in Dog
// ------------------------------------------------------------
public String speak()
{
return "woof";
}
}
答案 0 :(得分:0)
您无需在同一个包中导入该类。