如何使用Java中的继承使用toString进行打印

时间:2015-03-30 18:11:47

标签: java inheritance tostring super

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,抱歉关闭不好,试图达到这里的标准lol

1 个答案:

答案 0 :(得分:1)

你的“stus”变量只是main()方法中的范围,所以你不能在该方法之外访问它。此外,“stus”是一个数组,因此在它上面调用getId甚至没有意义。此外,请注意getId引用变量,因为它后面没有括号。

请记住,在你的toString()方法中,你已经在“学生对象”里面,所以你可以直接调用getId()函数:

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

另请注意,我已经删除了toString方法中的System.out.println()函数,因为它没有返回任何内容,因此无论如何返回都没有意义。

你的代码中有很多不正确的语法,我强烈建议你开始小一些。如果你逐步开发你的程序而不是一次性完成整个过程,你会有更好的运气。我建议您使用添加的每一行重新开始编译和测试。