编程语言
首页 > 编程语言> > java实验三正式报告

java实验三正式报告

作者:互联网

实验三 面向对象(二)(实验报告)

姓名: XXX          班级: XXX          学号: XXX

一、实验目的

  在集成开发环境下,实现类的继承及多态,实现抽象类、接口的定义,根据实际问题,运用抽象方法、接口及抽象类进行程序开发。

二、实验目标

  1. 能够运用Java语言来实现类的继承及多态性、熟练使用包来扩展程序功能并能建立自己的包;
  2. 能够灵活运用Java语言的抽象方法、接口、抽象类来编写应用程序解决实际问题。

三、实验内容

3.1 实验环境

IntelliJ IDEA Ultimate Edition 2021.3 x64. + openjdk-17.0.1.

3.2 具体实验内容

问题一

问题分析:定义抽象类用abstract关键字, 其他普通类继承该类即可,注意,抽象类被继承后,需要实现抽象类中的抽象方法

public class QuestionOne {
    public static void main(String[] args) {
        Shape shape1 = new Round(5);
        Shape shape2 = new Rectangle(2, 6);
        Shape shape3 = new EquilateralTriangle(3);
        shape1.describe();
        shape2.describe();
        shape3.describe();
    }
}

abstract class Shape {
    public abstract double calculatePerimeter ();
    public abstract void describe ();
    public abstract double calculateSquare ();
}

class Round extends Shape {
    private final double R;

    public Round(double r) {
        R = r;
    }

    @Override
    public double calculatePerimeter() {
        return Math.PI * this.R * 2;
    }

    @Override
    public void describe() {
        System.out.println("这是一个半径为" + this.R + "的圆, 面积为" + calculateSquare() + ", 周长为" + calculatePerimeter());
    }

    @Override
    public double calculateSquare() {
        return Math.PI * this.R * this.R;
    }
}

class Rectangle extends Shape {
    private final double a;
    private final double b;

    public Rectangle(double a, double b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public double calculatePerimeter() {
        return this.a * 2 + this.b * 2;
    }

    @Override
    public void describe() {
        System.out.println("这是一个长为" + this.a + ",宽为" + this.b + "的矩形, 面积为" + calculateSquare() + ", 周长为" + calculatePerimeter());
    }

    @Override
    public double calculateSquare() {
        return this.a * this.b;
    }
}

class EquilateralTriangle extends Shape {
    private final double a;

    public EquilateralTriangle(double a) {
        this.a = a;
    }

    @Override
    public double calculatePerimeter() {
        return this.a * 3;
    }

    @Override
    public void describe() {
        System.out.println("这是一个变长为" + this.a + "的等边三角形, 面积为" + calculateSquare() + ", 周长为" + calculatePerimeter());
    }

    @Override
    public double calculateSquare() {
        return this.a * this.a * 2 * Math.sqrt(3);
    }
}

这是一个半径为5.0的圆, 面积为78.53981633974483, 周长为31.41592653589793
这是一个长为2.0,宽为6.0的矩形, 面积为12.0, 周长为16.0
这是一个变长为3.0的等边三角形, 面积为31.17691453623979, 周长为9.0

问题二

问题分析:定义接口用interface关键字,实现接口使用implements关键字

public class QuestionTwo {
    public static void main(String[] args) {
        Teacher teacher = new Teacher("王老师");
        Student student = new Student("张三");
        teacher.talk();
        student.talk();
        teacher.homework();
        student.homework();
    }
}

interface Lecture {
    public abstract void talk ();
    public abstract void homework ();
}

class Student implements Lecture {

    private final String name;

    @Override
    public void talk() {
        System.out.println(this.name + "回答问题");
    }

    public Student(String name) {
        this.name = name;
    }

    @Override
    public void homework() {
        System.out.println(this.name + "做作业");
    }
}

class Teacher implements Lecture {

    private final String name;

    @Override
    public void talk() {
        System.out.println(this.name + "提出一个问题");
    }

    public Teacher(String name) {
        this.name = name;
    }

    @Override
    public void homework() {
        System.out.println(this.name + "布置了作业");
    }
}

王老师提出一个问题
张三回答问题
王老师布置了作业
张三做作业

问题三

问题分析:定义接口用interface关键字,实现接口的类重写其中的抽象函数

public class QuestionThree {
    public static void main(String[] args) {
        Even even = new Even();
        System.out.println("100以内的偶数有" + even.count() + "个");
        System.out.println("100以内的偶数和为:" +  even.sum());
        Odd odd = new Odd();
        System.out.println("100以内的奇数有" + odd.count() + "个");
        System.out.println("100以内的奇数和为:" +  odd.sum());
    }
}

interface Countable {
    public abstract int sum ();
    public abstract int count ();
}

// 偶数
class Even implements Countable {

    @Override
    public int sum() {
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                sum += i;
            }
        }
        return sum;
    }

    @Override
    public int count() {
        int count = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0)
                count++;
        }
        return count;
    }
}

// 奇数
class Odd implements Countable {

    @Override
    public int sum() {
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 2 != 0) {
                sum += i;
            }
        }
        return sum;
    }

    @Override
    public int count() {
        int count = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 2 != 0)
                count++;
        }
        return count;
    }
}

100以内的偶数有51个
100以内的偶数和为:2550
100以内的奇数有50个
100以内的奇数和为:2500

问题四

问题分析:定义接口用interface关键字,实现接口使用implements关键字,继承类使用extends关键字,注意,接口可以多实现,类不能多继承

public class QuestionFour {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.breathe();
        cat.eat();
        cat.sleep();
        cat.speak();
        cat.think();
        cat.walk();
        People people = new People();
        people.breathe();
        people.eat();
        people.sleep();
        people.speak();
        people.think();
        people.walk();
        people.swim();
        Bird bird = new Bird();
        bird.breathe();
        bird.eat();
        bird.sleep();
        bird.speak();
        bird.think();
        bird.walk();
        bird.fly();
        bird.swim();
        Fish fish = new Fish();
        fish.breathe();
        fish.eat();
        fish.sleep();
        fish.speak();
        fish.think();
        fish.swim();
    }
}

abstract class Animal {
    public abstract void breathe ();
}

class Cat extends Animal implements AbleToEat, AbleToSleep, Thinkable, Speakable, Walkable {

    @Override
    public void breathe() {
        System.out.println("猫呼吸");
    }

    @Override
    public void eat() {
        System.out.println("猫吃饭");
    }

    @Override
    public void sleep() {
        System.out.println("猫睡觉");
    }

    @Override
    public void think() {
        System.out.println("猫思考");
    }

    @Override
    public void speak() {
        System.out.println("喵喵喵");
    }

    @Override
    public void walk() {
        System.out.println("猫走路");
    }
}

class People extends Animal implements AbleToEat, AbleToSleep, Thinkable, Speakable, Walkable, AbleToSwim {
    @Override
    public void breathe() {
        System.out.println("人呼吸");
    }

    @Override
    public void eat() {
        System.out.println("人吃饭");
    }

    @Override
    public void sleep() {
        System.out.println("人睡觉");
    }

    @Override
    public void think() {
        System.out.println("人思考");
    }

    @Override
    public void speak() {
        System.out.println("人说话");
    }

    @Override
    public void walk() {
        System.out.println("人走路");
    }

    @Override
    public void swim() {
        System.out.println("人游泳");
    }
}

class Bird extends Animal implements AbleToEat, AbleToSleep, Thinkable, Speakable, Walkable, AbleToSwim, Flyable {
    @Override
    public void breathe() {
        System.out.println("鸟呼吸");
    }

    @Override
    public void eat() {
        System.out.println("鸟吃食");
    }

    @Override
    public void sleep() {
        System.out.println("鸟睡觉");
    }

    @Override
    public void think() {
        System.out.println("鸟思考");
    }

    @Override
    public void speak() {
        System.out.println("叽叽喳喳");
    }

    @Override
    public void walk() {
        System.out.println("鸟跳");
    }

    @Override
    public void swim() {
        System.out.println("鸟游泳");
    }

    @Override
    public void fly() {
        System.out.println("鸟飞翔");
    }
}

class Fish extends Animal implements AbleToEat, AbleToSleep, Thinkable, Speakable, AbleToSwim {

    @Override
    public void breathe() {
        System.out.println("鱼呼吸");
    }

    @Override
    public void eat() {
        System.out.println("鱼吃食");
    }

    @Override
    public void sleep() {
        System.out.println("鱼睡觉");
    }

    @Override
    public void think() {
        System.out.println("鱼思考");
    }

    @Override
    public void speak() {
        System.out.println("啵啵啵");
    }

    @Override
    public void swim() {
        System.out.println("鱼游动");
    }
}


interface AbleToEat {
    public abstract void eat ();
}

interface AbleToSleep {
    public abstract void sleep ();
}

interface Thinkable {
    public abstract void think ();
}

interface Speakable {
    public abstract void speak ();
}

interface Walkable {
    public abstract void walk ();
}

interface AbleToSwim {
    public abstract void swim ();
}

interface Flyable {
    public abstract void fly ();
}

猫呼吸
猫吃饭
猫睡觉
喵喵喵
猫思考
猫走路
人呼吸
人吃饭
人睡觉
人说话
人思考
人走路
人游泳
鸟呼吸
鸟吃食
鸟睡觉
叽叽喳喳
鸟思考
鸟跳
鸟飞翔
鸟游泳
鱼呼吸
鱼吃食
鱼睡觉
啵啵啵
鱼思考
鱼游动

四、实验总结

  学习和使用了java抽象类和接口,掌握了定义抽象类和接口中类属性、类方法,可以理解面向对象编程,会使用面向对象编程的方法解决问题,明白java中没有多继承,多继承是通过多接口实现。
  抽象类中的抽象方法是public abstract的
接口中的方法是public abstract的,属性是public final的

标签:java,void,System,println,实验,Override,正式,public,out
来源: https://blog.csdn.net/qq_37354060/article/details/121280679