该代码是否正确地演示了多态性? (JAVA)

时间:2018-08-12 12:43:49

标签: java oop polymorphism

我已经编写了一个小程序来练习和演示Java多态性,所以请看一看,让我知道该想法是否正确实现。

public class Main {

    public static void main(String[] args) {
        porche porche_car = new porche("Porche 9/11",8,"black");

        porche_car.startEngine(true);
        porche_car.accelerate(250);
        porche_car.brake(true);


        Ford ford_car = new Ford("Ford SUV",4,"blue");

        ford_car.startEngine(true);
        ford_car.accelerate(200);
        ford_car.brake(false);
    }
}

public class Car {

    private boolean engine;
    private String name;
    private int cylinders;
    private int wheels;

    public Car(String name, int cylinders)
    {
        this.engine = true;
        this.name = name;
        this.cylinders = cylinders;
        this.wheels = 4;
    }

    public void startEngine(boolean start)
    {
        if(start)
        {
            System.out.println(getName()+"Class.Engine Started.");
        }
        else
        {
            System.out.println(getName()+" Class.Engine Stopped.");
        }
    }

    public void accelerate(int acceleration)
    {
            System.out.println(getName()+" class.Accelerating car at " + acceleration + " Km/h");

    }
    public void brake(boolean applybrake)
    {
        if (applybrake)
        {
            System.out.println(getName()+" class called.Brakes applied.");
        }
        else
        {
            System.out.println(getName()+" class, brakes released.");
        }
    }

    public boolean isEngine() {
        return engine;
    }

    public String getName() {
        return name;
    }

    public int getCylinders() {
        return cylinders;
    }

    public int getWheels() {
        return wheels;
    }
}

public class porche extends Car{
    private String color;
    private int doors;

    public porche(String name, int cylinders, String color) {
        super(name, cylinders);
        this.color = color;
        this.doors = 2;
    }

    @Override
    public void startEngine(boolean start) {
        super.startEngine(start);
    }

    @Override
    public void accelerate(int acceleration) {
        super.accelerate(acceleration);
    }

    @Override
    public void brake(boolean applybrake) {
        super.brake(applybrake);
    }

    public String getColor() {
        return color;
    }

    public int getDoors() {
        return doors;
    }
}

public class Ford extends Car{
    private String color;
    private int doors;

    public Ford(String name, int cylinders, String color) {
        super(name, cylinders);
        this.color = color;
        this.doors = 2;
    }

    @Override
    public void startEngine(boolean start) {
        super.startEngine(start);
    }

    @Override
    public void accelerate(int acceleration) {
        super.accelerate(acceleration);
    }

    @Override
    public void brake(boolean applybrake) {
        super.brake(applybrake);
    }

    public String getColor() {
        return color;
    }

    public int getDoors() {
        return doors;
    }
}

注意:概述,我创建了一个基类Car,并扩展为创建了两个以上的类(Porche和Ford),并且我重写了Car类中的方法,以将基类Car的功能添加到其子类中(以实现多态性。)实现可接受吗?

更新实际上,我发布的代码是一个挑战,我正在从udemy(Java主类)学习课程。完成挑战后,我查看了那里提供的解决方案。在该解决方案中,使用了与我类似的方法,该解决方案的代码如下:

class Car {
private boolean engine;
private int cylinders;
private String name;
private int wheels;

public Car(int cylinders, String name) {
    this.cylinders = cylinders;
    this.name = name;
    this.wheels = 4;
    this.engine = true;
}

public int getCylinders() {
    return cylinders;
}

public String getName() {
    return name;
}

public String startEngine() {
    return "Car -> startEngine()";
}

public String accelerate() {
    return "Car -> accelerate()";
}

public String brake() {
    return "Car -> brake()";
}

三菱扩展级轿车{

public Mitsubishi(int cylinders, String name) {
    super(cylinders, name);
}

@Override
public String startEngine() {
    return "Mitsubishi -> startEngine()";
}

@Override
public String accelerate() {
    return "Mitsubishi -> accelerate()";
}

@Override
public String brake() {
    return "Mitsubishi -> brake()";
}

霍顿课扩展了汽车{

public Holden(int cylinders, String name) {
    super(cylinders, name);
}

@Override
public String startEngine() {
    return getClass().getSimpleName() +  " -> startEngine()";
}

@Override
public String accelerate() {
    return getClass().getSimpleName() +  " -> accelerate()";
}

@Override
public String brake() {
    return getClass().getSimpleName() +  " -> brake()";
}

福特类扩展了汽车{

public Ford(int cylinders, String name) {
    super(cylinders, name);
}

@Override
public String startEngine() {
    return "Ford -> startEngine()";
}

@Override
public String accelerate() {
    return "Ford -> accelerate()";
}

@Override
public String brake() {
    return "Ford -> brake()";
}

公共类主要{

public static void main(String[] args) {
    // We are going to go back to the car analogy.
    // Create a base class called Car
    // It should have a few fields that would be appropriate for a generice car calss.
    // engine, cylinders, wheels, etc.
    // Constructor should initialize cylinders (number of) and name, and set wheels to 4
    // and engine to true. Cylinders and names would be passed parameters.
    //
    // Create appropriate getters
    //
    // Create some methods like startEngine, accelerate, and brake
    //
    // show a message for each in the base class
    // Now create 3 sub classes for your favorite vehicles.
    // Override the appropriate methods to demonstrate polymorphism in use.
    // put all classes in the one java file (this one).

    Car car = new Car(8, "Base car");
    System.out.println(car.startEngine());
    System.out.println(car.accelerate());
    System.out.println(car.brake());

    Mitsubishi mitsubishi = new Mitsubishi(6, "Outlander VRX 4WD");
    System.out.println(mitsubishi.startEngine());
    System.out.println(mitsubishi.accelerate());
    System.out.println(mitsubishi.brake());

    Ford ford = new Ford(6, "Ford Falcon");
    System.out.println(ford.startEngine());
    System.out.println(ford.accelerate());
    System.out.println(ford.brake());

    Holden holden = new Holden(6, "Holden Commodore");
    System.out.println(holden.startEngine());
    System.out.println(holden.accelerate());
    System.out.println(holden.brake());
}

2 个答案:

答案 0 :(得分:2)

证明多态性示例的最佳方法是编写一个接受基类参数并通过传递子类对象来重用该方法的方法。

例如:-

     public class Test{

     public static void main(String[] args){

         Car c = new Car();
        checkCar(c);
        c = new Ferrari();
        checkCar(c);
        c = new Honda();
        checkCar(c);

     }




    public static void checkCar(Car c){

        c.start();
    }
  }

 class Car{

    public void start(){
        System.out.println("Car Starts");
    }

 };

 class Ferrari extends Car{
    public void start(){
        System.out.println("Ferrari Starts");
    }
 };

 class Honda extends Car{
    public void start(){
        System.out.println("Honda Starts");
    }
 };

答案 1 :(得分:0)

首先,您的示例是简单地使用多态。如上面的注释中所述,您已经从基类调用了方法。您无需重写此方法即可在派生类中使用它们。

第二,您将派生类用作主函数中的一种变量,因此您不会注意到多态性是如何工作的。您应该改用基类Car。

Car porche_car = new Porche("Porche 9/11",8,"black");
porche_car.startEngine(true); // uses method from Porsche class if overriden