父类

时间:2016-04-25 14:54:19

标签: java inheritance

我有一个在每个子类中重复的相同方法,我想将它重构为超类中的一个方法。

  public class SubClass1 extends SuperClass {
       private BoltHexHead bolt;
       private void computeFoo() {
          //Foo formula is identical in all subclasses. Need to move up
          setFoo(bolt.getDiameter() + bolt.getPitch() + bolt.getTpi());
       }
       private void computeBar() {
          //computeBar method in all subclasses but Bar formula is different amongst all subclasses
          setBar(bolt.getDiameter() - 2*bolt.getPitch() - 3*bolt.getTpi());
       }
       private void computeSeparation() {
          //computeSeparation method only exists for a Subclass 1
          setSeparation(bolt.getLength() - 2*nut.getFlatDia());
       }



  public class SubClass2 extends SuperClass {
       private BoltFlatHead bolt;
       private void computeFoo() {
          //Foo formula is identical in all subclasses. Need to move up
          setFoo(bolt.getDiameter() + bolt.getPitch() + bolt.getTpi());
       }
       private void computeBar() {
          //computeBar method here is different than in Subclass1
          setBar(bolt.getDiameter() - 4*bolt.getPitch() - 1/3*bolt.getTpi());
       }
       private void computeProtrusion() {
          //computeProtrusionmethod only exists for a Subclass 2
          setProtrusionmethod(bolt.getThreadAngle() - 6*bolt.getTpi());
       }

最初我发布bolt没有在SuperClass中设置但是在SubClass中。我在初次发布后的工作内容如下:

public abstract class SuperClass {
    protected Bolt<?> bolt;  <-- this was added but uses wildcard
    ...bolt getters/setter

       protected void computeFoo() {
          //Foo formula pulled up from all subclasses
          setFoo(bolt.getDiameter() + bolt.getPitch() + bolt.getTpi());
       }
  }

public class SubClass1 extends SuperClass {
  //private BoltHexHead bolt;   <-- commented this out in each subclass
}

这是一个JSF应用程序,在每个控制器bean中,我实例化特定的联合属性子类,然后设置特定的螺栓。早期的设计决定是使用setter来设置子类中的bolt(和其他属性),而不是使用Constructor进行设置;但是一次只有一个重构。

Controller for a Bolt Analysis using a HexHead Bolt
    private SubClass1 sc1 = new SubClass1();
    private BoltHexHead bolt;
    sc1.setBolt(bolt);
    sc1.computeResults();

Controller for a Bolt Analysis using a FlatHead Bolt
    private SubClass2 sc2 = new SubClass2();
    private BoltFlatHead bolt;
    sc2.setBolt(bolt);
    sc1.computeResults();

所以我的问题是,可以使用通配符Bolt<?> bolt还是有更好的方法?

1 个答案:

答案 0 :(得分:1)

  

我只想尝试将所有子类中相同/重复的方法放入父级,但其中一个变量(bolt)未设置

那是因为在java中你无法覆盖字段。 所以你的变量&#34;私人B螺栓;&#34;在你的超类和&#34;私人BoltHexHead螺栓;&#34;在你的子类中有两个不同的东西。它们实际上都存在于同一时间。

你要做的事情实际上并不复杂。您只需要清理代码:

  1. 仅定义&#34;私人B螺栓;&#34;和你的超级类中的setter / getters一样。
  2. 仅使用那些getters / setter来访问bolt
  3. 如果你想让你的子类有一个&#34; bolt&#34;类型&#34; BoltHexHead&#34;然后在类定义("extends JointAttribute<BoltHexHead>" instead of "extends JointAttribute<Bolt<BoltSpec>>")
  4. 中定义泛型参数

    演示的简单示例:

    超类:

    public class Superclass<T> {
    
        private T value;
    
        protected T getValue() {
            return value;
        }
    
        protected void setValue(T value) {
            this.value = value;
        }
    
        protected void print() {
            if(getValue()==null) {
                System.out.println("NULL");
            } else {
                System.out.println(getValue().toString());
            }
        }
    }
    

    Subclass1:

    public class Subclass extends Superclass<String> {
    
        public Subclass() {
        }
    
        public static void main(String[] args) {
            Subclass subclass= new Subclass();
            subclass.print();
            subclass.setValue("test");
            subclass.print();
        }
    }
    

    Subclass2:

    public class Subclass2 extends Superclass<Integer> {
    
    
        public Subclass2() {
        }
    
        public static void main(String[] args) {
            Subclass2 subclass= new Subclass2();
            subclass.print();
            subclass.setValue(3);
            subclass.print();
        }
    }