編寫教師類java
要求有屬性「姓名name」,「年齡age」,「職稱post」,「基本工資salary」ide
編寫業務方法, introduce(),實現輸出一個教師的信息。post
編寫教師類的三個子類:教授類(professor)、副教授類(vice professor)、講師類(lecturer)。測試
工資級別分別爲:教授爲1.三、副教授爲1.二、講師類1.1。在三個子類裏面都重寫父類的introduce()方法。this
定義並初始化一個老師對象,調用業務方法,實現對象基本信息的後臺打印。spa
package polymorphic_HomeWork; public class WorkTest_01 { public static void main(String[] args) { Teacher professor = new Professor("小范",20,"professor",4000.0); Teacher viceProfessor = new ViceProfessor("小黃",18,"ViceProfessor",3000.0); Teacher lecturer = new Lecturer("小雨",19,"lecturer",2000.0); System.out.println(professor.introduce()); System.out.println(viceProfessor.introduce()); System.out.println(lecturer.introduce()); } } //要求有屬性「姓名name」,「年齡age」,「職稱post」,「基本工資salary」, //編寫業務方法, introduce(),實現輸出一個教師的信息。 class Teacher{ private String name; private int age; private String post; private double salary; public Teacher(String name, int age, String post, double salary) { super(); this.name = name; this.age = age; this.post = post; this.salary = salary; } public Teacher() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPost() { return post; } public void setPost(String post) { this.post = post; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } //introduce(),實現輸出一個教師的信息。 public String introduce() { return "Teacher [name=" + name + ", age=" + age + ", post=" + post ; } } //教授類(professor) class Professor extends Teacher{ private double level; public Professor(String name, int age, String post, double salary) { super(name, age, post, salary); this.level = 1.3; } public double getLevel() { return level; } public void setLevel(double level) { this.level = level; } @Override public String introduce() { // TODO Auto-generated method stub return "Professor " + super.introduce() + ", salary=" + getSalary()*level + "]"; } } //副教授類(vice professor) class ViceProfessor extends Teacher{ private double level; public ViceProfessor(String name, int age, String post, double salary) { super(name, age, post, salary); this.level = 1.2; } public double getLevel() { return level; } public void setLevel(double level) { this.level = level; } @Override public String introduce() { // TODO Auto-generated method stub return "ViceProfessor " + super.introduce() + ", salary=" + getSalary()*level + "]"; } } //講師類(lecturer) class Lecturer extends Teacher{ private double level; public Lecturer(String name, int age, String post, double salary) { super(name, age, post, salary); this.level = 1.1; } public double getLevel() { return level; } public void setLevel(double level) { this.level = level; } @Override public String introduce() { // TODO Auto-generated method stub return "Lecturer " + super.introduce() + ", salary=" + getSalary()*level + "]"; } }
設計父類—員工類。子類:工人類(Worker),農民類(Peasant),教師類(Teacher),科學家類(Scientist),服務生類(Waiter)。設計
(1) 其中工人,農民,服務生只有基本工資。3d
(2) 教師除基本工資外,還有課酬(元/天)。對象
(3) 科學家除基本工資外,還有年終獎(bonus)。blog
(4) 編寫一個測試類,將各類類型的員工的整年工資打印出來。
package polymorphic_HomeWork; public class WorkTest_02 { public static void main(String[] args) { Employee worker = new Worker("工人 小范",20,2000); Employee peasant = new Peasant("農民 小范",40,1000); Employee waiter = new Waiter("服務生 小范",25,3000); Employee teacher = new Teacher("老師 小范",30,4000,50,200); Employee Scientist = new Scientist("科學家 小范",35,3500,250000); //這裏建立的對象的編譯類型都是Employee // System.out.println(worker.toString()); // System.out.println(peasant.toString()); // System.out.println(waiter.toString()); // System.out.println(teacher.toString()); // System.out.println(Scientist.toString()); WorkTest_02 wt = new WorkTest_02(); wt.showAll(Scientist); wt.showAll(teacher); wt.showAll(waiter); wt.showAll(peasant); wt.showAll(worker); } public void showAll(Employee e) { //當有對象傳入時,它的編譯類型就變爲了Employee。 //因爲子類重寫了父類的show方法,而這裏e的運行類型是動態綁定了的。 //因此會自動的尋找到傳入類型所對應的show方法 //父類的show方法,至關於讓編譯器能夠經過編譯。若是父類中沒有show方法會報語法錯誤。 //由於e的編譯類型爲Employee. e.show(); } } class Employee{ private String name; private int age; private double salary; public Employee(String name, int age, double salary) { super(); this.name = name; this.age = age; this.salary = salary; } public Employee() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public void show() { System.out.println(toString()); } @Override public String toString() { return "name=" + name + ", age=" + age; } //提供一個方法返回基本年薪,子類能夠在此基礎上進行修改 public double yearSalary() { return salary * 12; } } //工人類(Worker) class Worker extends Employee{ public Worker(String name, int age, double salary){ super(name,age,salary); } @Override public void show() { System.out.println("Worker : " + super.toString() + ", salary: " + yearSalary()); } } //農民類(peasant) class Peasant extends Employee{ public Peasant(String name, int age, double salary){ super(name,age,salary); } @Override public void show() { System.out.println("Peasant : " + super.toString() + ", salary: " + yearSalary()); } } //服務生類(waiter) class Waiter extends Employee{ public Waiter(String name, int age, double salary){ super(name,age,salary); } @Override public void show() { System.out.println("Waiter : " + super.toString() + ", salary: " + yearSalary()); } } //教師類(teacher),教師除基本工資外,還有課酬(元/天)。 class Teacher extends Employee{ private double dollars; //課酬 private int workDays; //工做天數 public Teacher(String name, int age, double salary, double dollars, int workDays) { super(name, age, salary); this.dollars = dollars; this.workDays = workDays; } public int getWorkDays() { return workDays; } public void setWorkDays(int workDays) { this.workDays = workDays; } public double getDollars() { return dollars; } public void setDollars(double dollars) { this.dollars = dollars; } @Override public void show() { System.out.println( "Teacher : " + super.toString() + ", salary: " + (yearSalary()+dollars*workDays)); } } //科學家類(scientist), 科學家除基本工資外,還有年終獎(bonus)。 class Scientist extends Employee{ private double bonus; public Scientist(String name, int age, double salary, double bonus) { super(name, age, salary); this.bonus = bonus; } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } @Override public void show() { System.out.println("Scientist : " + super.toString() + ",salary:" + (yearSalary()+bonus)); } }
定義一個圖形類Picture,再定義Picture的兩個子類:圓形類Circle、矩形類Rect。 要求: 圖像類:有求周長和求面積和顯示圖形信息的功能。 圓形類:包含圓心和半徑,重寫求周長和求面積的方法。 矩形類:包含長和寬,重寫求周長和求面積的方法。
package polymorphic_HomeWork; public class WorkTest_03 { public static void main(String[] args) { Picture circle = new Circle("2,2",3.1); Picture rect = new Rect(6.4,5.3); circle.show(); rect.show(); } } //圖像類:有求周長和求面積和顯示圖形信息的功能。 class Picture{ private double sideLength; //邊長 private int sideCount; //邊數 public Picture(double sideLength, int sideCount) { super(); this.sideLength = sideLength; this.sideCount = sideCount; } public Picture() { super(); } public double getSideLength() { return sideLength; } public void setSideLength(double sideLength) { this.sideLength = sideLength; } public int getSideCount() { return sideCount; } public void setSideCount(int sideCount) { this.sideCount = sideCount; } public double perimeter(){ //求周長 return sideCount * sideLength; } public double area() { //求面積 return 0.0; } public void show() { System.out.printf("周長(perimeter):%.2f\t面積(area):%.2f\n",perimeter(),area()); } } //圓形類Circle:包含圓心和半徑,重寫求周長和求面積的方法。 class Circle extends Picture{ private String centerCircle; //圓心:"X,Y" private double radius; //半徑 public Circle(String centerCircle, double radius) { super(); this.centerCircle = centerCircle; this.radius = radius; } public String getCenterCircle() { return centerCircle; } public void setCenterCircle(String centerCircle) { this.centerCircle = centerCircle; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } @Override public double perimeter(){ //求周長 return 2 * radius * Math.PI; } @Override public double area() { //求面積 return radius * radius * Math.PI; } @Override public void show() { System.out.printf("Circle:圓心爲 :%S ",centerCircle); super.show(); } } //矩形類Rect:包含長和寬,重寫求周長和求面積的方法。 class Rect extends Picture{ private double length;//長 private double wide;//寬 public Rect( double length, double wide) { super(); this.length = length; this.wide = wide; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWide() { return wide; } public void setWide(double wide) { this.wide = wide; } @Override public double perimeter(){ //求周長 return 2 * length + 2 * wide; } @Override public double area() { //求面積 return length * wide; } @Override public void show() { System.out.print("Rect: "); super.show(); } }