从调用静态方法非静态方法 [英] Invoking non static method from static method

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

问题描述

我还可以从静态方法(访问),非静态的方法? 像我有静态方法如果是,如何?

 公共静态无效的方法()//这样是一个静态方法
{
    methodsec(); //从这里我要访问以下定义的非静态方法
}

公共无效methodsec()//非静态方法
{

}
 

解决方案

是的,但你需要一个参照,通过做到这一点:

 公共静态无效的静态方法()
{
     someInstance.InstanceMethod();
}

公共无效InstanceMethod()
{
}
 

您需要考虑你要调用的方法,实例。实例方法通常使用实例的状态,所以该方法有可能做到这取决于实例,它被称为上不同的事情。

Can I call(access) non static method from static method ?? Like I have static method If yes, how??

public static void method()      //like this is a static method
{
    methodsec();        //from here I want to access non static method defined below
}

public void methodsec()      // non static method
{

}

解决方案

Yes, but you need a reference to do it through:

public static void StaticMethod()    
{
     someInstance.InstanceMethod();
}

public void InstanceMethod()
{
}

You need to think about which instance you want to call the method on. Instance methods typically use the state of the instance, so the method is likely to do different things depending on which instance it's called on.

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

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