【问题标题】:Calling a method of another class with no relationship调用另一个没有关系的类的方法
【发布时间】:2014-04-04 23:09:11
【问题描述】:

我正在学习编码,但有些事情我仍然不知道。 由于我很难解释我想要什么,所以我在这里显示代码。

public class classOne {

    classTwo object = new ClassTwo();

    (...)

    public void WhenClassTwoHasEnded {
        (...)
    }
}

public class classTwo {

    public classTwo () {(...)}

    public classTwoAsyncStuff {
        (...)
        notifyEnd();
    }

    public void notifyEnd() {
        //How I can call WhenClassTwoHasEnded from here?
    }
}

通常我总是在 Stack Overflow 上找到我的问题的答案,但由于我的英语,我已经搜索了一段时间并没有找到任何东西。

【问题讨论】:

    标签: c# class methods


    【解决方案1】:

    可以通过许多不同的方式做到这一点。以下是一些:

    1) 让 ClassOne 运行它的 ClassTwo 的方法,而不是直接调用它。所以 ClassOne 有一个(如果你愿意,可以异步)方法,它调用await classTwo.classTwoAsyncStuff(),然后调用this.WhenClassTwoHasEnded()

    2) 你可以加event handlers

    3) 您也可以将ActionFunc 发送给该方法,并让它在完成后运行它。

    【讨论】:

    • 这就是我想要的!非常感谢您的回答。我希望继续学习,不必多问。非常感谢您的回复。
    • 不客气。但是,如果您遇到困难并且找不到答案,您不应该害怕寻求帮助。它会为您节省大量时间,并且您可能会学到一些新的有趣的东西:)
    猜你喜欢
    • 2022-01-24
    • 2013-12-21
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 2019-12-05
    相关资源
    最近更新 更多