对于第1步和第2步,我试图弄清楚主方法中必须定义哪种类型的狗,这样它可以是任何类型的狗,getDog方法必须返回什么类型的狗,所以它可以是任何类型狗。
import java.util.Scanner;
public class DogsTest
{
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
final String YES = "y";
String answer;
_______________________________ my_dog; // Step 1
do
{
// ------------------------------------------------------------
// The compiler cannot know at compile time what type my_dog is
// so it is determined at runtime every time the loop iterates
// ------------------------------------------------------------
my_dog = getDog();
System.out.println(my_dog.getName() + " says " + my_dog.speak());
System.out.print("Try again? ");
answer = input.next();
} while (answer.equalsIgnoreCase(YES));
}
public static _________________ getDog() // Step 2
{
int choice;
____________________ selected_dog; // Step 3
String name,
color;
do
{
// ----------------------------------
// A null reference indicates that an
// invalid menu choice was entered
// ----------------------------------
selected_dog = null;
System.out.print("Choose a Breed (1. Labrador 2. Yorkshire): ");
choice = input.nextInt();
switch (choice)
{
case 1: System.out.print("Enter dog's name: ");
name = input.next();
System.out.print("Enter dog's color: ");
color = input.next();
selected_dog = __________________________________; // Step 4
break;
case 2: System.out.print("Enter dog's name: ");
name = input.next();
selected_dog = __________________________________; // Step 5
break;
default: System.out.println("Invalid choice");
break;
}
} while (selected_dog == null);
return __________________; // Step 6
}
}
狗班
// -------------------------------------------------------
// Dog superclass:
//
// A class that holds a dog's name and can make it speak
// -------------------------------------------------------
public class Dog
{
protected String name;
public Dog(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public String speak()
{
return "Woof";
}
}
拉布拉多班
// ----------------------------------------------------------
// Labrador subclass:
//
// A class derived from Dog that holds information about
// a labrador retriever, overrides the Dog speak method and
// includes information about average weight for this breed
// ----------------------------------------------------------
public class Labrador extends Dog
{
private String color;
private static int breed_weight = 75;
public Labrador(String name, String color)
{
this.color = color;
}
// =========================================
// Big bark -- overrides speak method in Dog
// =========================================
public String speak()
{
return "WOOF";
}
public static int avgBreedWeight()
{
return breed_weight;
}
}
约克郡班级
// -------------------------------------------------------
// Yorkshire subclass:
//
// A class derived from Dog that holds information about
// a Yorkshire terrier and 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)
现在你提供了额外的课程,我可以告诉你,正确的类型只是Dog
。
所以public static Dog getDog()
,并在需要类型的其他地方使用Dog
。