하나의 일반적인 메소드와 하나의 일반적인 메소드가있는 인터페이스가 있습니다. 나는 두 개의 다른 클래스에 대해 일반적인 메소드를 구현했지만, 지금은 일반적인 메소드로 그렇게하는 법은 없다.Java에서 두 클래스에 대해 하나의 일반 메소드를 구현하는 방법
Sphere.java :
public class Sphere implements GeometricShape<Sphere> {
private double radius;
public Sphere (double radius) {
this.radius = radius;
}
public double volume() {
return (4.0/3.0) * Math.PI * radius * radius * radius;
}
public void describe() {
System.out.println("Sphere[radius=" + radius + "]");
}
@Override
public Sphere supersize()
{
this.radius*=2;
return new Sphere(radius);
}
}
Rectangle.java
public class Rectangle implements TwoDShape {
private double width, height;
public Rectangle (double width, double height) {
this.width = width;
this.height = height;
}
public double area()
{
return width * height;
}
public double perimeter()
{
return 2.0 * (width + height);
}
public void describe()
{
System.out.println("Rectangle[width=" + width + ", height=" + height + "]");
}
@Override
public Rectangle supersize()
{
this.width*=2;
this.height*=2;
return new Rectangle(width, height);
}
}
TwoDShape.java :
public interface TwoDShape extends GeometricShape
{
public double area();
}
ThreeDShape.java :
public interface ThreeDShape extends GeometricShape<ThreeDShape>
{
public double volume();
}
,536 여기 내 코드입니다
GeometricShape.java :
public interface GeometricShape<T extends GeometricShape<T>>
{
public void describe();
public T supersize();
}
마지막으로 메인 클래스의 ArrayListExample.java :
import java.util.ArrayList;
public class ArrayListExample {
public static void describe_all(ArrayList<? extends GeometricShape> shapes)
{
for(int i=0;i<shapes.size();i++)
{
shapes.get(i).describe();
}
System.out.println("Total number of shapes:"+ shapes.size());
}
public static void main(String[] args) {
System.out.println("The describe() method:");
System.out.println();
System.out.println("Example rectangles");
ArrayList<Rectangle> rects = new ArrayList<Rectangle>();
rects.add(new Rectangle(2.0, 3.0));
rects.add(new Rectangle(5.0, 5.0));
describe_all(rects);
System.out.println();
ArrayList<Sphere> spheres = new ArrayList<Sphere>();
spheres.add(new Sphere(10.0));
spheres.add(new Sphere(50.0));
spheres.add(new Sphere(0.0));
System.out.println("Example spheres");
describe_all(spheres);
System.out.println();
System.out.println("The supersize() method:");
System.out.println();
ArrayList<Rectangle> double_rects = supersize_list(rects);
describe_all(double_rects);
System.out.println();
ArrayList<Sphere> double_spheres = supersize_list(spheres);
describe_all(double_spheres);
}
}
내가 supersize_list 방법을 구현할 수있는 방법이
같은 두 사각형 및 구 출력에서 초대형 방법을 취Rectangle[width=4.0, height=6.0]
Rectangle[width=10.0, height=10.0]
Total number of shapes: 2
Sphere[radius=20.0]
Sphere[radius=100.0]
Sphere[radius=0.0]
Total number of shapes: 3
도와 주시겠습니까? 도와 주셔서 감사합니다.
나는'supersize' 새로운 객체를 반환 이유에 혼란 스러워요을 변환하는 일반적인 방법이 아니라 기존 개체의 크기를 증가해야합니까? –
@ SkaryWombat 동의합니다.하지만 둘 중 하나만 수행해야합니다. –
예, 저는 구형과 사각형 모두에서 크기를 두 배로 늘리고 싶습니다. 당신의 제안은 무엇입니까? 증가 된 크기의 사각형과 구를 반환하는 하나의 supersize_list 메서드를 작성하고 싶습니다. @Scary Wombat @ Luke Lee –