简体   繁体   English

从另一个类调用方法

[英]Call a method from another class

I'm wanting to know how I can call a method from another class without having to make a new instance of that class. 我想知道如何从另一个类调用方法而不必创建该类的新实例。 I've looked up this and 90% of the examples I see require me to make a new copy of my referenced class. 我已经看过了这个,我看到的90%的例子都要求我制作我引用的类的新副本。

Something like this: 像这样的东西:

Fooclass test = new Fooclass();
test.CallMethod();

However, I'm wondering if there is a way I can call the method without making a new class instance. 但是,我想知道是否有一种方法可以在不创建新类实例的情况下调用该方法。 Right now I've tried the following in unity. 现在我已经在团结中尝试了以下内容。

public ImageLoader image; 
void Start () 
{
    image = gameObject.GetComponent<ImageLoader>() as ImageLoader;
}

void OnClick()
{
    image.MoveForward();
}

however, when I run this I get the following error: 但是,当我运行这个时,我收到以下错误:

NullReferenceException: Object reference not set to an instance of an object NullReferenceException:未将对象引用设置为对象的实例

i know this would be settled with making a new instance of my image loader class but I can't do that as it is holding a lot of data I don't want duplicated multiple times. 我知道这将通过制作我的图像加载器类的新实例来解决,但我不能这样做,因为它持有大量数据我不想多次重复。

Yes you can. 是的你可以。 The first way is to make your class to be static. 第一种方法是让你的班级变得静止。

public static class Fooclass
{
    // I don't know the return type of your CallMethod, so I used the void one.
    public static void CallMethod()
    {

    }
}

This way, whenever to your code you can call the CallMethod() like the following: 这样,无论何时对您的代码,您都可以调用CallMethod() ,如下所示:

Fooclass.CallMethod()

Another apporach it would be to define a static method in your current class, without the class needed to be static, like the following: 另一个应用是在当前类中定义静态方法,而不需要类是静态的,如下所示:

public class Fooclass
{
    // I don't know the return type of your CallMethod, so I used the void one.
    public static void CallMethod()
    {

    }
}

Now since all the instances of the Fooclass would share the same method called CallMethod , you can call it like below: 现在,由于Fooclass所有实例Fooclass将共享名为CallMethod的相同方法,因此您可以像下面这样调用它:

Fooclass.CallMethod()

without again needed to instantiate an object of type Fooclass, despite the fact that now Fooclass isn't a static class now ! 尽管现在Fooclass现在不是一个静态类,但不需要再次实例化Fooclass类型的对象!

For further documentation please take a look to the link Static classes and Static Members . 有关进一步的文档,请查看静态类和静态成员链接。

Make a static class / static method. 创建一个静态类/静态方法。 Making the method static is good enough if you do not want your class to be static. 如果您不希望您的类是静态的,那么使该方法静态就足够了。

class my_Class
{
    public static void Print()
    {
        Console.WriteLine("Hello World");
    }
}

my_Class.Print();

如果您的其他类的方法没有使用任何实例级别变量,那么您可以将其设置为静态并使用它

Fooclass.CallMethod();

The marked answers fit to your question, however please also note that your original Problem seems to be different. 标记的答案适合您的问题,但请注意您的原始问题似乎有所不同。

I wonder if the original Problem that results in your NullReferenceException: Object reference not set to an instance of an object error is that your method named OnClick() is either called before your Start() method or that the call image = gameObject.GetComponent<ImageLoader>() as ImageLoader; 我想知道导致你的NullReferenceException: Object reference not set to an instance of an object错误NullReferenceException: Object reference not set to an instance of an object的原始问题是你的方法名为OnClick()是在你的Start()方法之前调用还是调用image = gameObject.GetComponent<ImageLoader>() as ImageLoader; never returns a valid instance of your desired class, therefore image is always null. 永远不会返回所需类的有效实例,因此image始终为null。

You need to declare a static member in your static/non-static class . 您需要在静态/非静态class声明static成员。 The static member is callable on a class even when no instance of the class has been created. 即使没有创建类的实例, static成员也可以在class上调用。

For eg: 例如:

public class Automobile
{
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
    {
        get
        {
            return 15;
        }
    }
    public static void Drive() { }
    public static event EventType RunOutOfGas;

    // Other non-static fields and properties...
}

Static members are initialized before the static member is accessed for the first time and before the static constructor, if there is one, is called. Static成员在第一次访问static成员之前和static构造函数(如果有的话)被调用之前被初始化。 To access a static class member, use the name of the class instead of a variable name to specify the location of the member, as shown in the following example: 要访问静态类成员,请使用类的名称而不是变量名来指定成员的位置,如以下示例所示:

Automobile.Drive();
int i = Automobile.NumberOfWheels;

The static member is always accessed by the class name, not the instance name. static成员始终由class名访问,而不是实例名。 Only one copy of a static member exists, regardless of how many instances of the class are created. 无论创建了多少个类实例,都只存在一个static成员的副本。 Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter. Static方法和属性无法访问其包含类型中的非静态字段和事件,并且除非在方法参数中显式传递,否则它们无法访问任何对象的实例变量。

Source: http://msdn.microsoft.com/en-us/library/79b3xss3.aspx 来源: http//msdn.microsoft.com/en-us/library/79b3xss3.aspx

Hope this helps you. 希望这对你有所帮助。

Shishir Shishir

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM