实例化 subclass 时从 super class 调用方法

Calling methods from a super class when a subclass is instantiated

您将如何创建 Class,无论哪个 class 扩展 Class,方法都会自动 invoked/called。如果它听起来具有误导性,请编辑我的问题。我只是展示一些示例

示例 1:

在统一中,当您扩展 monobehavior 时,您的方法会被自动调用。我不知道我是否正确。

public class MyController : MonoBehaviour { 

   void Start()
    {     
      //Being Called Once
    }

    void FixedUpdate()
    {   
    //Being Called every update
}

于 libgdx

Game implements ApplicationListener {


    @Override
    public void render () {
        //Called multiple times
    }

}

根据我自己的理解和尝试实现它

public abstract Test{

        protected Test(){
            onStart();
        }


        public abstract void onStart();
}


public class Test2 extends Test{

    public Test2(){

    }

    @Override
    public void onStart(){
        //Handle things here
    }

}

对不起,但我真的不知道它是如何工作的,也不知道你怎么称呼这种技术。

特别是在 unity 中,当创建多个扩展 Monobehavior 的控制器时,所有已实现的控制器方法都会被调用。谁在调用此 classes 和方法?关于这方面的一些参考资料或书籍会很有帮助。

注意:请编辑我的标题以获得正确的术语以用于此。谢谢

I'm sorry, but I still really don't know how it works or what do you call this technique

在您的 Java 示例中,onStart 方法被称为挂钩或回调方法。

维基百科对hooking的定义如下:

In computer programming, the term hooking covers a range of techniques used to alter or augment the behavior of an operating system, of applications, or of other software components by intercepting function calls or messages or events passed between software components. Code that handles such intercepted function calls, events or messages is called a "hook"

维基百科对callback的定义如下:

In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback, or it might happen at later time as in an asynchronous callback

任何从 Test class 实例化 Test 方法的 class 将导致调用 instanceonStart 方法.示例:

Test test = new Test2();//calls onStart in Test2.

也就是说,我不确定在 MonoiBehavior 的情况下谁调用了这些方法,但是您对如何在 [=11] 中实现 hookcallback 的大致了解=] 是正确的。