[Java] Experiment 3 Abstract Classes and Interfaces

Experiment Name     Experiment 3 Abstract Classes and Interfaces

Purpose  

1. Deeply understand the meaning of abstract classes and interfaces.

2. Be proficient in the definition of abstract classes and interfaces, methods of inheriting abstract classes and implementing interfaces.

3. Understand and master polymorphism.  

Experiment content

(1) Abstract class experiment: Create a new ahpu.shape package in the project source code, and place the following classes in this package

1. Define an abstract class Shape, including:

(1) Attribute: color color (available string type is used for output simulation)

(2) Method: The abstract method draw() for drawing graphics; the set method and get method for color operations.

2. Inherit the Shape class and design the abstract class RingShape. RingShape does not need to define properties and methods.

3. Inherit the RingShape class, design the Circle class, and implement the specific draw method: you can use the print method to simulate drawing, such as

System.out.println("Draw a circle with color: " + this.getColor() );

4. Inherit the RingShape class, design the Ellipse class, and implement the specific draw method: you can use the print method to simulate drawing.

5. Inherit the Shape class, design the Triangle class, and implement the specific draw method: you can use the print method to simulate drawing.

6. Design a class Demo for demonstration, and perform functional testing on the draw methods of Circle and Ellipse. (Or test using JUnit

7. Define an interface IGeometry for finding the area. The method for finding the area is getArea().

8. Inherit the Shape class, implement the IGeometry interface, and design a rectangle class Rectangle. Rectangle has two attributes: length and width. Implement the specific draw method and the specific area getArea method.

9. In the Demo class, or use JUnit to test the functions of the Rectangle class.

// ahpu.shape 包

package ahpu.shape;



// 抽象类 Shape

public abstract class Shape {

    private String color;



    public Shape(String color) {

        this.color = color;

    }



    public String getColor() {

        return color;

    }



    public void setColor(String color) {

        this.color = color;

    }



    public abstract void draw();

}



// 抽象类 RingShape

public abstract class RingShape extends Shape {

    public RingShape(String color) {

        super(color);

    }

}



// 类 Circle

public class Circle extends RingShape {

    public Circle(String color) {

        super(color);

    }



    @Override

    public void draw() {

        System.out.println("Draw a circle with color: " + getColor());

    }

}



// 类 Ellipse

public class Ellipse extends RingShape {

    public Ellipse(String color) {

        super(color);

    }



    @Override

    public void draw() {

        System.out.println("Draw an ellipse with color: " + getColor());

    }

}



// 类 Triangle

public class Triangle extends Shape {

    public Triangle(String color) {

        super(color);

    }



    @Override

    public void draw() {

        System.out.println("Draw a triangle with color: " + getColor());

    }

}



// 接口 IGeometry

public interface IGeometry {

    double getArea();

}



// 类 Rectangle

public class Rectangle extends Shape implements IGeometry {

    private double length;

    private double width;



    public Rectangle(String color, double length, double width) {

        super(color);

        this.length = length;

        this.width = width;

    }



    @Override

    public void draw() {

        System.out.println("Draw a rectangle with color: " + getColor());

    }



    @Override

    public double getArea() {

        return length * width;

    }

}



// 类 Demo

public class Demo {

    public static void main(String[] args) {

        Circle circle = new Circle("Red");

        Ellipse ellipse = new Ellipse("Blue");



        circle.draw();

        ellipse.draw();



        Rectangle rectangle = new Rectangle("Green", 4, 5);

        System.out.println("Rectangle Area: " + rectangle.getArea());

    }

}

(2) Polymorphic demonstration experiment: Create a new ahpu.vehicle package in the project source code, and place the following classes in this package

1. Design the abstract class Vehicle , which defines an abstract method transport() to represent transportation.

2. Inherit the abstract class Vehicle and design the ordinary Java class Bus . Transport() is implemented in the Bus class and you can use print output to simulate the function, such as:

System.out.println("A bus is driving......");

3. Similar to 2 , inherit the abstract class Vehicle and design the ordinary Java class Aircraft . Transport() is implemented in the Aircraft class, and the print output can be used to simulate the function.

4. Design driver class Driver ,

( 1 ) There is a Vehicle class object member vehicle in the class , and it has set and get methods;

( 2 ) There is a method drive() in the class , which calls the transport method of the vehicle object , such as:

public void drive() {

        vehicle.transport();

}

5. Design the function demonstration class Demo . For the Driver class object driver , set up different transportation class objects bus ( Bus class object) and aircraft ( Aircraft class object) to demonstrate the different transportation functions of different transportation objects ( multiple transport methods state).

// ahpu.vehicle 包

package ahpu.vehicle;



// 抽象类 Vehicle

public abstract class Vehicle {

    public abstract void transport();

}



// 类 Bus

public class Bus extends Vehicle {

    @Override

    public void transport() {

        System.out.println("A bus is driving......");

    }

}



// 类 Aircraft

public class Aircraft extends Vehicle {

    @Override

    public void transport() {

        System.out.println("An aircraft is flying......");

    }

}



// 类 Driver

public class Driver {

    private Vehicle vehicle;



    public Vehicle getVehicle() {

        return vehicle;

    }



    public void setVehicle(Vehicle vehicle) {

        this.vehicle = vehicle;

    }



    public void drive() {

        vehicle.transport();

    }

}



// 类 Demo

public class Demo {

    public static void main(String[] args) {

        Driver driver = new Driver();



        Vehicle bus = new Bus();

        Vehicle aircraft = new Aircraft();



        driver.setVehicle(bus);

        driver.drive();



        driver.setVehicle(aircraft);

        driver.drive();

    }

}

(3) Interface and anonymous inner class experiment: Create a new ahpu.innerclass package in the project source code, and place the following classes in this package

1. Design the interface ( interface ) Car . Three methods are defined in the Car interface: start() , accelerate() , brake() .

2. Design the smart robot (i.e. the driver of the Car ) class SmartRobot , defined in the class

( 1 ) Attributes: the object car of the Car interface , and the corresponding set method and get method;

( 2 ) Method: drive() , simulates the driving function of the intelligent robot. For specific code, please refer to:

public void drive() {

        car.start();

        car.accelerate();

        car.brake();

    }

3. Design the function demonstration class Demo. For the SmartRobot class object robot, set up an anonymous inner class object based on the interface Car. The inner class covers the implementation of three methods: start() , accelerate() , and brake() .

4. Demonstrate the drive method of the robot object .

// ahpu.innerclass 包

package ahpu.innerclass;



// 接口 Car

public interface Car {

    void start();



    void accelerate();



    void brake();

}



// 类 SmartRobot

public class SmartRobot {

    private Car car;



    public Car getCar() {

        return car;

    }



    public void setCar(Car car) {

        this.car = car;

    }



    public void drive() {

        car.start();

        car.accelerate();

        car.brake();

    }

}



// 类 Demo

public class Demo {

    public static void main(String[] args) {

        SmartRobot robot = new SmartRobot();



        Car car = new Car() {

            @Override

            public void start() {

                System.out.println("Car is starting...");

            }



            @Override

            public void accelerate() {

                System.out.println("Car is accelerating...");

            }



            @Override

            public void brake() {

                System.out.println("Car is braking...");

            }

        };



        robot.setCar(car);

        robot.drive();

    }

}

Experimental procedures and results (Appendix)

think

How to understand compile-time polymorphism and run-time polymorphism? Give an example of each.

Understanding compile-time polymorphism and run-time polymorphism:

Compile-time polymorphism (also known as static polymorphism) refers to determining which method or class to use at the compile stage. It has to do with static typing, which is type information used by the compiler. For example, with compile-time polymorphism, the compiler can determine the signature of a method, but not the specific implementation of the method that is actually called.

Shape shape = new Circle("Red"); // Compile-time polymorphism, the static type of shape is Shape

shape.draw(); // Determine to use the draw method of Shape at compile time

Run-time polymorphism (also known as dynamic polymorphism) refers to determining which method or class to use while the program is running. It has to do with the type of the actual object, not the static type. At runtime, Java dynamically looks up the actual implementation of the method.

Shape shape = new Circle("Red"); // Runtime polymorphism, the actual type of shape is Circle

shape.draw(); // Determine to use Circle's draw method at runtime

Compile-time polymorphism mainly involves the compiler's type checking and method signature determination, while run-time polymorphism allows the corresponding method to be called based on the actual type of the object.

Guess you like

Origin blog.csdn.net/qq_42531954/article/details/135370714