【发布时间】:2018-07-29 23:25:58
【问题描述】:
我浏览了该站点,没有发现任何主题询问(或给出)静态在 C# 上的实用程序的具体示例。我是一个初学者,想找到证明静态有用性的给定案例,以便比对静态类/方法/等的概念理解更进一步。我只知道一两个示例,例如能够创建一个在运行时生成对象或值的方法,但通过删除创建的新值(当脚本再次运行)如果之前已经创建了一个值。代码是这样的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MusicPlayerScript : MonoBehaviour {
static MusicPlayerScript instance = null;
// Use this for initialization
void Start () {
if (instance != null)
{
Destroy(gameObject);
print("Duplicate self-destructing");
} else
{
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
// Update is called once per frame
void Update () {
}
}
还有什么其他的例子可以给我吗?谢谢!
【问题讨论】:
-
这个问题太笼统了。答案需要一本书的章节。
-
"The Static keyword can be applied on classes, variables, methods, properties, operators, events and constructors." 正如@MarkBenningfield 所说-这使您的问题非常广泛-您会通过使用谷歌或查看C#书籍找到很多信息。更多示例here
-
“我浏览了论坛...” Stack Overflow 不是一个讨论论坛,它是一个问答网站。有区别。请查看网站tour。
-
为什么要广泛?我不要求深入的解释,甚至不要求任何类型的概念洞察力。只有一两个例子,比如“你可以用静态来做这样那样的事情,以改进你的代码/程序上的这个和那个”。
标签: c# function class methods static