【问题标题】:Make object as an array return error "Method must have return type"将对象作为数组返回错误“方法必须具有返回类型”
【发布时间】:2014-10-27 20:54:34
【问题描述】:

我遵循 Head First 这本书,但我不知道为什么我的对象数组不能被声明。系统一直说“方法必须有返回类型”。我知道我可以标记每个对象的不同名称,例如 dog1、dog2、dog3,并使用 Guy 类创建对象,但我只是想知道我做错了什么,它不能是数组 dog[0]、dog[ 1] 等等。你能帮帮我吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Guy Joe = new Guy() {Money = 50}; 
        Guy Bob = new Guy() {Money = 75};
        Guy Al = new Guy() {Money = 45};


        Greyhound[] dog = new Greyhound[4];
        dog[0] = new Greyhound();
    }
    public class Guy
    {
        public int Money;

    }
    class Greyhound
    {
        public int StartingPosition;
        public int RaceTrackLengh;
        public PictureBox MyPictureBox = null;
        public Random Randomizer;
        public int Location;

        public bool Run()
        {
            Location += Randomizer.Next(5);
            MyPictureBox.Left = StartingPosition + Location;
            if (Location >= RaceTrackLengh)
            {
                TakeStartingPosition();
                return true;
            }
            else
            {
                return false;
            }

        }

        private void TakeStartingPosition()
        {
            Location = 0;
            MyPictureBox.Left = StartingPosition;
        }
    }
}

【问题讨论】:

  • 尝试将第 19 - 25 行的代码放入方法中,例如:void InitialiseMyObjects(){Guy Joe = new Guy() {Money = 50}; Guy Bob = new Guy() {Money = 75}; Guy Al = new Guy() {Money = 45}; Greyhound[] dog = new Greyhound[4]; dog[0] = new Greyhound();}
  • 应该是赌狗的赌博游戏。这是每个玩家拥有的游戏数量,但目前我正在努力完成这个项目的 20% 以完全了解问题所在。

标签: c# arrays object types return


【解决方案1】:

在构造函数中声明它们:

public Form1()
        {
            InitializeComponent();
            Guy Joe = new Guy() {Money = 50}; 
             Guy Bob = new Guy() {Money = 75};
             Guy Al = new Guy() {Money = 45};


             Greyhound[] dog = new Greyhound[4];
             dog[0] = new Greyhound();
        }

【讨论】:

    【解决方案2】:

    问题在于dog[0] = new Greyhound(); 行是一个语句,而Greyhound[] dog = new Greyhound[4]; 例如是一个带有初始化的字段声明。

    语句必须在方法中,但是对于您想要做的事情,还有另一种使用初始化器列表的方法:

    Greyhound[] dog = new Greyhound[4]
    {
        new Greyhound(),
        new Greyhound(),
        new Greyhound(),
        new Greyhound()
    }
    

    【讨论】:

      【解决方案3】:

      你不能只写那样的代码

      Greyhound[] dog = new Greyhound[4];
      dog[0] = new Greyhound();
      

      在一个类中,它必须进入构造函数或其他方法中。

      【讨论】:

        【解决方案4】:

        试试这个:

        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Forms;
        
        namespace WindowsFormsApplication7
        {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
               Guy Joe = new Guy() {Money = 50}; 
              Guy Bob = new Guy() {Money = 75};
              Guy Al = new Guy() {Money = 45};
        
        
            Greyhound[] dog = new Greyhound[4];
            dog[0] = new Greyhound();
        
            }
          }
        public class Guy
        {
            public int Money;
        
        }
        class Greyhound
        {
            public int StartingPosition;
            public int RaceTrackLengh;
            public PictureBox MyPictureBox = null;
            public Random Randomizer;
            public int Location;
        
            public bool Run()
            {
                Location += Randomizer.Next(5);
                MyPictureBox.Left = StartingPosition + Location;
                if (Location >= RaceTrackLengh)
                {
                    TakeStartingPosition();
                    return true;
                }
                else
                {
                    return false;
                }
        
            }
        
            private void TakeStartingPosition()
            {
                Location = 0;
                MyPictureBox.Left = StartingPosition;
            }
        }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-16
          • 2012-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多