【问题标题】:Using and returning methods使用和返回方法
【发布时间】:2016-08-25 04:13:37
【问题描述】:

对于第 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";
  }
}

【问题讨论】:

  • 我看不到可以使用的Dogs 类型。但它将是层次结构中最高的。
  • 据我所知只有拉布拉多和约克郡
  • 我猜 Labrador 和 Yorskhire 类扩展了一些东西。这将是您要使用的类。
  • 他们没有。这就是所有可用的代码,这是一个大学 cis 201 的作业,说明非常不清楚
  • 那么根本没有其他课程提供给您吗? selected_dog 应该是什么类型?这里缺少一些东西。编辑:必须为您提供其他类,因为my_dog.speak() 正在对另一个不在此处的类进行方法调用。

标签: java methods return


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多