English 中文(简体)
消除一种方法,使之固定不变
原标题:Override a method and make it behave static

我有一个可以改变的现有框架,即2个特性。

  • ClassA=somepackage.AnImplementationOfInterfaceA
  • ClassB=somepackage.AnImplementationOfInterfaceB

电话public methodsA on a new categoryA(,public methodsB on a new categoryB( in that order

我愿制作一个<条码>的C,用以实施A、B和并提供条码<条形码>、<条码>、<条码>、<条码>至上>(<条码>>;<条码>;<条码>> 甲型六氯环己烷/甲型六氯环己烷>;<条形状> /条码>含有大量碎块和复合体,用于实施-条码和>;<条码> 然后,我的财产将归结在我身上。

  • ClassA=somepackage.classD
  • ClassB=somepackage.classD

问题在于,执行D类的人可能会被诱惑撰写诸如:

class D extends class C
{
   private int foo; //must be non-static due to multi-threaded new Class D() calls going on
   int methodC1() {this.foo = read number from network}
   methodC2(int x) {y = this.foo;} //methodC2 is always called after methodC1

   //methodA, methodB implementation inherited from C
}

但是,由于该框架实际上将产生一个新的目标,即:<代码>D<>/代码>。 在援引<代码>methodA之前,>methodB,从而不能依靠使用“这一”参考资料。

Defining methodC1, methodC2 as static 要么是因为对<代码>methodC1的号召与在<代码>C上的执行有关,而不是与在<代码>上的首要要求有关。 D 。

实际需要的是:

class D extends class C
{
   int methodC1() {return number from network;}
   methodC2(int x) {y = x}  //here y is using the return value of methodC1

   //methodA, methodB implementation inherited from C
}

我也希望 只有<>><>>>>>>> > > 方法1< 代码> 方法2 , 才能高于标准,即:在D领域工作的方案人员可使用< 代码>methodA。

理想的设计本来可以做到。

  • the properties refer to one class only
  • methodC1, methodC2 to be in that class

挑战摘要

  • no this in methodC1, methodC2
  • can t make methodC1, methodC2 static
  • properies takes only a instantiable class

我如何设计这一框架? 难道这甚至可以降低一半? 您可更改<代码>methodC1的签名,methodC2

问题回答

组成 建立你与延伸者之间的接口。

这一类别将包含方法学D1和D2的逻辑,而对于其他一切来说,只是简单地呼吁你目前类别的其他方法。 人民赢得了变革基本逻辑的呼声。

静态方法不能压倒一切!

如果子类对静态方法下定义,其签字与超级舱内固定方法相同,则子级的方法隐藏在超级舱内。 藏匿和压倒一切之间的区别具有重要影响。





相关问题
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 ...

热门标签