C# 有没有办法从静态方法调用非静态方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15905248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to call a non-static method from a static method?
提问by Glimpse
Here's what I have.
这就是我所拥有的。
public static void Person_home_phone_TextChanged(object sender, EventArgs e) { ... }
Is there any way to access non-static methods from the same or another class from inside this static method?
有没有办法从这个静态方法内部访问来自同一个或另一个类的非静态方法?
I need grab the text in the Person_home_phone text box and save it to a class data member.
我需要获取 Person_home_phone 文本框中的文本并将其保存到类数据成员中。
采纳答案by Gabe
Example() -> Example
示例() -> 示例
You would just need to create an instance of the type
then call the non-static
, from a static
method.
您只需要创建一个实例,type
然后non-static
从static
方法调用, 。
public class Example(){
public static void StaticExample()
{
Example example = new Example();
example.NonStatic();
}
public void NonStatic()
{
}
}
回答by Mattias Jakobsson
You need to have a instance of the class to call a non-static method.
您需要有一个类的实例来调用非静态方法。
回答by masoud
You should have an object to access its method.
你应该有一个对象来访问它的方法。
statics doesn't belong to objects. They belongs to class declarations.
静态不属于对象。它们属于类声明。
回答by MarcinJuraszek
Instance methods (vel. non-static) can only be called in context of an instance of that class. So you can call it, but you have to have an object of that class available somewhere in your static method.
实例方法(vel。非静态)只能在该类的实例的上下文中调用。所以你可以调用它,但是你必须在你的静态方法的某个地方有一个该类的对象。
回答by duDE
A non-static method requires an instance of the class. Unless you have passed in an instance, or created an instance in your method, you cannot call a non-static method, as you have no idea what instance of the class that method should operate on.
非静态方法需要类的实例。除非您传入了一个实例,或者在您的方法中创建了一个实例,否则您不能调用非静态方法,因为您不知道该方法应该操作的类的哪个实例。
You need an instance of the class class to call the non-static method.
您需要一个类的实例来调用非静态方法。
回答by Kiran.Bakwad
Solution #1: Instantiate a new instance of Car every time the method is called.
解决方案#1:每次调用该方法时实例化一个新的 Car 实例。
public static void DoSomething()
{
Car c = new Car();
c.NonStaticMethod();
}
Solution #2: Pass a Car to the method.
解决方案#2:将 Car 传递给方法。
public static void DoSomething(Car c)
{
c.NonStaticMethod();
}
Solution #3:
解决方案#3:
Use a singleton Car to support the static method. (If calls from multiple threads are a possibility, you might also need locking. Note that System.Windows.Forms.Timer does not introduce a thread.)
使用单例 Car 来支持静态方法。(如果可以从多个线程调用,您可能还需要锁定。请注意 System.Windows.Forms.Timer 不会引入线程。)
public class Car
{
private static Car m_Singleton = new Car();
public static void DoSomething()
{
m_Singleton.NonStaticMethod();
}
Note that you have not explained your memory problems with Timer. It is very possible that there is a solution to that underlying problem.
请注意,您没有用 Timer 解释您的内存问题。很可能存在解决该潜在问题的方法。