丶Gatsby 2017-11-07 01:49 采纳率: 100%
浏览 884
已采纳

希望各位大牛可以赐教。。。。

class Person
{
public static void main(String[] args)
{ String name="abc",sex="m",age="19";
System.out.println("The name is "+name+",sex is "+sex+",age is "+age);
Student stu=new Student("2017年",28);
Teacher tea=new Teacher("cjc","abc");
System.out.println(stu);
System.out.println(tea);
}
}
class Student extends Person
{
private String time;
private int num;
Student(String str,int number)
{
time=str;
num=number;
}
public String toString()
{
return "The time is "+time+"."+"The num is "+num+".";
}
}
class Teacher extends Person
{
private String title;
private String section;
Teacher(String str,String num)
{
title=str;
section=num;
}

public String toString()
{
return "The title " +title+".The section is " +section+".";

}
}

这个程序该怎样修改,才能在输入学生和老师时调用其该有的参数,而不是整体全部输出student 和 teacher

  • 写回答

6条回答 默认 最新

  • threenewbee 2017-11-07 01:53
    关注

    就像你写的public String toString()
    你想输出什么,就在里面写什么。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?