迫害老作业。咱们继续
今天的任务是写一个人类,并由其派生学生类和教室类
代码如下:
package natusyasumi; import java.util.Scanner; class Person{ protected String no; protected String name; public Person() {} public Person(String a,String b) { this.no=a; this.name=b; } public Person(Person p) { this.no=p.no; this.name=p.name; } public void show() { System.out.print("No="+no+",Name="+name+"\n"); } public void setNo(String a) { no=a; } public void setName(String b) { name=b; } public String getNo() { return no; } public String getName() { return name; } } class Student extends Person { private String sNo,className; private double score; public Student(String a,String b,String c,String d,double e){ this.no=a; this.name=b; this.sNo=c; this.className=d; this.score=e; } public Student(Student c) { this.no=c.no; this.name=c.name; this.sNo=c.sNo; this.className=c.className; this.score=c.score; } public void setSNo(String a) { sNo=a; } public String getSNo() { return sNo; } public void setClassName(String c) { className=c; } public String getClassName() { return className; } public void setScore(double b) { score=b; } public double getScore() { return score; } public void show() { System.out.print("No="+no+",Name="+name+"\n"); System.out.print("SNo="+sNo+",ClassName="+className+",Score="+(int)(score+0.5)+"\n"); } } class Teacher extends Person { private String tNo,departmentName; private double wages; public Teacher(String a,String b,String c,String d,double e){ this.no=a; this.name=b; this.tNo=c; this.departmentName=d; this.wages=e; } public Teacher(Teacher c) { this.no=c.no; this.name=c.name; this.tNo=c.tNo; this.departmentName=c.departmentName; this.wages=c.wages; } public void setTNo(String a) { tNo=a; } public String getTNo() { return tNo; } public void setDepartmentName(String c) { departmentName=c; } public String getDepartmentName() { return departmentName; } public void setWages(double b) { wages=b; } public double getWages() { return wages; } public void show() { System.out.print("No="+no+",Name="+name+"\n"); System.out.print("TNo="+tNo+",DepartmentName="+departmentName+",Wages="+(int)(wages+0.5)+"\n"); } } public class Class6 { public static void main(String[] args) { String s1="130502190001010332"; String s2="doublebest"; String s3="20181234"; String s4="铁1801"; double value=60.67; Student stu1=new Student(s1,s2,s3,s4,value); stu1.show(); Student stu2=new Student(stu1); Scanner input=new Scanner(System.in); s3=input.next(); s4=input.next(); value=input.nextDouble(); stu2.setSNo(s3); stu2.setClassName(s4); stu2.show(); Teacher t1=new Teacher(s1,s2,s3,s4,value); t1.show(); Teacher t2=t1; t2.setTNo(s3); t2.setDepartmentName(s3); t2.show(); input.close(); } }
运行结果如下: