【问题标题】:Java: Use Static methods of Parent Class in Child ClassJava:在子类中使用父类的静态方法
【发布时间】:2014-05-03 17:05:06
【问题描述】:

我正在尝试使用BaseComponentType 类重构我的代码,并在我的ElectricalComponentType 类(和类似的子类)中继承此代码,如下所示:

BaseComponentType.java

public abstract class BaseComponentType {

    public static BaseComponentType findByUid ( Class klass, String uid ) {

        return new Select().from( klass ).where( "uid = ?", uid ).executeSingle();

    }

}

ElectricalComponentType.java

public class ElectricalComponentType extends BaseComponentType {

    public static ElectricalComponentType findByUid( String uid ) {

        return (ElectricalComponentType) findByUid( ElectricalComponentType.class, uid );

    }

}

我需要做的是调用ElectricalComponentType.findByUid( 'a1234' ),但如果我不必在ElectricalComponentType 类中定义findByUid 而是可以从BaseComponentType 继承此功能,那就太好了。

您会注意到有两件事阻碍:

  1. 我需要 ElectricalComponentType 父方法中的 ElectricalComponentType 类。

  2. 我需要返回 ElectricalComponentType 对象(或任何子类对象)而不是 BaseComponentType 类对象。

有没有办法做到这一点?

【问题讨论】:

  • “删除”findByUid 方法是什么意思?不能直接删除吗?而且您的抽象类似乎没有任何抽象方法,因此您不必“必须”覆盖任何东西。这里的动机是什么?
  • 如果您的 ElectricalComponentType 中不需要 findByUid 方法,那您为什么要在那里编写呢?我敢说删除它的最好方法是从一开始就没有对其进行编程。
  • 您可能想看看这里:stackoverflow.com/questions/10291949/… 或右侧任何其他关于静态方法和继承的讨论。
  • “删除”是指我不想在ElectricalComponentType 模型中定义它,但我仍然需要在ElectricalComponentType.findByUid( 'a433' ) 中进行。我会稍微更新一下问题。
  • @JoshPinter 您不需要覆盖除抽象方法(您未在示例中定义)之外的任何方法。

标签: java inheritance abstract-class static-methods


【解决方案1】:

使用泛型,只有父类方法:

public abstract class BaseComponentType {
    public static <T extends BaseComponentType> T findByUid(Class<T> klass, String uid) {
        return new Select().from( klass ).where( "uid = ?", uid ).executeSingle();
    }
}

【讨论】:

  • 这正是我所寻找的,正是。谢谢。
  • 所以这个格式很特别,需要&lt;T extends BaseComponentType&gt; T。你有一个链接我可以阅读更多吗?
  • 很高兴你喜欢它。欲了解更多信息,请从official docs 开始。如果您需要更多 google “java generic methods”或“java typed methods”
  • 谢谢。最后一件事,是否可以以编程方式确定子类,以便我可以删除 Class&lt;T&gt; klass 函数参数?
  • 如果from() 方法需要class 类型为T 的对象(很有可能),那么“否”就无法避免传递class 对象,因为由于运行时类型擦除,您无法在运行时创建 T 类型的 class 对象。
【解决方案2】:

有几点需要注意:

  • static 方法没有被继承,也不能被覆盖;
  • 要调用父类的static方法,必须先写类名:BaseComponentType.findById();

如果您想摆脱子类中具有相同名称的方法,您可能希望将其设为非静态或/并重新考虑您的类设计,因为如果您在绑定的类中有两个同名的静态方法继承关系,很可能是类设计有问题。

【讨论】:

    【解决方案3】:

    我希望你需要类似以下的东西..

    public class TestClass{
        public static void main(String args[]){
            Child c2;
            c2 = (Child) Child.findByUid(Child.class, "123");
            System.out.println(c2.getClass());
        }            
    }
    
    class Base{
        public static Base findByUid ( Class klass, String uid ) {
            System.out.println(klass);
            Child c = new Child();
                //execute your query here and expect it to return the type of object as the class by which it was called
            //your parent class method always returns the type of the child by which the method was called
            return c;
    
        }
    
    }
    
    class Child extends Base{
        /*public static Child findByUid( String uid ) {
            System.out.println(Child.class);
            return (Child) findByUid( Child.class, uid );
    
        }*/
    }
    

    【讨论】:

      【解决方案4】:

      我认为您可以重新设计它,以便您拥有一个找到 Components 的 ComponentFinder 类。

      public class ElectricalComponent extends Component {
      
           @Override
           public void method1(){
               //specific stuff
           }
      
      }
      
      public abstract class Component{
           public abstract void method1();
      }
      
      public class ComponentFinder{
      
          public static Component findByUid ( Class klass, String uid ) {
      
              return new Select().from( klass ).where( "uid = ?", uid ).executeSingle();
      
          }
      
      }
      

      那么你就不用担心继承问题了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-08
        相关资源
        最近更新 更多