简体   繁体   English

我必须为新的类实例指定名称吗?

[英]c# Do I have to specify name for a new class instance?

I'm making a simple game in unity and have a small uncretainity you could help me with: do I have to name the new instance of a class? 我正在统一地制作一个简单的游戏,但有一点点不确定性可以帮助我:我是否必须命名一个类的新实例? for example: 例如:

public class CreateCharacter : MonoBehaviour {

public string charname;

public CreateCharacter (string charname)
{
    this.charname = charname;
    //does all the stuff needed
}}

I want to run this from another script: 我想从另一个脚本运行它:

int e = data.Length - 1;
            if (e > 0)
            {
                new CreateCharacter(data[e]);
                e--;
            }

data is a string array and CreateCharacter will store the new Character object in a list of such objects, so I can accss it later. data是一个字符串数组,CreateCharacter会将新的Character对象存储在此类对象的列表中,因此以后可以使用它。 My question is: Do I have to write: 我的问题是:我必须写:

CreateCharacter SomeName = new ChreateCharacter

for the code to run? 为代码运行? or is simple 还是很简单

new Class (constructor)

enough? 足够?

While it is poor practice to new something and not toss it in a variable, that code WILL compile and run just fine. 尽管添加一些新东西并且不将其扔入变量中是一种不好的做法,但是该代码将可以编译并正常运行。 Additionally, is there a reason you have a class called CreateCharacter? 另外,您是否有一个叫CreateCharacter的类,原因是什么? Instead of a function that returns an actual character given the data? 而不是返回给定数据的实际字符的函数?

It won't cause the program to die using without storing it. 如果不存储该程序,将不会导致该程序死亡。

new CreateCharacter(data[e]);

but yes, you want to store it so you can call methods on it like 但是是的,您想要存储它,以便可以像上面那样调用方法

CreateCharacter char = new CreateCharacter(string);

char.methodName();

Your other option is to put the code you are trying to execute in a static method because it seems that the code you are running in the constructor is not dependent on the instance of the class CreateCharacter . 另一个选择是将要尝试执行的代码放在static method因为似乎在构造函数中运行的代码不依赖于CreateCharacter类的实例。

You are allowed to call the constructor by itself like you stated. 您可以像声明的那样自行调用构造函数。 However, do keep in mind that the newly created object will immediately fall out of scope and be deallocated after the constructor is done. 但是,请记住,新创建的对象将立即超出范围,并在构造函数完成后被释放。 So depending on what's included in "// does all the stuff needed", it could -potentially- crash your app (although it would be unlikely). 因此,根据“ //是否包含所有需要的东西”中的内容,它可能会-使您的应用程序崩溃(尽管不太可能)。

As stated in other answers, it is a best practice in both readability and functionality to assign the newly created object to a variable, even if only for the scope of that for loop. 如其他答案所述,将可读性和功能性的最佳做法是将新创建的对象分配给变量,即使仅针对该for循环的范围也是如此。

If you use 如果您使用

new Class (constructor) 新类(构造函数)

it is anonymous object and it does the job. 它是匿名对象,可以完成工作。 This king of object is used whenever the object is not referenced from other places of your code and its benefit is that you are not supposed to find an appropriate name for that. 只要未从代码的其他位置引用该对象,就使用该对象的王者,其好处是您不应该为此找到合适的名称。

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

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