簡體   English   中英

C# 錯誤:“非靜態字段、方法或屬性需要對象引用”

[英]C# error: "An object reference is required for the non-static field, method, or property"

我有兩個類,一個用於定義算法參數,另一個用於實現算法:

第一類(算法參數):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VM_Placement
{
    public static class AlgorithmParameters
    {
        public static int pop_size = 100;
        public static double crossover_rate = 0.7;
        public static double mutation_rate = 0.001;

        public static int chromo_length = 300;
        public static int gene_length = 4;
        public static int max_allowable_generations = 400;

        static Random rand = new Random();
        public static double random_num = rand.NextDouble();
    }
}

第 2 類(實現算法):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VM_Placement
{
    public class Program
    {
        public struct chromo_typ
        {
            public string   bits;
            public float    fitness;

            //public chromo_typ(){
            // bits = "";
            // fitness = 0.0f;
            //}
            chromo_typ(string bts, float ftns)
            {
                bits = bts;
                fitness = ftns;
            }
        };

        public static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng =
              new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

        public string GetRandomBits()
        {
            string bits="";

            for (int i = 0; i < VM_Placement.AlgorithmParameters.chromo_length; i++)
            {
                if (VM_Placement.AlgorithmParameters.random_num > 0.5f)
                    bits += "1";
                else
                    bits += "0";
            }
            return bits;
        }

        public static void Main(string[] args)
        {
            Random rnd = new Random(GetRandomSeed());

            while (true)
            {
                chromo_typ[] Population = new chromo_typ[VM_Placement.AlgorithmParameters.pop_size];
                double Target;

                Console.WriteLine("\n Input a target number");
                Target = Convert.ToDouble(Console.ReadLine());

                for (int i = 0; i < VM_Placement.AlgorithmParameters.pop_size; i++)
                {
                    Population[i].bits = GetRandomBits();
                    Population[i].fitness = 0.0f;
                }
            }
        }
    }
}

我在Population[i].bits = GetRandomBits();上遇到錯誤Main()中。

錯誤是:

非靜態字段、方法或屬性“VM_Placement.Program.GetRandomBits()”需要對象引用

我錯過了什么嗎?

Main方法是靜態的。 您不能從靜態方法調用非靜態方法。

GetRandomBits()

不是靜態方法。 要么你必須創建一個Program的實例

Program p = new Program();
p.GetRandomBits();

或制作

GetRandomBits()靜態的。

看起來你想要:

public static string GetRandomBits()

如果沒有static ,您需要一個對象才能調用GetRandomBits()方法。 但是,由於GetRandomBits()的實現不依賴於任何Program對象的狀態,因此最好將其聲明為static

Main方法在Program類中是靜態的。 您不能從靜態方法內部調用實例方法,這就是您收到錯誤的原因。

要修復它,您只需要將GetRandomBits()方法也設為靜態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM