How to use polymorphism to make list with different types in java?

后端 未结 5 1301
北荒
北荒 2020-12-22 04:30

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 me

相关标签:
5条回答
  • 2020-12-22 05:00

    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.

    0 讨论(0)
  • 2020-12-22 05:07

    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());
    
    0 讨论(0)
  • 2020-12-22 05:16

    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());
    }
    
    0 讨论(0)
  • 2020-12-22 05:17

    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

    0 讨论(0)
  • 2020-12-22 05:23

    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.

    0 讨论(0)
提交回复
热议问题