java静态调用虚方法 [英] java static calls of virtual methods

查看:74
本文介绍了java静态调用虚方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我创建了一个具有虚拟方法的类,该方法要在派生类中重写,但有时我希望该方法是静态调用同一类中的其他方法。



例如:

Hi,

I made a class that has a virtual method that is to overrided in a derived class, but sometimes I want that method is to call statically by other methods inside the same class.

An example:

class A extends BaseClass {
    public String init() {
        doSomethingWithVirtualMethodResult(virtualMethod());
        super.init();
    }
    protected String virtualMethod() {
        return something;
    }
}
class B extends A {
    public String init() {
        doSomethingWithVirtualMethodResult(virtualMethod());
        super.init();
    }
    protected String virtualMethod() {
        return somethingElse;
    }
}
class Main {
    public Main() {
        BaseClass obj = new B();
        obj.init();
        String virtualResult = obj.virtualMethod();
    }
}





我需要在init中调用doSomethingWithVirtualMethodResult方法somethingElse,但virtualResult的内容是什么东西。



怎么做?



谢谢你



I need that the method doSomethingWithVirtualMethodResult inside the init have to be called with both something and somethingElse, but the content of virtualResult is to be somethingElse.

How to do it?

Thank you

推荐答案

一切都没有意义。请注意,方法 doSomethingWithVirtualMethodResult ,尽管名称与 VirtualMethod 或其结果无关。它可以用任何字符串调用。



静态调用听起来很荒谬,但事实上,根本不清楚你的意思是什么。不涉及静态成员,因此您想要的与Java技术中与 static 相关的唯一明确定义的概念无关:静态成员vs 实例成员的。您的所有成员都是实例成员,即显式this参数传递给实例方法,以引用该类的特定实例。基本上,没有这样的实例就没有电话这样的事情。



所以,你需要一个解决方案,对吗?解决方案是:了解OOP的工作原理并正确解决您的问题。你的问题是无法解决的或无法解决的问题,它根本就没有意义。现在,我将解释你的情况如何运作。您的 VirtualMethod 是虚拟的并被覆盖。没有用东西和别的东西调用这样的东西,因为它没有用任何参数调用。没有任何可以称之为。您只有两个不同的返回对象。但是有一个名为this的参数。比如说,你创建了两个实例,一个是另一个类型的另一个类型(重要的运行时类型,该方法的实现是根据引用this动态选择的。这是OOP的核心根据定义,所有调用都基于引用this,在虚拟方法的情况下,基于其运行时类型。请参阅: https://en.wikipedia.org/wiki/Dynamic_dispatch [ ^ ]。



你可能会问:怎么做?没什么。只要了解事情是如何运作的。你的另一个问题是你问的问题。你不是在问什么重要的东西,这些东西应该在行为中真正发生。然后我们可以立即帮助你。但是你要问的是一些只存在于你想象中的不存在的概念,比如静态地,这基本上不是一些东西在可观察的世界中发生的事情。也许你需要学习如何制定和理解你的最终目标。



-SA
It all makes no sense at all. Note that the method doSomethingWithVirtualMethodResult, despite its name, has nothing to do with VirtualMethod or its result. It simply can be called with any string.

"Calling statically" sounds like absurd, but in fact, it's simply unclear what would you mean by that. No static members are involved, so what you want has nothing to do with the only well-defined concept related to static in Java technology: static members vs instance members. All your members are instance members, that is, the explicit "this" parameter is passed to instance methods, to reference the particular instance of the class. Essentially, there is no such thing as a call without such instance.

So, you need a solution, right? The solution is: learn how OOP works and pose your problems correctly. Your problem is not solvable or unsolvable, it simply makes no sense as it is put. For now, I'll explain how things work in your case. Your VirtualMethod is virtual and overridden. There is no such thing as "called with both something and something else", because it is not called with any parameter. There is no anything "to call it with". You only have two different return objects. But there is a parameter called explicitly, "this". Say, you create two instances, one of one type and another of another type (important runtime type, the implementation of the method is dynamically chosen based on the reference "this". This is a heart of OOP, its central mechanism. All calls are, by definition, are based on the reference "this", in the case of a virtual method, on its runtime type. Please see: https://en.wikipedia.org/wiki/Dynamic_dispatch[^].

You may ask: what to do? Nothing. Just learn how things work. Your other problem is the question you ask. You are not asking about anything material, something which should really happen in behavior. Then we could help you immediately. But you are asking in terms of some non-existing concept which only exist in your imagination, such as "calling statically", which is, essentially, is not something which happens in observable world. Perhaps you need to learn how to formulate and understand your ultimate goals.

—SA






我想我们误会了。当我谈到静态方法时,我不了解静态的java含义,那么我并不是说实例/静态成员,而是像虚拟或动态调用的相反含义。 />


阅读我的真实代码(与我公开的示例相比)我找到了创建私有(然后是最终)方法的解决方案:

Hi,

I think we misunderstood. When I speak about static methods, I didn't understand about the java meaning of "static", then I didn't mean talk about the instance/static members, but just like the opposite meaning of virtual, or dynamic call.

Reading my real code (vs the example I exposed) I found the solution creating private (then final) methods:
class A extends BaseClass {
    public String init() {
        doSomethingWithVirtualMethodResult(staticMethod());
        super.init();
    }
    private String staticMethod() {
        return something;
    }
    protected String virtualMethod() {
        return staticMethod();
    }
}
class B extends A {
    public String init() {
        doSomethingWithVirtualMethodResult(staticMethod());
        super.init();
    }
    private String staticMethod() {
        return somethingElse;
    }
    protected String virtualMethod() {
        return staticMethod();
    }
}


嘿,男孩,尊重所有人!我认为这是这个论坛的主要内容。如果你是政体,你的回复可以是这样的:



不,你不能。



结束
Hey boy, respect all! I touhgt that it is the main of this forum. If you was polity, your reply could be simlpy this:

No, you can't.

End


这篇关于java静态调用虚方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆