java學習整理四


1.繼承多態綜合練習1

1)建立一個Java抽象類Drink,應當:

聲明一個抽象方法taste(),該方法負責輸出飲料的味道;

聲明int型常量來代表不同的飲料類型(咖啡、啤酒、牛奶),如:

聲明靜態方法getDrink(int drinkType),根據傳入的參數創建不同的飲料對象,並返回該對象,建議使用switch語句。

2)建立Drink的子類:

分別建立Drink的子類:Coffee(咖啡類),Beer(啤酒類),Milk(牛奶類);

實現taste()方法,要求在控制台打印各自的味道特征。

3)建立Test測試類,測試以上內容的正確性

編寫main方法,輸入要新建的飲料類型。

調用Drink類的getDrink方法,獲得相應的飲料對象。然后調用該飲料對象的taste()方法,輸出該飲料的味道。

②編譯程序,並運行。

要求:測試類放在包名為com.sy4.exe02.test包中,其它類放在com.sy4.exa02包中。

(知識點:類的定義,抽象類,繼承,多態)

 

package Exp04_01_20170566134;

public  class Beer extends Drink{
    public void taste(){
        System.out.println("我是啤酒");
    }
}
Beer extends Drink
package Exp04_01_20170566134;

public class Coffee extends Drink{
    public void taste(){
        System.out.println("我是咖啡");
    }
}
Coffee extends Drink
package Exp04_01_20170566134;

public abstract class Drink {
    int cf;
    int pj;
    int nn;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
    public abstract void taste();
    
    public static Drink getDrink(int drinkType){
        switch (drinkType) {
        case 1:    
            return new Coffee();
        case 2:    
            return new Beer();        
        case 3:    
            return new Milk();

        }
        return null;
    }
}
abstract class Drink
package Exp04_01_20170566134;

public class Milk extends Drink{
    public void taste(){
        System.out.println("我是牛奶");
    }
}
Milk extends Drink
package Exp04_sy4_exe.test;

import java.util.Scanner;

import Exp04_01_20170566134.Drink;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("飲料類型 1:咖啡 2:啤酒 3:牛奶 4:退出");
        int choice = sc.nextInt();
        while (true) {
            switch (choice) {
            case 1:
                Drink cf = Drink.getDrink(choice);
                cf.taste();
                break;
            case 2:
                Drink pj = Drink.getDrink(choice);
                pj.taste();
                break;
            case 3:
                Drink nn = Drink.getDrink(choice);
                nn.taste();
                break;
            default:
                System.out.println("退出成功");
                System.exit(0);
                break;
            }
            choice = sc.nextInt();
        }
        
    }

}
test類

 

2.繼承多態綜合應用練習2

1)定義一個抽象的People類,有姓名(name),年齡(age),性別(sex)等成員變量,要求成員變量的訪問修飾符為private,通過getXxx()setXxx()方法對各變量進行讀寫。聲明具有一個抽象的role()方法,該方法不返回任何值,用於輸出人的身份類型,同時至少定義兩個構造方法。

2)定義一個抽象類Employee類,該類繼承People類,該類具有People類的所有成員,並新增雇員底薪薪水(salary)和職工編號(empid)成員變量。同樣要有至少兩個構造方法,要體現出thissuper的幾種用法。聲明具有一個抽象的getSalary()方法,該方法返回float值,用於返回員工薪水。

3)定義SalariedEmployee類,它是Employee的子類,拿固定工資的員工,他的薪水就是底薪。重寫rolegetSalary方法。

4)定義HourlyEmployee 類,它是Employee 的子類,按小時拿工資的員工,每月工作超出160 小時的部分按照1.5 倍工資發放。新增屬性:每小時的工資(hourSalary)、每月工作的小時數(hourWork)。重寫rolegetSalary方法。

5)定義SalesEmployee類,它是Employee 的子類,銷售人員,工資由月銷售額和提成率決定。新增屬性:月銷售額(saleMoney)、提成率(rate)

6)定義一個類Company,在該類中寫一個方法print(Employee e),調用該方法可以打印出某個員工的工資數額以及該員工的身份類型,寫一個測試類CompanyTestmain方法,把若干各種類型的員工放在一個Employee 數組里,並調用print方法輸出數組中每個員工當月的工資。

要求:測試類放在包名為com.sy4.exe02.test包中,其它類放在com.sy4.exa02包中。

(知識點:類的定義,抽象類,繼承,多態)

 

package Exp04_02_20170566134;

public class Company {
    
    
    public  void print(Employee e){
        System.out.println("身份是:");
        e.role();
        System.out.println(e.getSalary());
    }
}
Company
package Exp04_02_20170566134;

public abstract class Employee extends People{
    public int salary;
    String empid;
    
    public Employee(String name, String age, String sex,int salary, String empid) {
        super(name,age,sex);
        this.salary = salary;
        this.empid = empid;
    }
    
    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }

    public abstract void role();
    public abstract float getSalary(); //返回float值,用於返回員工薪水
}
Employee extends People
package Exp04_02_20170566134;

public class HourlyEmployee extends Employee{
    int hourSalary;
    int hourWork;
    int salary;
    public  void role(){
        System.out.println("小時制員工");
    }
    public  float getSalary(){
        if (hourWork >160) {
            salary =(int)(hourSalary*160+(hourWork-160)*1.5*hourSalary);
        } else {
            salary = hourSalary*hourWork;
        } 
        return salary;
    }
    
    public HourlyEmployee() {
        super();
        // TODO Auto-generated constructor stub
    }
    public HourlyEmployee(String name, String age, String sex, int salary,
            String empid,int hourSalary,int hourWork) {
        super(name, age, sex, salary, empid);
        this.hourSalary=hourSalary;
        this.hourWork=hourWork;
        // TODO Auto-generated constructor stub
    }
    public int getHourSalary() {
        return hourSalary;
    }
    public void setHourSalary(int hourSalary) {
        this.hourSalary = hourSalary;
    }
    public int getHourWork() {
        return hourWork;
    }
    public void setHourWork(int hourWork) {
        this.hourWork = hourWork;
    }
}
HourlyEmployee extends Employee
package Exp04_02_20170566134;

public abstract class People {
    private String name;
    private String age;
    private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public People(String name, String age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public People() {
        super();
        // TODO Auto-generated constructor stub
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public abstract void role();  //輸出人的身份類型
    
}
abstract class People
package Exp04_02_20170566134;

public class SalariedEmployee extends Employee{
    
    
    public  void role(){
        System.out.println("拿固定工資的員工");
    }
    public  float getSalary(){
        
        return super.salary;
    }
    public SalariedEmployee() {
        super();
        // TODO Auto-generated constructor stub
    }
    public SalariedEmployee(String name, String age, String sex, int salary,
            String empid) {
        super(name, age, sex, salary, empid);
        // TODO Auto-generated constructor stub
    }
}
SalariedEmployee extends Employee
package Exp04_02_20170566134;

public class SalesEmployee extends Employee{
    int saleMoney;
    float rate;
    float salary=0;
    public  void role(){
        System.out.println("拿有提成的銷售員工");
    }
    public  float getSalary(){
        salary =saleMoney*rate;
        return salary;
    }
    
    public SalesEmployee() {
        super();
        // TODO Auto-generated constructor stub
    }
    public SalesEmployee(String name, String age, String sex, int salary,
            String empid,int saleMoney,float rate) {
        super(name, age, sex, salary, empid);
        this.saleMoney=saleMoney;
        this.rate=rate;
        // TODO Auto-generated constructor stub
    }
    public int getSaleMoney() {
        return saleMoney;
    }
    public void setSaleMoney(int saleMoney) {
        this.saleMoney = saleMoney;
    }
    public float getRate() {
        return rate;
    }
    public void setRate(float rate) {
        this.rate = rate;
    }
}
SalesEmployee extends Employee
package Exp04_sy4_exe.test;

import java.lang.reflect.Array;

import Exp04_02_20170566134.Employee;
import Exp04_02_20170566134.HourlyEmployee;
import Exp04_02_20170566134.SalariedEmployee;
import Exp04_02_20170566134.SalesEmployee;

public class CompanyTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Employee[] per= new Employee[10];
        SalariedEmployee p1 =new SalariedEmployee("張三","18","男",1000,"1001");
        HourlyEmployee p2 =new HourlyEmployee("李四","20","男",1000,"1002",10,170);
        HourlyEmployee p3 =new HourlyEmployee("李四媳婦","21","女",1000,"1003",10,150);
        SalesEmployee p4 =new SalesEmployee("王五","18","男",1000,"1004",10000,(float)0.2);
        per[0]=p1;
        per[1]=p2;
        per[2]=p3;
        per[3]=p4;
        for (int i = 0; i < per.length; i++) {
            
                try {
                    if(per[i].salary>0){
                        System.out.print("員工的姓名是:"+per[i].getName()+" ,類型是:");
                        per[i].role();
                        System.out.println("工資薪水為:"+per[i].getSalary());        
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    break;
                }
            
                
        }
            
        
        
    }
}
CompanyTest

 

3.繼承多態綜合應用練習3

1)定義一個駕駛員抽象類Driver,它有String sex(性別)和int age(年齡)兩個成員屬性,還有一個抽象的成員方法drives(Vehicle v),並從它派生出女駕駛員FemaleDriver和男駕駛員MaleDriver兩個子類。

2)定義一個抽象類Vehicle,它有String vtype(車型)和String vcode(車牌號碼)兩個成員屬性,還有一個抽象的成員方法drivedByDriver(Driver who),並從它派生出BusCar兩個子類。

3)定義一個測試類DriverTest,在其main方法中先創建fdmd兩個男女Driver對象,然后創建vbvc兩個汽車類Vehicle對象。

要求:

1)將以上所有類均放置到com.sy4.exa03包中,然后分別通過調用drives()drivedByDriver()成員方法輸出如下字符串:

一個男司機駕駛一輛小轎車。

一個女司機駕駛一輛公交車。

2)在com.sy4.exa03.test包中新建一個DriverTest測試類。在該類的main方法中先創建20Vehicle對象(隨機生成車牌號,車牌號范圍為1~1000),並規定只有雙號車輛能夠通過,輸出所有車輛的通過情況。

要求:測試類放在包名為com.sy4.exe03.test包中,其它類放在com.sy4.exa03包中。

 

package Exp04_03_20170566134;

public class Bus extends Vehicle{

    @Override
    public void drivedByDriver(Driver who) {
        // TODO Auto-generated method stub
        System.out.println("一輛公交車");
    }

}
Bus extends Vehicle
package Exp04_03_20170566134;

public class Car extends Vehicle{

    public Car(String vtype, int num) {
        // TODO Auto-generated constructor stub
        super(vtype,num);
    }

    @Override
    public void drivedByDriver(Driver who) {
        // TODO Auto-generated method stub
        System.out.println("一輛小轎車");
    }

    public Car() {
        super();
        // TODO Auto-generated constructor stub
    }
    
}
Car extends Vehicle
package Exp04_03_20170566134;

public abstract class Driver {
    String sex;
    int age;
    public abstract void drives(Vehicle v);
    
    public Driver(String sex, int age) {
        super();
        this.sex = sex;
        this.age = age;
    }
}
abstract class Driver
package Exp04_03_20170566134;

public class FemaleDriver extends Driver{

    public FemaleDriver(String sex, int age) {
        super(sex, age);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void drives(Vehicle v) {
        // TODO Auto-generated method stub
        System.out.print("一個"+super.sex+"司機駕駛");
    }

}
FemaleDriver extends Driver
package Exp04_03_20170566134;

public class MaleDriver extends Driver{

    public MaleDriver(String sex, int age) {
        super(sex, age);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void drives(Vehicle v) {
        // TODO Auto-generated method stub
        System.out.print("一個"+super.sex+"司機駕駛");
    }

}
MaleDriver extends Driver
package Exp04_03_20170566134;

public abstract class Vehicle {
    String vtype;
    int vcode;
    
    public abstract void drivedByDriver(Driver who);
    
    public Vehicle(String vtype, int vcode) {
        super();
        this.vtype = vtype;
        this.vcode = vcode;
    }

    public String getVtype() {
        return vtype;
    }

    public void setVtype(String vtype) {
        this.vtype = vtype;
    }

    public int getVcode() {
        return vcode;
    }

    public void setVcode(int vcode) {
        this.vcode = vcode;
    }

    public Vehicle() {
        super();
        // TODO Auto-generated constructor stub
    }
}
abstract class Vehicle
package Exp04_sy4_exe.test;

import Exp04_03_20170566134.Bus;
import Exp04_03_20170566134.Car;
import Exp04_03_20170566134.Driver;
import Exp04_03_20170566134.FemaleDriver;
import Exp04_03_20170566134.MaleDriver;
import Exp04_03_20170566134.Vehicle;

public class DriverTest {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Driver fd =new FemaleDriver("女",18);
        Driver md =new MaleDriver("男",20);
        
        Vehicle vb =new Bus();
        Vehicle vc =new Car();
        
        md.drives(vc);
        vc.drivedByDriver(md);
        
        fd.drives(vb);
        vb.drivedByDriver(fd);
        Vehicle[] veh =new Vehicle[20];
        int num;
        for (int i = 0; i < veh.length; i++) {
            num=(int)(Math.random()*1000);
            
            veh[i]=new Car(num%2==0?"雙":"單",num);
            
            //veh[i]=new Car("雙",(int)num);
        }
        for (int i = 0; i < veh.length; i++) {
            
            
            if (veh[i].getVtype()=="雙") {
                System.out.println("車牌類型為"+veh[i].getVtype()+"數類型,車牌號為:"+veh[i].getVcode());
            }
        }
        
    }
}
DriverTest

 

4、設計一個具有各種功能門的系統。

1)定義一張抽象的門Door,對於門來說,擁有開門openDoor()和關門closeDoor()的操作,定義抽象方法String doorType()返回門的類型(如防火門、防盜門等)

2)定義具有防盜--theftproof()、防水--waterproof()、防彈--bulletproof()、防火等功能的接口

3)創建防盜門類、防水門類、防彈門類、防盜和防水門類

4)定義一個測試類DoorTest,在其main方法中創建各種門的對象,輸出門的功能及門的類型。

(知識點:抽象類,接口,繼承,多態)

要求:測試類放在包名為com.sy4.exe04.test包中,其它類放在com.sy4.exa04包中。

 

package Exp04_04_20170566134;

public class bullet extends Door{

    @Override
    public String doorType() {
        // TODO Auto-generated method stub
        System.out.print("防彈類型門, ");
        return null;
    }

    @Override
    public void theftproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防盜");
    }

    @Override
    public void waterproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防水");
    }

    @Override
    public void bulletproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防彈");
    }

    @Override
    public void huoproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防火");
    }
    
}
bullet extends Door

 

package Exp04_04_20170566134;

public abstract class Door implements Interface{
    public void  openDoor() {
        System.out.println("開門");
    }
    public void closeDoor() {
        System.out.println("關門");
    }
    public abstract String doorType();
}
abstract class Door implements Interface
package Exp04_04_20170566134;

public class huo extends Door{
    
    @Override
    public String doorType() {
        // TODO Auto-generated method stub
        System.out.print("防火類型門, ");
        return null;
    }
    @Override
    public void theftproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防盜");
    }

    @Override
    public void waterproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防水");
    }

    @Override
    public void bulletproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防彈");
    }

    @Override
    public void huoproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防火");
    }
}
huo extends Door
package Exp04_04_20170566134;

public interface Interface {
    public void theftproof();
    
    public void waterproof();
    
    public void bulletproof();
    
    public void huoproof();
}
interface Interface
package Exp04_04_20170566134;

public class theft extends Door{

    @Override
    public String doorType() {
        // TODO Auto-generated method stub
        System.out.print("防盜類型門, ");
        return null;
    }
    @Override
    public void theftproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防盜");
    }

    @Override
    public void waterproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防水");
    }

    @Override
    public void bulletproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防彈");
    }

    @Override
    public void huoproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防火");
    }
}
theft extends Door
package Exp04_04_20170566134;

public class water extends Door{

    @Override
    public String doorType() {
        // TODO Auto-generated method stub
        System.out.print("防水類型門, ");
        return null;
    }
    @Override
    public void theftproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防盜");
    }

    @Override
    public void waterproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防水");
    }

    @Override
    public void bulletproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防彈");
    }

    @Override
    public void huoproof() {
        // TODO Auto-generated method stub
        System.out.println("我的功能是防火");
    }
}
water extends Door
package Exp04_sy4_exe.test;

import Exp04_04_20170566134.bullet;
import Exp04_04_20170566134.huo;
import Exp04_04_20170566134.theft;
import Exp04_04_20170566134.water;

public class DoorTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
            huo huo =new huo();
            huo.doorType();
            huo.huoproof();
            
            water water =new water();
            water.doorType();
            water.huoproof();
            
            bullet bullet =new bullet();
            bullet.doorType();
            bullet.huoproof();
            
            theft theft =new theft();
            theft.doorType();
            theft.huoproof();

    }

}
DoorTest

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM