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
就像你写的public String toString()
你想输出什么,就在里面写什么。
没有看懂什么意思?
就图片上说的这个意思,谢谢
就图片上说的这个意思,谢谢
就是图片上说的意思,谢谢!
首先程序是不会知道你想输出什么的,你如果想输出不同的样式就要自己写好输出格式的代码方法,然后你要根据传参来确定调用哪个输出格式的方法,而不是一厢情愿的认为自己什么都不写就可以按照你的意思输出,否则程序员也就没必要存在来