具体来说,我正在尝试这段代码:
package hello;
public class Hello {
Clock clock = new Clock();
public static void main(String args[]) {
clock.sayTime();
}
}
Run Code Online (Sandbox Code Playgroud)
但它给出了错误
无法访问静态方法main中的非静态字段
所以我把声明改为clock:
static Clock clock = new Clock();
Run Code Online (Sandbox Code Playgroud)
它奏效了.在声明之前放置该关键字是什么意思?对于该对象可以做什么,它究竟会做什么和/或限制什么?
假设我在表单中有一个下拉列表,并且我在此类中有另一个嵌套类.现在,从嵌套类访问此下拉列表的最佳方法是什么?
使用示例1:在此MSDN教程中创建,启动和交互线程,更具体地说是第3行到第7行Main()
我有以下代码,出现以下错误:
无法使用实例引用访问; 用类型名称来限定它.
Program.cs中
public static ThreadTest threadTest = new ThreadTest();
private static Thread testingThread = new Thread(new ThreadStart(threadTest.testThread()));
static void Main(string[] args)
{
}
Run Code Online (Sandbox Code Playgroud)
ThreadTest.cs
public static void testThread()
{
}
Run Code Online (Sandbox Code Playgroud) 我创建了扩展方法:
public static class XDecimal
{
public static decimal Floor(
this decimal value,
int precision)
{
decimal step = (decimal)Math.Pow(10, precision);
return decimal.Floor(step * value) / step;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用它:
(10.1234m).Floor(2)
Run Code Online (Sandbox Code Playgroud)
但编译说Member 'decimal.Floor(decimal)' cannot be accessed with an instance reference; qualify it with a type name instead.我明白有静态decimal.Floor(decimal)方法.但它有不同的签名.为什么编译器无法选择正确的方法?
今天,我正在研究c Sharp,并且正在尝试静态类,但是它似乎不适用于我,我很想知道解决方案。我已经在网上浏览了一段时间,但似乎找不到答案。
这是我的代码:
class Count
{
public static int sum(int add1, int add2)
{
int result = add1 + add2;
return result;
}
}
class Program
{
static void Main(String[] args)
{
Console.WriteLine("Adding: \nPlease enter the first number");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter the second number");
int num2 = int.Parse(Console.ReadLine());
Count add = new Count();
int total = add.sum(num1, num2);
Console.WriteLine("The sum is {0}.", total);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud) 我有一对我用C#编写的脚本,其中一个脚本从另一个脚本调用一个方法.但是,我收到以下错误:
Member 'PlayerActions.Attack()' cannot be accessed with an instance reference; qualify it with a type name instead
这是我想要调用的方法所在的位置:
public class PlayerActions:MonoBehaviour{
public static void Attack(){
Debug.Log("Attacking");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试图调用该方法的地方:
public class Combat:MonoBehaviour{
PlayerActions playerActions;
void Start(){
playerActions = GetComponent<PlayerActions>();
playerActions.Attack();
}
}
Run Code Online (Sandbox Code Playgroud)
两个脚本都附加到同一个游戏对象.
任何人都可以告诉我如何修复上面提到的错误以及为什么我实际上得到错误?我一直认为你需要引用你试图访问的类,但从我的理解,这个错误是另外说的.