Ali*_*eli 4 java oop polymorphism class list
我有3类 圆形,矩形和正方形
我想获取上述每个类的必需数据,并由user创建它们。
这意味着用户可以根据需要进行操作,例如3个圆形,2个矩形和7个正方形。形状的数量取决于用户。
然后,我想将它们保存在单元列表中,并调用我的类方法,它们分别是calculateArea和calculatePerimeter,并显示它们的周长和面积及其名称。
我该怎么做?
这些是我的课
圈
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;
}
}
Run Code Online (Sandbox Code Playgroud)
长方形
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;
}
}
Run Code Online (Sandbox Code Playgroud)
广场
public class Square {
private int edge;
public int calculateArea() {
return edge*edge;
}
public int calculatePrimeter() {
return edge*4;
}
}
Run Code Online (Sandbox Code Playgroud)
Swa*_*til 17
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();
}
Run Code Online (Sandbox Code Playgroud)
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{}
Run Code Online (Sandbox Code Playgroud)
Then you need to have one list
static List<Shapes> unitList = new ArrayList<Shapes>();
Run Code Online (Sandbox Code Playgroud)
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());
Run Code Online (Sandbox Code Playgroud)
For calculating Perimeter
for (Shapes shape : unitList)
System.out.println("Perimeter: " + shape.calculatePrimeter());
Run Code Online (Sandbox Code Playgroud)
Create a interface lets call TwoDimensionalShape and put common methods in it.
public interface TwoDimensionalShape {
double calculateArea();
int calculatePrimeter();
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
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());
}
Run Code Online (Sandbox Code Playgroud)
小智 5
Create an Interface For eg. Shape.
public interface Shape {
int calculateArea();
int calculatePrimeter();
}
Run Code Online (Sandbox Code Playgroud)
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