今天長沙中公優(yōu)就業(yè)java培訓(xùn)機(jī)構(gòu)小編為大家分享java繼承的實(shí)例,希望通過此文大家對(duì)java繼承能夠有更深的理解,下面就隨小編一起來了解一下java繼承的實(shí)例。
實(shí)例需求:實(shí)現(xiàn)一個(gè)名為Person的類和它的子類Employee,Manager是Employee的子類,設(shè)計(jì)一 個(gè)類Add()用于漲工資,普通員工一次能漲10%,經(jīng)理能漲20%。
具體要求如下:
(1)Person類中的屬性有:姓名name String(類型),地址address(String類型),定義該類的構(gòu)造方法;
(2)Employee 類中的屬性有:工號(hào)ID(String類型),工資wage(double類型),工齡(int 型),定義該類的構(gòu)造方法;
(3)Manager類中的屬性有:級(jí)別level(String類型)定義該類的構(gòu)造方法;
?。?)編寫一個(gè)測(cè)試類,產(chǎn)生一個(gè)員工和一個(gè)經(jīng)理,給該員工和經(jīng)理漲工資。
1、利用繼承的特性
當(dāng)子類沒有相應(yīng)的成員方法時(shí),調(diào)用父類的同名方法。
Person.java
package person_employee;
public class Person {
private String name = "";
private String address = "";
//定義構(gòu)造方法
public Person(String name, String address){
this.name = name;
this.address = address;
}
}
Employee.java
package person_employee;
// 繼承Person類
public class Employee extends Person {
private String ID = "";
private double wage = 0;
private int age = 0;
public Employee(String name, String address, String ID, double wage, int age){
super(name, address); //調(diào)用父類的構(gòu)造方法
this.ID = ID;
this.wage = wage;
this.age = age;
}
public double add(String position){// 判斷職位
if(position == "0"){
wage = wage*1.2;
}
else{
wage = wage*1.1;
}
return wage;
}
//設(shè)置get/set方法
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
}
Manager.java
package person_employee;
//繼承Employee類
public class Manager extends Employee{
private String level = ""; //1為普通員工,0為經(jīng)理
public Manager(String name, String address, String ID, double wage, int age, String level){
super(name, address, ID, wage, age);
this.level = level;
}
//設(shè)置get/set方法
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
Test.java
package person_employee;
public class Test {
public static void main(String[] args) {
// 新建對(duì)象
Manager normal = new Manager("wsl", "jit", "12", 1000, 2, "1");
Manager manager = new Manager("ctl", "jitt", "123", 10000, 10, "0");
// 傳入職位,Manager類型的對(duì)象沒有add()方法,所以自動(dòng)搜尋其父類,調(diào)用add()方法
normal.add(normal.getLevel());
manager.add(manager.getLevel());
System.out.println("normal wage is:"+normal.getWage());
System.out.println("manager wage is:"+manager.getWage());
}
}
打印結(jié)果:
2、定義抽象類
定義抽象類,在子類中實(shí)現(xiàn)不同的抽象方法。
Person.java
package person_employee;
public class Person {
private String name = "";
private String address = "";
//定義構(gòu)造方法
public Person(String name, String address){
this.name = name;
this.address = address;
}
}
Employee.java
package person_employee;
public abstract class Employee extends Person {
private String ID = "";
private double wage = 0;
private int age = 0;
public Employee(String name, String address, String ID, double wage, int age){
super(name, address);
this.ID = ID;
this.wage = wage;
this.age = age;
}
//定義抽象方法
public abstract void add(String position);
//設(shè)置get/set方法
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
}
Manager.java
package person_employee;
public class Manager extends Employee{
private String level = "";
public Manager(String name, String address, String ID, double wage, int age, String level){
super(name, address, ID, wage, age);
this.level = level;
}
//實(shí)現(xiàn)抽象方法
public void add(String position){
double wage = super.getWage();
if(position == "0"){
super.setWage(wage*1.2);
}
else{
super.setWage(wage*1.1);;
}
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
Test.java
package person_employee;
public class Test {
public static void main(String[] args) {
Manager normal = new Manager("wsl", "jit", "12", 1000, 2, "1");
Manager manager = new Manager("ctl", "jitt", "123", 10000, 10, "0");
normal.add(normal.getLevel());
manager.add(manager.getLevel());
System.out.println("normal wage is:"+normal.getWage());
System.out.println("manager wage is:"+manager.getWage());
}
}
打印結(jié)果:
3、在2的基礎(chǔ)上利用多態(tài)實(shí)現(xiàn)方法的重載
Manager.java
package person_employee;
public class Manager extends Employee{
private String level = "";
public Manager(String name, String address, String ID, double wage, int age, String level){
super(name, address, ID, wage, age);
this.level = level;
}
//實(shí)現(xiàn)抽象方法
public void add(){
double wage = super.getWage();
super.setWage(wage*1.1);
}
public void add(String position){
double wage = super.getWage();
super.setWage(wage*1.2);
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
Test.java
package person_employee;
public class Test {
public static void main(String[] args) {
Manager normal = new Manager("wsl", "jit", "12", 1000, 2, "1");
Manager manager = new Manager("ctl", "jitt", "123", 10000, 10, "0");
normal.add();
manager.add(manager.getLevel());
System.out.println("normal wage is:"+normal.getWage());
System.out.println("manager wage is:"+manager.getWage());
}
}
打印結(jié)果:
以上就是長沙中公優(yōu)就業(yè)java培訓(xùn)機(jī)構(gòu)小編分享的“java繼承的實(shí)例”,希望對(duì)大家有幫助,更多java最新資訊請(qǐng)繼續(xù)關(guān)注長沙中公優(yōu)就業(yè)java培訓(xùn)機(jī)構(gòu)官網(wǎng),每天會(huì)有精彩內(nèi)容分享與你。
相關(guān)免費(fèi)視頻教程推薦
java開發(fā)基礎(chǔ)教程下載——編寫繼承代碼:http://www.bjpowernode.com/xiazai/2615.html