变量阴影并从java中的抽象类继承具体方法

时间:2022-09-25 07:33:10

I'm working on an assignment has a class hierarchy like blow:

我正在进行一项任务,有一个类层次结构,比如打击:

abstract EventClass

someEventClass1 extends EventClass

someEventClass1扩展了EventClass

someEventClass2 extends EventClass

someEventClass2扩展了EventClass

inside briefly:

abs EventClass{
//not in use just make it compile
int priority;
concrete compareTo(){
   //need access priority when working in subclass}
}
someEventClass1 extends EventClass{
  int priority;
}

in my EventClass I have a concrete method compareTo(Event in), which i expect it inherit by all subclasses

在我的EventClass中,我有一个具体的方法compareTo(Event in),我希望它可以继承所有子类

it will compare the priority(a int) of different events. Obviously, inside this method I need to access priority within the subclasses. To make it compile I have to add a int priority in EventClass, and I was thinking I can shallow it in subclasses and it will works just fine with the compareTo inherited.

它将比较不同事件的优先级(int)。显然,在这个方法里面我需要访问子类中的优先级。为了使它编译,我必须在EventClass中添加一个int优先级,我想我可以在子类中对它进行浅化,并且它可以在compareTo继承的情况下正常工作。

However, It's not working properly, it seems the compareTo is not working.

但是,它没有正常工作,似乎compareTo无法正常工作。

If I erase the int priority and compareTo() in EventClass, put the compareTo into all my subclasses(ugly duplicated code). It works just fine...

如果我在EventClass中删除int优先级和compareTo(),则将compareTo放入我的所有子类中(丑陋的重复代码)。它工作得很好......

Can some one help me to design this? Or tell me what I missed..

有人可以帮我设计一下吗?或者告诉我我错过了什么..

Thanks for any help!

谢谢你的帮助!

2 个解决方案

#1


0  

Fields are not polymorphic in Java. Methods are. Add a getPriority() method to the Event class, and call this method from the compareTo method.

字段在Java中不是多态的。方法是。将一个getPriority()方法添加到Event类,并从compareTo方法调用此方法。

You'll then be able to override the getPriority method in the subclasses, and the appropriate method will be called, in a polymorphic way. Note that if the getPriority method is not used from the outside, you can make it protected.

然后,您将能够覆盖子类中的getPriority方法,并以多态方式调用相应的方法。请注意,如果未从外部使用getPriority方法,则可以使其受到保护。

Side note: please learn Java naming conventions and stick to them. And it would be easier to understand your question if you used real Java code rather than obscure pseudo-code.

附注:请学习Java命名约定并坚持使用它们。如果您使用真正的Java代码而不是模糊的伪代码,那么理解您的问题会更容易。

#2


0  

The additional declaration of int priority in the subclass shadows int priority from the superclass. Try this:

子类中int优先级的附加声明从超类中隐藏int优先级。试试这个:

abstract class Event {
    protected int priority;
    public int compareTo(Object other) {
        Event ev = (Event) other;
        return priority - ev.priority;
    }
}

class SomeEvent extends Event {
}

Whether you declare your priority as protected or as private and provide a corresponding getter method is another topic.

无论是将您的优先级声明为受保护的还是私有的,并提供相应的getter方法都是另一个主题。

#1


0  

Fields are not polymorphic in Java. Methods are. Add a getPriority() method to the Event class, and call this method from the compareTo method.

字段在Java中不是多态的。方法是。将一个getPriority()方法添加到Event类,并从compareTo方法调用此方法。

You'll then be able to override the getPriority method in the subclasses, and the appropriate method will be called, in a polymorphic way. Note that if the getPriority method is not used from the outside, you can make it protected.

然后,您将能够覆盖子类中的getPriority方法,并以多态方式调用相应的方法。请注意,如果未从外部使用getPriority方法,则可以使其受到保护。

Side note: please learn Java naming conventions and stick to them. And it would be easier to understand your question if you used real Java code rather than obscure pseudo-code.

附注:请学习Java命名约定并坚持使用它们。如果您使用真正的Java代码而不是模糊的伪代码,那么理解您的问题会更容易。

#2


0  

The additional declaration of int priority in the subclass shadows int priority from the superclass. Try this:

子类中int优先级的附加声明从超类中隐藏int优先级。试试这个:

abstract class Event {
    protected int priority;
    public int compareTo(Object other) {
        Event ev = (Event) other;
        return priority - ev.priority;
    }
}

class SomeEvent extends Event {
}

Whether you declare your priority as protected or as private and provide a corresponding getter method is another topic.

无论是将您的优先级声明为受保护的还是私有的,并提供相应的getter方法都是另一个主题。