【发布时间】: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