Java实验(3)类的继承与多态

一、实验目的:
1.掌握类的继承方法及上转型对象的方法调用。
2.掌握this和super的区别及使用。
3.理解抽象类的概念及作用,掌握接口的声明,实现及接口回调
二、实验代码
1(题目编号7173)、现定义一个类体系,基类为Dog,派生类为斑点狗SpottedDog类和非斑点狗UnspottedDog类,具体要求如下:
(1)在基类中记录狗的品种breed,体重weight以及颜色color等属性,定义一个方法show()显示Dog信息;
(2)在UnspottedDog类中,调用Dog类的构造方法,重写show()方法,只显示狗的品种;
(3)在SpottedDog类中,新增表示斑点颜色的spotColor属性,定义包含四个属性的构造方法,重写show()方法;
(4)定义测试类,构造斑点狗对象,分别显示斑点狗的品种、体重、颜色和品种、颜色、斑点颜色;构造非斑点狗对象,显示狗的品种、体重、颜色信息。
(说明:构造斑点狗对象和非斑点狗对象时要分别输入,各属性值之间用空格分割,输入完后按回车键确认,输入内容参照测试数据。)

import java.util.Scanner;

class Dog{
    protected int weight;
    protected String color,bread;
    public Dog(String bread,int weight,String color){
        this.weight=weight;
        this.bread=bread;
        this.color=color;
    }
    public void show(){
        System.out.println("这是一只"+this.bread+"体重为"+this.weight+",颜色为"+this.color);
    }
}

class SpottedDog extends Dog{
    String spottedColor;
    public SpottedDog(String bread,int weight,String color,String spottedColor){
        super(bread, weight,  color);
        this.weight=weight;
        this.bread=bread;
        this.color=color;
        this.spottedColor=spottedColor;
    }
    public void show(){
        System.out.println("这是一只"+this.bread+",颜色为"+this.color+",斑点颜色为"+this.spottedColor);
    }
}

class UnspottedDog extends Dog{
    public UnspottedDog(String bread,int weight,String color){
        super(bread, weight, color);
        this.weight=weight;
        this.bread=bread;
        this.color=color;
    }
    public void show(){
        System.out.println("这是一只"+this.bread+"犬");
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int weight;
        String bread,color,spotColor;
        bread = sc.next();
        weight = sc.nextInt();
        color = sc.next();
        spotColor = sc.next();
        Dog dog = new Dog(bread,weight,color);
        dog.show();
        SpottedDog spottedDog = new SpottedDog(bread,weight,color,spotColor);
        spottedDog.show();
        UnspottedDog unspottedDog = new UnspottedDog(sc.next(),sc.nextInt(),sc.next());
        unspottedDog.show();
    }
}

测试截图:
在这里插入图片描述
2(题目编号7174)、编写一个制造各种车辆的程序。包含三个类,具体要求如下:
(1)基类Vehicle,包含轮子数和汽车自身重量两个属性,一个两参数的构造方法,一个显示汽车信息的方法;
(2)小轿车类Car,增加载客数属性,重写构造方法和显示车辆信息的成员方法;
(3)卡车类Truck,增加载客数和载货量属性,重写构造方法和显示车辆信息的成员方法;
(4)主程序类,要求输入各种车辆的信息,并在控制台输出各种车辆信息。

import java.util.Scanner;

class Vehicle {
    int wheelnum;
    double weight;

    public Vehicle(int wheelnum, double weight) {
        this.wheelnum = wheelnum;
        this.weight = weight;
    }

    public void getVehicle() {
        System.out.println("汽车:\n" + "轮子数:" + this.wheelnum + "个\n" + "自身重量:" + this.weight + "吨");
    }
}

class Car extends Vehicle {
    int passgernum;

    public Car(int wheelnum, double weight, int passgernum) {
        super(wheelnum, weight);
        this.wheelnum = wheelnum;
        this.weight = weight;
        this.passgernum = passgernum;
    }

    public void getCar() {
        System.out.println("小轿车:\n" + "轮子数:" + this.wheelnum + "个\n" + "自身重量:" + this.weight + "吨\n" + "额定乘客数:" + this.passgernum+"人");
    }
}

class Truck extends Car {
    double load;

    public Truck(int wheelnum, double weight, int passgernum, double load) {
        super(wheelnum, weight, passgernum);
        this.wheelnum = wheelnum;
        this.weight = weight;
        this.passgernum = passgernum;
        this.load = load;
    }

    public void getTruck() {
        System.out.println("卡车:\n" + "轮子数:" + this.wheelnum + "个\n" + "自身重量:" + this.weight + "吨\n" + "额定乘客数" + this.passgernum + "人\n" + "载重量" + this.load + "吨");
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        Vehicle car1 = new Vehicle(in.nextInt(),in.nextDouble());
        car1.getVehicle();
        Car car=new Car(in.nextInt(),in.nextDouble(),in.nextInt());
        car.getCar();
        Truck turck=new Truck(in.nextInt(),in.nextDouble(),in.nextInt(),in.nextDouble());
        turck.getTruck();
    }
}

3(题目编号7175)、使用接口或者抽象类编写程序实现显示员工基本信息。具体要求如下:
(1)使用接口或者抽象类实现基类Employer(体会接口和抽象类的不同),包含姓名、部门和工资三个属性,显示工资的方法showSalary()和显示奖金的抽象方法showBonus();提示:因每位职工奖金不同,showBonus()方法定义为抽象方法,只抽象定义,不具体实现;
(2)定义BasicEmployee和GoodEmployee类,重写Employer类中的方法,不同员工有不同的工资和奖金;
(3)定义主类进行测试,要求输入两个不同的员工信息,并输出其个人信息。

import java.util.*;
abstract class Employer {
    String name;
    String department;
    double salary;
    abstract void showSalary();//输出工资的抽象方法
    abstract void showBonus();//输出奖金的抽象方法
}
class BasicEmployee extends Employer {
    BasicEmployee(String n, String d, double s) {
        this.name = n;
        this.department = d;
        this.salary = s;
    }
    void showSalary() {
        System.out.printf("我叫%s,在%s部门,我的工资是%.1f\n", this.name, this.department, this.salary);

    }
    void showBonus() {
        System.out.printf("我是普通员工,没有奖金,加油升级!\n");
    }
}
class GoodEmployee extends Employer {
    double bonus;
    GoodEmployee(String n, String d, double s, double b) {
        this.name = n;
        this.department = d;
        this.salary = s;
        this.bonus = b;
    }
    void showSalary() {
        System.out.printf("我叫%s,在%s部门,我的工资是%.1f\n", this.name, this.department, this.salary);
    }
    void showBonus() {
        System.out.println("我是优秀员工,我的奖金是"+this.bonus);
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BasicEmployee e = new BasicEmployee(in.next(), in.next(), in.nextInt());
        e.showSalary();
        e.showBonus();
        GoodEmployee e1 = new GoodEmployee(in.next(),in.next(), in.nextInt(), in.nextInt());
        e1.showSalary();
        e1.showBonus();
    }
}

4(题目编号7176)、编写一个教师讲课的程序。所有老师都具有共同的讲课方法,但是不同科目的教师讲课内容不同,主程序中编写一个讲课的方法TeachingRace(Teacher t),显示不同的老师t讲授不同的课程内容。提示:
(1)所有老师具有共同的讲课方法,可在接口中定义一个讲课方法;
(2)不同科目的老师实现接口中的讲课方法;
(3)在主程序中定义一个讲课的方法TeachingRace(Teacher t),构造不同的教师,显示讲课内容【主要考察接口回调】。

import java.util.*;

interface Teach {
    void TeachRace();
}

class Teacher implements Teach {
    private String course;
    public void setCourse(String course) {
        this.course = course;
    }
    public void TeachRace() {
        if (course.equals("ABC")) {
            System.out.println("我是"  + "英语老师," + "I say ABC");
        }
        if (course.equals("方程组求解")) {
            System.out.println("我是" + "数学老师," + "I say 方程组求解");
        }
    }
}


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Teacher teacher1 = new Teacher();
        teacher1.setCourse(sc.next());
        Teacher teacher2 = new Teacher();
        teacher2.setCourse(sc.next());
        TeachingRace(teacher1);
        TeachingRace(teacher2);
    }
    public static void TeachingRace(Teacher t){
        t.TeachRace();
    }
}

5(题目编号7177)、创建Animal(动物)类:Mouse,dog等的一个继承分级结构.在父类中提供适用于所有Animal的方法,并在子类中覆盖他们,从而根据不同类型的Animal采取不同的行动Anima类有如下方法:public void speak()。

import java.util.*;
class Animal{
    public void speak(){}
}
class Cat extends Animal{
    String name;
    public Cat(String name){
        this.name=name;
    }
    public void speak(){
        System.out.println(this.name+"的叫声为喵喵");
    }
}
class Mouse extends Animal{
    String name;
    public Mouse(String name){
        this.name=name;
    }
    public void speak(){
        System.out.println(this.name+"的叫声为吱吱");
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Cat cat=new Cat(in.next());
        cat.speak();
        Mouse mouse=new Mouse(in.next());
        mouse.speak();
    }
}

6(题目编号7178)、编写一个通过接口实现不同应用情况下计算平均分的程序,具体要求如下:

  • (1)编写一个ComputerAverage接口,接口有一个求平均分的抽象方法average,方法的参数为double类型的数组。
    *(2)定义Gymnastics类和School类,它们都是ComputerAverage的实现类,Gymnastics类中的平均分方法为计算体育比赛中选手的平均成绩,具体算法是去掉一个最低分,去掉一个最高分,然后对剩余的数求平均分。
    *(3)School类中的平均分为计算学生考试成绩的平均分,具体算法是分数的和除以总的科目数
    *(4)要求:在主类中声明ComputerAverage的对象,并使用为上转型对象,实现ComputerAverage的对象调用average方法, 实现多态,同样的两条语句实现两种不同计算平均分的方法。输入的成绩为一组数,数据的个数和具体的数据从键盘输入。
import java.util.Scanner;

interface ComputerAverage {
    public double average(double [] data);
}

class Gymnastics implements ComputerAverage {
    public double average(double[] data) {
        int i,i_max=0,i_min=0;
        double sum = 0;
        double temp;
        double max=data[0],min=data[0];
        for(i=0;i<data.length;i++){
            if(data[i]>max){
                max=data[i];
            }
            if(data[i]<min){
                min=data[i];
            }
        }
        for (double x : data) {
            sum = sum + x;
        }
        sum=(sum-max-min)/(data.length-2);
        return sum;
    }
}
class School implements ComputerAverage {
    public double average(double[] data) {
        double sum = 0;
        //double temp;
        for (double x : data) {
            sum = sum + x;
        }
        sum=sum/(data.length);
        return sum;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        double[] data = new double[n];
        for (int i = 0; i < n; i++) {
            data[i] = scan.nextDouble();
        }
        //start
        //write your code between start and  end,do not  delete any code
        //end
        ComputerAverage g=new Gymnastics();
        System.out.print("Gymnastics average is:");
        System.out.printf("%.2f\n", g.average(data));
        //start
        //write your code between start and  end,do not  delete any code
        //end
        ComputerAverage G=new School();
        System.out.print("School average is:");
        System.out.printf("%.2f", G.average(data));
    }
}

三、实验过程分析与讨论:
面对对象编程的关键思想是多态性,通过继承而互相关联的类型为多态类型。

其中向上转型:当创建一个父类对象的时候,通过向上转型可以访问父类里的变量,也可以访问子类重写的父类方法。例如:Animal dog=new Dog();
向下转型:需要建立在向上转型的基础之上,即进行向下转型的时候必须有向上转型的步骤。

抽象类和接口:都不能被实例化,抽象类被继承,接口被实现,且必须实现其中的所有抽象方法,否则只能是抽象类。接口只能做方法申明,抽象类既可以方法申明,也可以实现方法。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小绵杨Yancy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值