如何在java中为两个类实现一个通用方法
我有一个有一个普通方法和一个通用方法的接口。我已经为两个不同的类实现了普通的方法,但是现在不用如何用泛型方法来实现。这里是我的代码:如何在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();
}
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
你能帮我解决这个问题吗?我非常感谢你的帮助!
的类层次看起来不协调。例如,您同时拥有ThreeDShape extends GeometricShape<ThreeDShape>
和TwoDShape extends GeometricShape
,原因不明。为这些类型编写泛型方法并不有趣。
这是一个不太令人困惑的版本。 (我希望)注意:我选择不在supersize方法中更改形状本身的大小,而是让它在保持原始大小不变的情况下返回更大的形状。
1. GeometricShape
/**
* A geometric shape interface. You can do two things with it.
* 1. Ask it to describe itself (to stdout);
* 2. Ask it to return a bigger version of itself (double the size).
*/
public interface GeometricShape<T extends GeometricShape<T>> {
/**
* Print a description to STDOUT
*/
void describe();
/**
* Returns a bigger shape.
* @return Something that's a GeometricShape
*/
T supersize();
}
2. Shape2D和矩形
/**
* A 2-dimensional shape.
* It has area.
* Its supersize() method should return a Shape2D instance.
*/
public interface Shape2D<T extends Shape2D<T>> extends GeometricShape<T> {
double area();
}
/**
* A rectangle.
*/
public final class Rectangle implements Shape2D<Rectangle> {
private final double width;
private final double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public String toString() {
return "Rectangle{" +
"width=" + width +
", height=" + height +
'}';
}
@Override
public void describe() {
System.out.println(this);
}
@Override
public Rectangle supersize() {
return new Rectangle(width*2, height*2);
}
@Override
public double area() {
return width * height;
}
}
3.了Shape3D和球
/**
* A 3-dimensional shape.
* It has volume.
* Its supersize() method should return a Shape3D instance.
*/
public interface Shape3D<T extends Shape3D<T>> extends GeometricShape<T> {
double volume();
}
/**
* A sphere
*/
public final class Sphere implements Shape3D<Sphere> {
private final double radius;
public Sphere(double radius) {
this.radius = radius;
}
@Override
public String toString() {
return "Sphere{" +
"radius=" + radius +
'}';
}
@Override
public void describe() {
System.out.println(this);
}
@Override
public Sphere supersize() {
return new Sphere(radius*2);
}
@Override
public double volume() {
return 4*Math.PI*Math.pow(radius, 3)/3;
}
}
现在转换列表
public static <T extends GeometricShape<T>>
List<T> supersize_list(List<T> list) {
List<T> result = new ArrayList<>();
for (T shape : list) {
result.add(shape.supersize());
}
return result;
}
谢谢@Luke Lee的好解释!但是,它在主体中的supersize_list方法中都显示错误,因此它仍然与supersize_list不匹配的球体和矩形,并且需要在它们前面进行强制转换才能正确运行。我不明白为什么会出现这个错误 –
你是否将结果投射到'ArrayList'?尝试将它们声明为'List'。 '列表
不,我没有投出结果,为了正确运行,我铸造了ArrayList
你不需要返回一个新的对象。对于Rectangle
例如
@Override
public void supersize()
{
this.width*=2;
this.height*=2;
}
足够
然后它不会是通用的。但对我来说很好。 –
在这种情况下,我应该将supersize更改为GeometricShape中的普通方法。我想在不改变它的情况下实现T supersize() –
@LukeLee由于'supersize'作用于具体对象的字段,因此不涉及泛型。 **超大的几何**也应该改变 –
我很困惑,为什么'supersize'将返回一个新对象的泛型方法,它应该不只是增加现有对象的大小? –
@ScaryWombat我同意,它应该做,但不是两者。 –
是的,我想在球体和矩形两次增加大小。你有什么建议?我想写一个supersize_list方法,返回尺寸增加的矩形和球体@Scary Wombat @ Luke Lee –