父抽象类的构建

问题描述 投票:0回答:1

我有两个类(我们称它们为A和B),它们所包含的一些方法(例如......)非常相似。doSomething(...)),它们所包含的方法非常相似,实际上它们共享相同的代码。我决定让我的代码更有效,并使用某种父类抽象类C,A,B将从该类中继承。

这是我的问题。方法 做某事 类A中的方法有如下签名。

doSomething(VievForA view, ...)

而同一个方法 做某事 类B的签名是这样的。

doSomething(VievForB view, ...)

如你所见,doSomething方法使用了不同类型的第一个参数 所以我不知道如何在抽象类中解决这个问题。

正确的构造C类的方法是什么,doSomething方法应该是什么样的? 我希望能很好地解释我的情况,否则我可以再添加一些伪代码。

java oop model-view-controller abstract-class
1个回答
3
投票

你也可以有一个基础视图类,它将被ViewA和ViewB继承,并使用ViewBase作为doSomething的参数。


2
投票

你可以使用Java结构你的代码 仿制药 如下所示。

// View Classes
//====    

/**
 * The generic view parent class
 */
public abstract class GenericView{
}

/**
 * The specific A view class
 */
public class ViewForA extends GenericView{
    //...
}

/**
 * The specific B view class
 */
public class ViewForB extends GenericView{
    //...
}

// View Handler Classes
//====

/**
 * The generic view handler
 * @param <T> The view object that extends from GeneriView
 */
public abstract class C<T extends GenericView>{

    private void doGenericSomething(GenericView t) {
        //Do something generic on all views
    }

    //Do something specific on specific view
    abstract void doSpecificSomething(T t);

    //Do all stuff
    protected void doSomething(T t){
        doGenericSomething(t);
        doSpecificSomething(t);
    }

}

/**
 * The specific A view handler
 */
public class A extends C<ViewForA>{

    @Override
    void doSpecificSomething(ViewForA view) {
        //Do your stuff with viewForA
    }
}

/**
 * The specific B view handler
 */
public class B extends C<ViewForB>{

    @Override
    void doSpecificSomething(ViewForB view) {
        //Do your stuff with viewForB
    }
}

这样你就可以插入你的 普通 码上 doGenericSomething(GenericView t) 体在C类,然后让每个具体的视图类,如A类和B类来实现 特定 码上 doSpecificSomething(ViewForSpecific t) 其中ViewForSpecific是一个继承自GenericView的类(就像共享代码中的ViewForA和ViewForB)。


1
投票

你可以使用一个共同继承的类作为方法的参数。

doSomething(CommonViev view, ...)
{
   if (view instanceof VievForA)
   {
      VievForA vievForA = (VievForA)view;
      ...
   }
   else if (view instanceof VievForB)
   {
      VievForB vievForB = (VievForB)view;
      ...
   }
}

0
投票

如果你想维护两个扩展C的类A和B,那么在C类中,你可以做这样的声明。

doSomething(Object view, ...)

而在A类或B类中,使用下面这样的方法。

doSomething(Object view, ...){

VievForA(B) vievForA(B) = (VievForA(B)) view;     //basically typecast it as per child class

}


0
投票

假设您的 doSomething() 有重复的代码,您可以在 abstract 阶层 C 并将其用于在 AB.

abstract class C {  
    protected Result doSomething(CommonStuff common){
        // doing common stuff and
        return result;  // returning the result
    }
} 


class A extends C { 
    public void doSomething(ViewForA view, ..){
         CommonStuff common = view.getStuff();
         Result result = this.doSomething(common);
         view.applyResult(result);
    }
}

但这是非常...抽象的例子。如果你的ViewForA和ViewForB有任何形式的通用接口,甚至用这个更好。一旦我用了这样的通用接口。

 abstract class C {

      abstract protected ViewInterface getView();

      protected void doSomething(){
           this.getView().doStuffWithView();
      }
 }


 class A extends C {
     private ViewForA myViewForA; 

     @Override
     protected ViewForA getView(){
          return this.myViewForA;
     }
  }

那么抽象类就可以处理 doSomething 假设会有任何一种通用的View实例被使用。而类A和类B将自动实现共同的 doSomething 你可以用它像

A a = new A();
a.doSomething();
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.