从调用静态方法非静态方法静态、方法

由网友(独奏那曲离伤)分享简介:我还可以从静态方法(访问),非静态的方法?像我有静态方法如果是,如何?公共静态无效的方法()//这样是一个静态方法{methodsec(); //从这里我要访问以下定义的非静态方法}公共无效methodsec()//非静态方法{}解决方案 是的,但你需要一个参照,通过做到这一点:公共静态无效的静态方法(){s...

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

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

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

}
 

解决方案

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

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

公共无效InstanceMethod()
{
}
 

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

对象调用静态方法 Java静态方法和实例方法

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.

阅读全文

相关推荐

最新文章