0

I'm new to Java but not to basic programming. Because of this, I have not yet fully learned all of the terms and their meaning. With that being said, I may not be describing what I want so I'll try to be as specific as possible.

I am writing an abstract parent class Status which will parent subclasses such as (Buff) and (Debuff) which will most likely be abstract as well. Suffice to say that eventually I will be writing concrete classes with their own class names that inherit, in some way, from Status.

The idea is to iterate through a list of these objects and have them modify another Dragon.object. The order becomes important though because these Status objects mathematically modify values in another class of objects. Because of this and the fact that there is no order that these objects will be instantiated, I would end up with unintended results when one Status increases a value by 10% and another subtracts 5 from that value.

The reason I provide this background is because I'm not sure that I am approaching this problem correctly and hope that if I am going down the wrong path, someone can correct me here.

On to my question; it seems easy enough to compare objects by a value in one of their fields. Does this hold true for class types as well? If so, how would I write a comparator that compares object types. By "object types", i mean;

compare: Status.Buff.Percentage.Strength10Percent to Status.Buff.Additive.Strength10

Should I just place a field in this class that designates it's priority and then use a custom comparator when sorting or is the object type enough (provided Java can do that)?

Thanks for reading.

4

3 回答 3

1

如果您想通过他们都拥有的东西进行比较,即Buffor中的某个字段Status,这是可能的。Buff您可以在这种情况下或在这种情况下使用非抽象的 compareTo 方法Status(确保 class implements Comparable)。由于子类继承自Buffor Status,因此将从子类调用相同的compareTo()方法,除非它们覆盖它(实际上不应该这样做)。

于 2013-04-24T05:14:54.373 回答
0

您可能需要一个优先级字段。

话虽如此,如果这足以让您找到优先级,您可以使用this.getClass().getName()获取当前类的名称。然后,您可以在 compareTo 方法中使用类名。

您还可以使用instanceof来检查特定对象是否是特定类的子类的对象。例如,参考您的示例:

的对象Status.Buff.Percentage.Strength10Percent将返回 true:

obj instanceof Percentage

或者

obj instanceof Buff

于 2013-04-24T05:55:08.543 回答
0

我认为您对设计采取了错误的方法。您要做的是计算 RPG 的修饰符,对吗?如果是这种情况,您应该让他们都用一个apply(Character c)方法实例化一个接口,该方法封装修改并将其应用于角色(或机器人或任何您应用修改器的对象)。应该实现一个接口,该Character接口具有要修改的属性的 getter 和 setter,以便apply()可以进行调整。

于 2013-04-24T07:44:07.963 回答