[英]Java Subclasses that have different primitive data members, but same methods
I'm writing a Ray Tracer and I have a Vec3 class which is a typical math vector with 3 coordinates: X, Y, and Z. I've also created a Color class that holds 3 components: R, G, and B. After writing a decent amount of code, I started to notice that Color is basically a Vec3. 我正在编写Ray Tracer,我有一个Vec3类,这是一个典型的数学向量,具有3个坐标:X,Y和Z。我还创建了一个Color类,其中包含3个分量:R,G和B。在编写了大量代码之后,我开始注意到Color基本上是Vec3。 I started to write a lot of the same methods for Color as I did for Vec3.
我开始为Color编写许多与Vec3相同的方法。 Seeing that DRY code is the way to go, I wanted to merge the two classes so I didn't have to keep writing the same code for both classes.
看到DRY代码是路要走,我想合并两个类,这样我就不必继续为两个类编写相同的代码。
As it stands, here are the classes: 就目前而言,以下是这些类:
class Vec3{
public static final int X=0,Y=1,Z=2;
double components[];
...
// Constructors and Methods
}
class Color extends Vec3{
public static final int R=0,G=1,B=2;
int components[];
...
// Constructors and Methods
}
The only difference between the two classes is that a Vec3 uses doubles for its components and a Color uses ints. 这两个类之间的唯一区别是,Vec3的组件使用双精度,而Color则使用整数。 Other than the type of data, all of the methods are exactly the same.
除了数据类型之外,所有方法都完全相同。 My original inclination was to try and make a BaseVec3 class of generic type T and then just declare the type in the subclasses.
我最初的意图是尝试创建通用类型T的BaseVec3类,然后在子类中声明该类型。 But, of course, a generic requires a class, not a primitive.
但是,当然,泛型需要一个类,而不是原始类。
Is there any way I can have a single method code-base but with different primitive data types? 有什么办法可以使一个方法代码库具有不同的原始数据类型?
Edit: I should probably note that I have been programming in Python and C for a while and it's been a while since I've touched Java. 编辑:我可能应该注意,我已经用Python和C编程了一段时间了,自从接触Java以来已经有一段时间了。 So, my first instinct answer to this question is NO .
因此,我对这个问题的第一个直觉答案是“ 否” 。
I'm not 100% sure of what you're asking, but if your code is as you've shown, don't you have what you want? 我不确定您要问什么,但是如果您的代码如您所显示的那样,我不是100%知道吗? In your code,
Color extends Vec3
. 在您的代码中,
Color extends Vec3
。 Due to this statement, a Color
object can be treated as a Vec3
object; 由于此声明,可以将
Color
对象视为Vec3
对象。 all methods from Vec3 are inherited by Color and methods which take an arg of type Vec3 will accept args of type Color. Vec3中的所有方法均由Color继承,并且采用Vec3类型的arg的方法将接受Color类型的args。
Each primitive has its corresponding class. 每个原语都有其对应的类。 For example,
Integer
for int
. 例如,
Integer
for int
。 Java can autobox them, ie convert them automatically. Java可以将它们自动装箱,即自动将它们转换。
However, I don't think using inheritance is appropriate in this case. 但是,在这种情况下,我认为使用继承不合适。 What if you
Vec3
implementation change? 如果您的
Vec3
实施发生变化该怎么办? What if you decide to make you members private (as you should)? 如果您决定将您的成员设为私有(应该这样做)怎么办?
EDIT - I see your comment about generics at the end. 编辑-我在最后看到了您对泛型的评论。 Isn't that what autoboxing is for?
那不是自动装箱的目的吗?
Couldn't you just use a generic like this? 您不能只使用这样的泛型吗?
public abstract class GenericVector<T> {
private final T x, y, z;
public GenericVector(T x, T y, T z) {
this.x=x;
this.y=y;
this.z=z;
}
public T getX() { return x; }
public T getY() { return y; }
public T getZ() { return z; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.