有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用和返回方法

对于第1步和第2步,我试图弄清楚必须在main方法中定义什么类型的狗,以便它可以是任何类型的狗,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";
  }
}

共 (1) 个答案

  1. # 1 楼答案

    既然您提供了额外的类,我可以告诉您正确的类型只是Dog

    所以public static Dog getDog(),并在其他需要类型的地方使用Dog