簡體   English   中英

如何在Java中使用繼承使用toString打印

[英]How do I print using toString using inheritance in Java

public class StuTest2
{
   public static final int NUMBER_OF_STUDENTS = 7;

   public static void main(String[] args)
   {
  Student[] stus = new Student[NUMBER_OF_STUDENTS];

  // Student has ID, name, and GPA
  stus[0] = new Student(123, "Suzy", 3.9);

  // Default for missing GPA will be 9.99 "special value
  stus[1] = new Student(234, "Tom");

  // Default name will be "Student #xxx" where
  // "xxx" is the actual ID number
  stus[2] = new Student(456);

  // A grad student also has a thesis topic
  stus[3] = new GradStudent(567, "Fred", 3.8, "Java");

  // Default thesis topic is "Undecided"
  stus[4] = new GradStudent(678, "Staci", 3.1);

  // Doctoral students earn a stipend
  stus[5] = new DoctoralStudent(789, "Mandy", 4.0, "Databases", 3550.00);

  // If missing, the default stipend is $3000.00
  stus[6] = new DoctoralStudent(890, "Ned", 3.7, "Cisco Networking");

  // Inside the loop, the toString method is called for each
  // student. All graduate students show the word "Graduate" in
  // front of the output from this method.
  for(Student stu : stus)
  {
  }
 }
}

class Student
  {
   private int id;
   private String name;
   private double gpa;

   public Student(int i, String n, double g)
   {
       id = i;
       name = n;
       gpa = g;
   }

   public Student(int i)
   {
       this(i, "Student #" + i);
   }

   public Student(int i, String n)
   {
       this(i, n, 9.99);
   }

   public int getId()
   {
       return id;
   }

   public String getName()
   {
       return name;
   }

   public double getGPA()
   {
       return gpa;
   }

   public String toString()
   {
       return System.out.println(stus.getId+", " + stus.getName
            + ", " + stus.getGPA);
   }

  }

class GradStudent extends Student
{
private String topic;

public GradStudent(int i, String n, double g, String t)
{
    super(i, n, g);
    topic = t;
}

public GradStudent(int i, String n, double g)
{
    this(i, n, g, "Undecided");
}

public String getTopic()
{
    return topic;
}

public String toString()
{
    return super.getTopic();
}
}

class DoctoralStudent extends GradStudent
{
private double stip;

public DoctoralStudent(int i, String n, double g, String t, double s)
{
    super(i, n, g, t);
    stip = s;
}

public DoctoralStudent(int i, String n, double g, String t)
{
    this(i, n, g, t, 3000.00);
}

public double getStip()
{
    return stip;
}

public String toString()
{
    return super.getStip();
}

}

我正在嘗試使用return super.toString()進行打印,但是我發現錯誤提示無法找到stus的符號,但是我在開始學生課程之前就擁有了它。 是什么賦予了? ps,對不起,結賬不好,嘗試達到這里的標准,哈哈

您的“ stus”變量僅 main()方法內部,因此您不能在該方法之外訪問它。 此外,“ stus”是一個數組,因此在其上調用getId甚至都沒有意義。 此外,請注意,getId引用變量,因為它后面沒有括號。

請記住,在您的toString()方法中,您已經在“學生對象”內部,因此您可以直接調用getId()函數:

 public String toString()
   {
       return getId() +", " + getName() + ", " + getGPA();
   }

還要注意,我已經在您的toString方法中刪除了System.out.println()函數,因為它不返回任何內容,因此無論如何都沒有意義。

您的代碼中有很多不正確的語法,我強烈建議從小得多的角度開始。 如果逐步開發程序,而不是一口氣完成全部工作,那么運氣會更好。 我建議從頭開始,對添加的每一行進行編譯和測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM