简体   繁体   English

如何从父的静态方法调用child的静态方法?

[英]How can i call child's static method from parent's static method?

I have a Base class 我有一个基类

public class BaseStatic {
    public static String fname = "Base";
    public static String lname = "Static";

    public static void send(){
        System.out.println("BaseStatic send");
        sendTo();
    }

    public static void sendTo(){
        //How to call from here Child's static method.
        System.out.println("BaseStatic sendTo");
    }
}

and I have a Child Class which extends it. 我有一个扩展它的Child Class。

public class FirstStatic extends BaseStatic {
    public static String fname = "First";
    public static String lname = "Static";

    public static void sendTo(){
        System.out.println("FirstStatic sendTo");
    }
}

Now there is an Main class 现在有一个Main类

public class Main {
    public static void main(String args[]){
        FirstStatic.send();
    }
}

Does java provide me a way so that when i call from Main method FirstStatic.send , It goes to send method of BaseStatic , from there i can call sendTo method of FirstStatic rather than calling sendTo method of BaseStatic java是否为我提供了一种方式,当我从Main方法FirstStatic.send调用时,它转到BaseStatic的send方法,从那里我可以调用FirstStatic的sendTo方法而不是调用BaseStatic的sendTo方法

There is no polymorphism for static methods. 静态方法没有多态性。 Therefore, in order to call a static method x of class A , you must write Ax() . 因此,为了调用类A的静态方法x ,必须编写Ax()

FirstStatic.send() will call BaseStatic 's send only because FirstStatic has no static send method. FirstStatic.send()将调用BaseStaticsend ,只是因为FirstStatic没有固定send方法。 However, BaseStatic 's send will always call BaseStatic 's sendTo , unless you explicitly call FirstStatic.sendTo() . 然而, BaseStaticsend总是会调用BaseStaticsendTo ,除非你明确地调用FirstStatic.sendTo()

The commented-out sendTo() method here would have the effect of hiding the implementation of sendTo() in the base class. 这里注释掉的sendTo()方法可以隐藏基类中sendTo()的实现。

public class BaseStatic {
    public static String fname = "Base";
    public static String lname = "Static";

    public static void send(){
        System.out.println("BaseStatic send");
        sendTo();
    }

    public static void sendTo(){
        //How to call from here Child's static method.
        System.out.println("BaseStatic sendTo");
    }

    public static void main(String[] args) {
        FirstStatic.sendTo();
    }
}

class FirstStatic extends BaseStatic {
    public static String fname = "First";
    public static String lname = "Static";

    //public static void sendTo(){
    //    System.out.println("FirstStatic sendTo");
    //}
}

You can do the following, of course, but the net effect is identical to the code shown above. 当然,您可以执行以下操作,但净效果与上面显示的代码相同。 If you need polymorphism, you'll need to do away with the statics--probably a good idea, anyway! 如果你需要多态性,你需要取消静态 - 无论如何可能是一个好主意!

public class BaseStatic {
    public static String fname = "Base";
    public static String lname = "Static";

    public static void send(){
        System.out.println("BaseStatic send");
        sendTo();
    }

    public static void sendTo(){
        //How to call from here Child's static method.
        System.out.println("BaseStatic sendTo");
    }

    public static void main(String[] args) {
        FirstStatic.sendTo();
    }
}

class FirstStatic extends BaseStatic {
    public static String fname = "First";
    public static String lname = "Static";

    public static void sendTo(){
        BaseStatic.sendTo();
    }
}

EDIT: These examples are structured such that you can easily do the following to verify the behavior. 编辑:这些示例的结构使您可以轻松执行以下操作来验证行为。

javac BaseStatic.java
java BaseStatic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从静态main()方法调用内部类的方法 - how to call inner class's method from static main() method 如何在JAVA中从父类的内部类中调用子类中的重写方法? - How can I call the overridden method in a child class from a parent class's inner class in JAVA? 我可以从静态方法中调用非静态方法吗? - Can I call a non static method from a static method? 是否可以从父类型调用子 static 方法? - Is it possible to call child static method from parent type? 我可以从构造函数的主体中调用静态方法来获取实例,而不是调用Super吗? - Can I call a static method from my constructor's body to get at the instance instead of calling Super? 如何在ActivityGroup的ActivityGroup的子Activity中调用非静态方法? - How can I call a non-static method in a child Activity of an ActivityGroup from the ActivityGroup? 如何在jstl中调用静态方法? - How can I call a static method in jstl? 如何使用列表调用方法 <Child> ,但方法的参数为List <Parent> ? - How to call a method with List<Child>, but method's parameter is List<Parent>? 从父类调用子类的静态方法 - Calling subclass's static method from parent class 如何判断它是对静态方法的调用还是对实例方法的调用? - How can I tell whether it is a call to a static method or an instance method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM