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类里面
public String toString()
{
return "名字=" + name + ",性别="+ sex +",年龄="+age+",学号="+num+", 时间=" +time ;
}
教师的做法类似
你这代码结构都不太合理,我给你改了一下。但是是符合你的要求的。
package pratice;
public class Main
{
public static void main(String[] args)
{
Person person=new Person();
Student stu=new Student("2017年",28);
Teacher tea=new Teacher("cjc","abc");
System.out.println(person);
System.out.println(stu);
System.out.println(tea);
}
}
class Person
{
String name="abc",sex="m",age="19";
public String toString()
{
return "The name is "+name+",sex is "+sex+",age is "+age;
}
}
class Student extends Person
{
private String time;
private int num;
Student(String str,int number)
{
time=str;
num=number;
}
public String toString()
{
return super.toString()+",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 super.toString()+",The title " +title+",The section is " +section+".";
}
}
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Program
{
public static void main (String[] args) throws java.lang.Exception
{
Student stu=new Student("abc", "m", "19", "2017年", 28);
Teacher tea=new Teacher("cjc","abc");
System.out.println(stu);
System.out.println(tea);
}
}
class Person
{
public String name;
public String sex;
public String age;
public String toString()
{
return "name=" + name + ",sex=" + sex + ",age=" + age;
}
public Person(String name, String sex, String age)
{
this.name = name; this.sex = sex; this.age = age;
}
}
class Student extends Person
{
public String time;
public int num;
public Student(String name, String sex, String age, String time, int num)
{
super(name, sex, age);
this.time = time; this.num = num;
}
public String toString()
{
return "名字=" + name + ",性别=" + sex + ",年龄=" + age + ",学号=" + num + ", 时间=" + time;
}
}
class Teacher extends Person
{
public String title;
public String section;
public Teacher(String str,String num)
{
super("","","");
title = str;
section = num;
}
public String toString() { return "The title " +title+".The section is " +section+"."; }
}