C#错误:“非静态字段,方法或属性需要对象引用”

我有两个类,一个用于定义算法参数,另一个用于实现算法:


第1类(算法参数):


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;

        }


我对得到一个错误Population[i].bits = GetRandomBits();在Main()。


错误是:


非静态字段,方法或属性'VM_Placement.Program.GetRandomBits()'需要对象引用。


我有什么想念的吗?


慕桂英546537
浏览 730回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP