C# 无法访问方法
下面的代码有错误。我无法调用ShowUp()函数。类Dx位于错误的位置,还是方法声明错误C# 无法访问方法,c#,methods,C#,Methods,下面的代码有错误。我无法调用ShowUp()函数。类Dx位于错误的位置,还是方法声明错误 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Program1 { class Program { public class Dx {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Program1
{
class Program
{
public class Dx
{
void ShowUp()
{
Console.Write("Show");
Console.Read();
return;
}
}
static void Main(string[] args)
{
Dx.ShowUp();
}
}
}
该函数不是静态的 因此,要么更改函数定义,要么创建实例。此外,您的职能需要是内部的或公共的,如@uguraldanmaz所述:
ShowUp()是一个实例方法,或者换句话说,是一个非静态方法。您需要先创建一个实例,然后才能调用它。这里没有继承。或者你需要把ShowUp()变成一个静态方法……比如
publicstaticvoidshowup()
正确,但是你应该更加强调其中的一个方面。也许对答案进行编号。在我看来,答案很清楚,如果OP想知道实例方法与静态方法之间的区别,我认为OP应该用谷歌搜索它,并进行一些阅读@Ako无法向OP充分解释这一区别