编程语言
首页 > 编程语言> > Java面对对象程序设计实验之“继承与多态”

Java面对对象程序设计实验之“继承与多态”

作者:互联网

目录

☀️| 一、实验目的

☀️| 二、实验内容

❀1. 继承多态综合练习1

❀2. 继承多态综合应用练习2

❀3. 继承多态综合应用练习3

❀4. 设计一个具有各种功能门的系统

☀️| 三、实验源代码

⭐️1. 源代码:

⭐️2. 源代码:

⭐️3. 源代码:

⭐️4. 源代码:

☀️| 四、实验总结


☀️| 一、实验目的

1.掌握Java语言中继承和多态的相关概念

2.掌握Java程序设计中继承和多态机制的使用

3.掌握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包中。

(知识点:类的定义,抽象类,继承,多态)

实验结果:

❀2. 继承多态综合应用练习2

(1)定义一个抽象的People类,有姓名(name),年龄(age),性别(sex)等成员变量,要求成员变量的访问修饰符为private,通过getXxx()和setXxx()方法对各变量进行读写。声明具有一个抽象的role()方法,该方法不返回任何值,用于输出人的身份类型,同时至少定义两个构造方法。

(2)定义一个抽象类Employee类,该类继承People类,该类具有People类的所有成员,并新增雇员底薪薪水(salary)和职工编号(empid)成员变量。同样要有至少两个构造方法,要体现出this和super的几种用法。声明具有一个抽象的getSalary()方法,该方法返回float值,用于返回员工薪水。

(3)定义SalariedEmployee类,它是Employee的子类,拿固定工资的员工,他的薪水就是底薪。重写role和getSalary方法。

(4)定义HourlyEmployee 类,它是Employee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。新增属性:每小时的工资(hourSalary)、每月工作的小时数(hourWork)。重写role和getSalary方法。

(5)定义SalesEmployee类,它是Employee 的子类,销售人员,工资由月销售额和提成率决定。新增属性:月销售额(saleMoney)、提成率(rate)

(6)定义一个类Company,在该类中写一个方法print(Employee e),调用该方法可以打印出某个员工的工资数额以及该员工的身份类型,写一个测试类CompanyTest在main方法,把若干各种类型的员工放在一个Employee 数组里,并调用print方法输出数组中每个员工当月的工资。

要求:测试类放在包名为com.sy4.exe02.test包中,其它类放在com.sy4.exa02包中。

(知识点:类的定义,抽象类,继承,多态)

实验结果:

❀3. 继承多态综合应用练习3

(1)定义一个驾驶员抽象类Driver,它有String sex(性别)和int age(年龄)两个成员属性,还有一个抽象的成员方法drives(Vehicle v),并从它派生出女驾驶员FemaleDriver和男驾驶员MaleDriver两个子类。

(2)定义一个抽象类Vehicle,它有String vtype(车型)和String vcode(车牌号码)两个成员属性,还有一个抽象的成员方法drivedByDriver(Driver who),并从它派生出Bus和Car两个子类。

(3)定义一个测试类DriverTest,在其main方法中先创建fd和md两个男女Driver对象,然后创建vb和vc两个汽车类Vehicle对象。

要求:

(1)将以上所有类均放置到com.sy4.exa03包中,然后分别通过调用drives()和drivedByDriver()成员方法输出如下字符串:

一个男司机驾驶一辆小轿车。

一个女司机驾驶一辆公交车。

(2)在com.sy4.exa03.test包中新建一个DriverTest测试类。在该类的main方法中先创建20个Vehicle对象(随机生成车牌号,车牌号范围为1~1000),并规定只有双号车辆能够通过,输出所有车辆的通过情况。

要求:测试类放在包名为com.sy4.exe03.test包中,其它类放在com.sy4.exa03包中。

实验结果:

❀4. 设计一个具有各种功能门的系统

(1)定义一张抽象的门Door,对于门来说,拥有开门openDoor()和关门closeDoor()的操作,定义抽象方法String doorType()返回门的类型(如防火门、防盗门等)

(2)定义具有防盗--theftproof()、防水--waterproof()、防弹--bulletproof()、防火等功能的接口

(3)创建防盗门类、防水门类、防弹门类、防盗和防水门类

(4)定义一个测试类DoorTest,在其main方法中创建各种门的对象,输出门的功能及

门的类型。

(知识点:抽象类,接口,继承,多态)

要求:测试类放在包名为com.sy4.exe04.test包中,其它类放在com.sy4.exa04包中。

实验结果:


☀️| 三、实验源代码

⭐️1. 源代码:

package EXPS.Exp04.com.sy4.exa01.test;
import EXPS.Exp04.com.sy4.exa01.Drink;
import java.util.Scanner;
/**
 * 班级:19软嵌2
 * 学号:20190507223
 * 姓名:夏旭
 * 实验时间:2020-4-27
 * 本程序的功能是:实现Drink类
 */
public class Exp04_01_20190507223 {
    public static void main(String[] args) {
        System.out.println("输入要新建的饮料类型  1咖啡,2啤酒,3牛奶");
        Scanner sc=new Scanner(System.in);
        int type=sc.nextInt();
        Drink drink=Drink.getDrink(type);
        if(drink==null){
            System.out.println("非法输入,程序将结束");
            return;
        }
        drink.taste();
    }
}

package EXPS.Exp04.com.sy4.exa01;

public class Beer  extends Drink {
    public void taste(){
        System.out.println("辣的");
    }
}

package EXPS.Exp04.com.sy4.exa01;

public class Coffee extends Drink {
    public void taste(){
        System.out.println("苦的");

    }
}

package EXPS.Exp04.com.sy4.exa01;

public  abstract class Drink {
    static final int cf=1,pj=2,nn=3;//1.咖啡 2.啤酒 3.牛奶
    protected Drink() {


    }

    public abstract void taste();

    public static Drink getDrink(int drinkType){
        Drink res=null;
        switch (drinkType){
            case cf:
                res=new Coffee();
                break;
            case pj:
                res=new Beer();
                break;
            case nn:
                res=new Milk();
                break;
        }
        //this.drinkType=drinkType;
        return res;
    }
}

package EXPS.Exp04.com.sy4.exa01;

public class Milk extends Drink {
    public void taste(){
        System.out.println("甜的");
    }
}

⭐️2. 源代码:

package EXPS.Exp04.com.sy4.exa02.test;
import EXPS.Exp04.com.sy4.exa02.*;
/**
 * 班级:19软嵌2
 * 学号:20190507223
 * 姓名:夏旭
 * 实验时间:2020-4-27
 * 本程序的功能是:实现employ
 */
public class CompanyTest {
    public static void main(String[] args) {
        Employee [] list={new HourlyEmployee(1,100),new SalariedEmployee(),new SalesEmployee(2,2),new HourlyEmployee(1,170)
        };
        Company company=new Company();

        for(int i=0;i<list.length;i++){
            company.print(list[i]);
            System.out.println("");
        }
    }
}

package EXPS.Exp04.com.sy4.exa02;

public class Company {
    public void print(Employee e){
        System.out.println("工资:"+e.getSalary());
        e.role();
    }
}

package EXPS.Exp04.com.sy4.exa02;

public abstract class Employee extends People {
    private String name;
    private int age;
    private String sex;
    private float salary=111;
    private String empid;

    public Employee() {


    }

    public Employee(String name, int age, String sex, float salary, String empid) {
        super(name, age, sex);
        this.salary = salary;
        this.empid = empid;
    }
    public abstract float getSalary();
    public float QueryBaseSalary(){
        return this.salary;
    }

}

package EXPS.Exp04.com.sy4.exa02;

public class HourlyEmployee extends Employee {
    public float hourSalary;
    public int hourWork;

    public HourlyEmployee(float hourSalary, int hourWork) {
        this.hourSalary = hourSalary;
        this.hourWork = hourWork;
    }

    public HourlyEmployee() {
    }

    @Override
    public float getSalary() {
        return hourWork>160? (float) (hourWork * hourSalary + (hourWork - 160) * 0.5 * hourSalary) : hourWork*hourSalary;
    }

    @Override
    public void role() {
        System.out.println("这是HourlyEmployee");

    }
}

package EXPS.Exp04.com.sy4.exa02;

public abstract class  People {
    private String name;
    private int age;
    private String sex;


    public People(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public People() {


    }

    public abstract void  role();

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}



package EXPS.Exp04.com.sy4.exa02;

public class SalariedEmployee extends Employee {
    @Override
    public float getSalary() {
        return super.QueryBaseSalary();
    }

    @Override
    public void role() {
        System.out.println("这是SalariedEmployee");
    }
}

package EXPS.Exp04.com.sy4.exa02;

public class SalesEmployee extends  Employee {
    public float saleMoney;
    public float rate;

    public SalesEmployee() {

    }

    public SalesEmployee(float saleMoney, float rate) {
        this.saleMoney = saleMoney;
        this.rate = rate;
    }

    @Override
    public float getSalary() {
        return saleMoney*rate;
    }

    @Override
    public void role() {
        System.out.println("这是SalesEmployee");
    }
}

⭐️3. 源代码:

package EXPS.Exp04.com.sy4.exa03.test;
import EXPS.Exp04.com.sy4.exa03.Car;
import EXPS.Exp04.com.sy4.exa03.Vehicle;
import java.util.Random;
/**
 * 班级:19软嵌2
 * 学号:20190507223
 * 姓名:夏旭
 * 实验时间:2020-4-27
 * 本程序的功能是:实现Driver类
 */
public class DriverTest {
    public static void main(String[] args) {
        Random random=new Random();
        Vehicle [] list=new Vehicle[20];
        for(int i=0;i<list.length;i++){
            list[i]=new Car(String.valueOf(random.nextInt(999)+1));
        }
        for(int i=0;i< list.length;i++){
            int num=Integer.parseInt(list[i].vcode);
            System.out.println("第"+(i+1)+"辆车"+" 车牌号:"+list[i].vcode+ (num%2==0?"  通过":" 未通过") );
        }
    }
}

package EXPS.Exp04.com.sy4.exa03;

public class Bus extends Vehicle {
    @Override
    public void drivedByDriver(Driver who) {
        who.drives();
        System.out.print("一辆公交车");
    }
}

package EXPS.Exp04.com.sy4.exa03;

public class Car  extends Vehicle{
    public Car(String vcode) {
        super(vcode);
    }
    public Car(){}

    @Override
    public void drivedByDriver(Driver who) {
        who.drives();
        System.out.print("一辆小轿车");
    }
}

package EXPS.Exp04.com.sy4.exa03;

public abstract class Driver {
    public String sex;
    public int age;
    public abstract void drives();
}

package EXPS.Exp04.com.sy4.exa03;

public class DriverTest {
    public static void main(String[] args) {
        FemaleDriver fd=new FemaleDriver();
        MaleDriver md=new MaleDriver();
        Bus vb=new Bus();
        Car vc=new Car();
        vc.drivedByDriver(md);
        System.out.println("。");
        System.out.println("------------");
        vb.drivedByDriver(fd);
        System.out.println("。");
    }
}



package EXPS.Exp04.com.sy4.exa03;

import EXPS.Exp04.com.sy4.exa03.*;
public class FemaleDriver extends Driver{
    @Override
    public void drives() {
        System.out.print("一个女司机驾驶");
    }
}

package EXPS.Exp04.com.sy4.exa03;

public class MaleDriver extends Driver {
    @Override
    public void drives(){
        System.out.print("一个男司机驾驶");
    }
}

package EXPS.Exp04.com.sy4.exa03;

public abstract class Vehicle {
    public String type;
    public String vcode;

    public Vehicle() {
    }

    public Vehicle(String type, String vcode) {
        this.type = type;
        this.vcode = vcode;
    }

    public Vehicle(String vcode) {
        this.vcode = vcode;
    }

    public abstract void drivedByDriver(Driver who);
}

⭐️4. 源代码:

package EXPS.Exp04.com.sy4.exa04.test;
import EXPS.Exp04.com.sy4.exa04.*;
import java.util.Scanner;
/**
 * 班级:19软嵌2
 * 学号:20190507223
 * 姓名:夏旭
 * 实验时间:2020-4-27
 * 本程序的功能是:实现Door类并实现接口
 */
public class DoorTest {
    public static void main(String[] args) {
        bulletproof bf=new bulletproof();
        waterproof wf=new waterproof();
        theftproof tf=new theftproof();
        waterandbulletproof wbf=new waterandbulletproof();
        Scanner sc=new Scanner(System.in);
        System.out.println("输入要查询的门 1.防盗门 2.防水门 3.防弹门 4.防弹防水门");
        int type=sc.nextInt();
        switch (type){
            case 1:
                tf.theftproof();
                break;
            case 2:
                wf.waterproof();
                break;
            case 3:
                bf.bulletproof();
                break;
            case 4:
                wbf.waterproof();
                wbf.bulletproof();
                break;

        }
    }
}

package EXPS.Exp04.com.sy4.exa04;

public class bulletproof extends Door implements bulletproof_interface {
    @Override
    public String doorType() {
        return "防弹";
    }

    @Override
    public void bulletproof() {
        System.out.println(this.doorType());

    }
}

package EXPS.Exp04.com.sy4.exa04;

public interface bulletproof_interface {
    public abstract void bulletproof();
}

package EXPS.Exp04.com.sy4.exa04;

public abstract class Door {
    public  void openDoor(){
        System.out.println("open door");
    };
    public  void closeDoor(){
        System.out.println("close door");
    };
    public abstract String doorType();
}



package EXPS.Exp04.com.sy4.exa04;
public class theftproof extends Door implements theftproof_interface  {
    @Override
    public void theftproof() {
        System.out.println(this.doorType());
    }

    @Override
    public String doorType() {
        return "防盗";
    }
}

package EXPS.Exp04.com.sy4.exa04;

public interface theftproof_interface {
    public abstract void theftproof();
}

package EXPS.Exp04.com.sy4.exa04;

public class waterandbulletproof extends Door implements waterproof_interface,bulletproof_interface{
    @Override
    public String doorType() {
        return "防水防弹";
    }

    @Override
    public void bulletproof() {
        System.out.println("防弹");
    }

    @Override
    public void waterproof() {
        System.out.println("防水");
    }
}

package EXPS.Exp04.com.sy4.exa04;

public class waterproof extends Door implements waterproof_interface {

    @Override
    public String doorType() {
        return "防水";
    }

    @Override
    public void waterproof() {
        System.out.println(this.doorType());
    }
}

package EXPS.Exp04.com.sy4.exa04;

public interface waterproof_interface {
    public abstract void waterproof();
}

☀️| 四、实验总结

通过这次实验,我掌握了Java语言中继承和多态的相关概念;Java程序设计中继承和多态机制的使用;Java语言中接口和包的概念及使用。

标签:Java,EXPS,Exp04,void,多态,sy4,程序设计,com,public
来源: https://blog.csdn.net/Luoxiaobaia/article/details/121844502