How to use polymorphism to make list with differen

2020-02-06 08:36发布

问题:

I have 3 Classes Circle, Rectangle and Square

I want to get required data for each of above classes and create them by user .

It means that user can make what ever wants ,For example 3 Circles ,2 Rectangles and 7 Squares . The number of shapes it depends on the user.

Then I want to save them in a unit list and call my classes methods ,which are calculateArea and calculatePerimeter and show perimeter and area of them with their name .

How can I do It?

These are my classes

Circle

public class Cricle {

    private int radius;

    public Cricle(int radius) {
        this.radius = radius;
    }

    public  double calculateArea()
    {
        return (radius*radius)*Math.PI;
    }
    public double  calculatePerimeter()
    {
        return  (radius*2)*Math.PI;
    }
}

Rectangle

public class Rectangle {

    private int width;
    private int height;

    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }
    public int calculateArea() {
        return width*height;
    }

    public int calculatePrimeter() {
        return (width+height)*2;
    }
}

Square

public class Square {
    private int edge;


    public int calculateArea() {
        return edge*edge;
    }

    public int calculatePrimeter() {
        return edge*4;
    }
}

回答1:

You can define an interface and all your classes will implement this interface. Add all common methods into an interface.

public interface Shapes {
   public double calculateArea();
   public double calculatePrimeter();
}

Now all your shape class's will implement the above interface and provide implementation to interface methods. In your case change the return type of all your methods. you can keep it double.

public class Circle implements Shapes{
    private int radius;

    public Circle (int radius) {
        this.radius = radius;
    }

    @Override
    public double calculateArea() {
        return (radius * radius) * Math.PI;
    }

    @Override
    public double calculatePrimeter() {
        return (radius * 2) * Math.PI;
    }
}

public class Rectangle implements Shapes{}
public class Square implements Shapes{}

Then you need to have one list

static List<Shapes> unitList = new ArrayList<Shapes>();

Get inputs from the user & add to the above list. Then simply loop unitList & call respective methods

For calculating Area

for (Shapes shape : unitList)
    System.out.println("Area: " + shape.calculateArea());

For calculating Perimeter

for (Shapes shape : unitList)
    System.out.println("Perimeter: " + shape.calculatePrimeter());


回答2:

Create a interface lets call TwoDimensionalShape and put common methods in it.

public interface TwoDimensionalShape {
   double calculateArea();
   int calculatePrimeter();
}

And all the classes implement this interface

public class Circle implements TwoDimensionalShape {
    //your code 
}

public class Rectangle implements TwoDimensionalShape {
    //your code 
}

public class Square implements TwoDimensionalShape {
    //your code 
}

And create a List<TwoDimensionalShape> and put all these shapes in this list. Like

List<TwoDimensionalShape> shapes= new ArrayList<TwoDimensionalShape>();
shapes.add(new Circle(5));
shapes.add(new Rectangle(4,3));
shapes.add(new Square(4));
for (TwoDimensionalShape shape : shapes) {
    System.out.println("Area = " + shape.calculateArea());
    System.out.println("Perimeter = " + shape.calculatePrimeter());
}


回答3:

Create an Interface For eg. Shape.

public interface Shape {
    int calculateArea();
    int calculatePrimeter();
}

implement this interface in all the three classes. unit list will be a List<Shape> and you can then invoke calculateArea() and calculatePrimeter() methods while iterating over the list



回答4:

If you want an official reference on how polymorphism works in java, there's the Oracle Java Tutorial's section on Polymorphism, which explains that a variable of a given class can hold values of any of its subclasses, and that methods called on a superclass variable will use implementations in the subclass if they exist.

To tailor the information in this tutorial to your question:
A List can be considered a collection of variables, and a list of Shapes as described in previous answers can contain of instances of any subclass of Shape, i.e. Square, Rectangle, or Circle. Calling the calculateArea and calculatePrimeter methods on elements of the list will call the corresponding method for that shape.



回答5:

you can use the concept of inheritance to do this. for example, create a Shape class and then make the other classes inherit:

public abstract class Shape {
  public int calculateArea();
}
public class Circle extends Shape {}
public class Square extends Shape {}
public class Rectangle extends Shape {}

Then, you can use list of Shape.