Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:在子类中使用父类的静态方法_Java_Inheritance_Abstract Class_Static Methods - Fatal编程技术网

Java:在子类中使用父类的静态方法

Java:在子类中使用父类的静态方法,java,inheritance,abstract-class,static-methods,Java,Inheritance,Abstract Class,Static Methods,我试图通过使用BaseComponentType类重构代码,并在我的ElectricalComponentType类(以及类似的子类)中继承该类,如下所示: BaseComponentType.java ElectricalComponentType.java 我需要做的是调用ElectricalComponentType.findByUid('a1234'),但是如果我不必在ElectricalComponentType类中定义findByUid,而是可以从BaseComponentType继

我试图通过使用
BaseComponentType
类重构代码,并在我的
ElectricalComponentType
类(以及类似的子类)中继承该类,如下所示:

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

你会注意到有两件事阻碍了你:

  • 我需要
    findByUid
    parent方法中的
    ElectricalComponentType

  • 我需要返回
    ElectricalComponentType
    对象(或任何子类对象),而不是
    BaseComponentType
    类对象


  • 有办法做到这一点吗?

    有几点需要注意:

    • 静态
      方法未被继承,无法重写
    • 要调用父类的
      static
      方法,必须首先编写类名:
      BaseComponentType.findById()

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

    我希望您需要以下内容

    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 );
    
        }*/
    }
    

    我认为您可以重新设计它,这样您就有了一个
    ComponentFinder
    类来查找
    Component
    s

    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();
    
        }
    
    }
    

    这样您就不必担心继承问题。

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

    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();
        }
    }
    
    公共抽象类BaseComponentType{
    公共静态T findByUid(类klass,字符串uid){
    返回new Select().from(klass).where(“uid=?”,uid.executeSingle();
    }
    }
    
    findByUid方法的“删除”是什么意思?你不能直接删除它?而且您的抽象类似乎没有任何抽象方法,因此您不必“必须”重写任何内容。这里的动机是什么?如果在ElectricalComponentType中不需要findByUid方法,那么为什么要在那里编程?我敢说,删除它的最好方法是一开始就从未对它进行过编程。您可能想看看这里:或者右边任何一个讨论静态方法和继承的方法。所谓“删除”,我的意思是我不想在
    ElectricalComponentType
    模型中定义它,但我仍然需要执行
    ElectricalComponentType.findByUid('a433')
    。我会稍微更新一下这个问题。@JoshPinter除了抽象方法(您在示例中没有定义)之外,您不需要重写任何方法。这正是我想要的。谢谢。所以这个的格式非常特殊,需要
    T
    。你有我能读到更多的链接吗?很高兴你喜欢。有关更多信息,请从开始。如果您需要更多的谷歌“java通用方法”或“java类型方法”,谢谢。最后一件事,是否可以通过编程方式确定子类,以便删除
    类klass
    函数参数?如果
    from()
    方法需要类型为
    T
    对象,则“否”无法避免传递
    对象,因为由于运行时类型擦除,您无法在运行时创建类型为
    t
    class
    对象。
    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();
    
        }
    
    }
    
    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();
        }
    }