English 中文(简体)
在 Java使用特定类别方法(无多种形态)
原标题:Call a specific class method in Java (without polymorphism)

我在 Java有问题。 我的方法可以扩大。 问题在于,这种方法指其他类别方法,也可以推广。

Consider the following class:

public class Foo {
    protected int xLast;

    public updateMe(int x, int y) {
        updateX(x);
    }

    protected updateX(int x) {
        this.xLast = x;
    }
}

班级延长如下:

public class Bar extends Foo {
    protected int xAverage = 0;
    protected int xCount = 0;
    protected int y;

    public updateMe(int x, int y) {
        super.updateMe(x, y);       
        updateX(x);
        updateY(x);
    }

    protected updateX(int x) {
        this.xAverage = (this.xAverage * this.xCount) + x;
        this.xCount++;
        this.xAverage /= xCount;
    }

    protected updateY(int y) {
        this.y = y;
    }
}

也是按这一类别:

public class Abc extends Foo {
}

当我做以下工作时:

Foo myBar = new Bar();
myBar.updateMe(1, 2);

<代码>Foo.updateX 无需使用<代码>Bar.updateX方法。 有一些解决办法,有些办法没有效果,或者很简单:

  1. In Bar.updateX call super.updateX. This will cause both updateX methods to be called twice.
  2. Remove the updateX call in Foo.updateMe, forcing the extending classes to call super.updateX. This will cause the class Abc to have useless code that it doesn t need, or else the Abc class will not call updateX at all.
  3. Rename the methods so that they do not override each other. This will work, but is not safe (in the future this might be forgotten and lead to problems) and is not enforced by the language.

我知道,这在一定程度上属于法典范畴,但我看不到这样做的更好办法。

Basically I am looking to do something like this: in Foo.updateMe I would like to call Foo.updateX specifically, and not just the polymorph-ed updateX.

我认为,像<代码>新

<><>Edit>:

最后,我选择重新命名犯罪方法。 我只有一个办法造成这一问题,这里建议的解决办法是,尽管从设计观点看,这一特定法律将更难以理解和维持。

最佳回答

页: 1

  1. Remove updateX() call from Bar.updateMe() since updateX() is already called by super.updateMe().
  2. Call super.updateX() from Bar.updateX() since the method with the same name is supposed to extend functionality, not replace it.

在此情况下,必须改变Abc胜诉的编号,同时将采用更新的X()方法。

问题回答

Java,所有实例方法都通过具有动态约束力的手段发出。 我知道,没有实现你想要的东西的leg。

如果你关心原则和遗产,你可以检查福oo的某类案例。

public updateMe(int x, int y) { 
   if (getClass()==Foo.class)         
        updateX(x);     
}  

看来,你要取消欧佩组织。 首先是坏的。 如果你需要改变设计。 Java纯粹是面向目标的语言,因此所有电话都是多变的。 如果您延长任期,如果其职能超出部分,就意味着你们需要这样做,因此,应当要求这一功能。 因此,请在<条码>上打上<条码>。

BTW要做你想要做的事情,就应当创造静态的方法,并以这种方式:Foo.updateX()。

你们还可以使更新的X()成为私人。 然后,实施更新X(X)的子类是“编码”,而不是“过度”方法。

Drawbacks: Since updateX() is conceptually part of a Template pattern, it really "feels" like it should be protected.

You can t put abstract private void updateX(); in the superclass to force users to implement it.

最新措施的所有执行情况都必须永远记住,需要更新X()

可能的反馈和可能的好处:

所有私人更新措施方法都将被采用。 (就我而言,YMMV希望这样做)

我最近面临这一问题,并选择了这一解决办法,尽管我考虑重新命名方法和非常接近(植被委员会=MyFoo.class)的 something点。 海事组织都是合理的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签