1

像下面的两个类,除了调用不同版本的参数外,它们看起来几乎相同,那么如何重构它们呢?

import com.v1.B;
import com.v1.C;
class A1
{
  public A1(B,C)
  public dosomething1()
  {
    //do something with B and C
  }
  public dosomething2()
  {
    //do something
  }
}

A2 类:

import com.v2.B;
import com.v2.C;
class A2
{
  public A2(B,C)
  public dosomething1()
  {
    //do something with B and C
  }
  public dosomething2()
  {
    //do something
  }
}
4

1 回答 1

1

您应该首先为这两种类型创建一个基类或接口,例如com.v.BaseB用于com.v1.Bcom.v2.B促进多态性,然后您可以使用单个泛型类而无需任何重复,例如:

import com.v.B;
import com.v.C;
class A
{
  public A(B,C)
  public dosomething1()
  {
    //do something with B and C
  }
  public dosomething2()
  {
    //do something
  }
}
于 2012-12-19T04:38:21.920 回答