使输出匹配需要什么

时间:2016-08-21 00:48:09

标签: java

我有一个练习题,我需要完成并完成所有操作但是我无法获得输出以匹配所需的内容。我已经尝试了一些谷歌的答案,但似乎没有任何工作。下面是我得到的代码和输出与我想要的。我们不允许修改main方法,只允许修改类。

我对如何让每个类的输出从一个新行开始感到困惑。

说明书中有这样的陈述,但我不明白该怎么做:

  

Student类应该有一个公共显示函数,可以调用父类的显示   功能,

代码:

public class H255{public static void main (String[] args){while (JPL.test()){
  Person pObj = new Person("Albert","Einstein");
  Student sObj = new Student("John","Smith",123456,"First Year","Pullan");
  Teacher tObj = new Teacher("Wayne","Pullan","Computer Science",100000,"Lecturer");
  System.out.println("Person :");
  pObj.Display();
  System.out.println("");
  System.out.println("Student :");
  sObj.Display();
  System.out.println("");
  System.out.println("Teacher :");
  tObj.Display();
}}}

class Person{
  private String FirstName;
  private String LastName;

  public Person(String fName,  String lName){
    this.FirstName = fName;
    this.LastName = lName;
  }

  public void Display(){
    System.out.println("First Name: " + FirstName + " Last Name: " + LastName);
  }
}

class Student extends Person{
  private int id;
  private String standard;
  private String instructor;

  public Student(String fName,  String lName, int  nId, String stnd, String instr){
    super(fName, lName);
    this.id = nId;
    this.standard = stnd;
    this.instructor = instr;
  }

  public void Display(){
    System.out.println("ID: " + id + "Standard: " + standard + "Instructor: " + instructor);
  }
}

class Teacher extends Person{
  private String mainSubject;
  private int salary;
  private String type;

  public Teacher(String fName,  String lName, String sub, int slry, String sType){
    super(fName, lName);  
    this.mainSubject = sub;
    this.salary = slry;
    this.type = sType;
  }

  public void Display(){
    System.out.println("Main Subject: " + mainSubject + "Salary: " 
                         + salary + "Type: " + type );
  }
} 

输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

编写像这些代码的主要方法:

System.out.print("Person :");
pObj.Display();
System.out.print("Student :");
sObj.Display();
System.out.print("Teacher :");
tObj.Display();

因为:println方法有一个内置包装功能,所以只需将println替换为print。