【发布时间】:2014-06-08 16:52:09
【问题描述】:
我目前正在浏览各种资源并尝试学习 C# OOP。我还没有经历过任何事情,但是我尝试了对象与对象的交互。不幸的是,它没有按计划进行,我对我应该引用的对象有点困惑。我想创建一个简单的攻击方法,降低另一个对象的健康状况,只是为了降低对象与对象交互的基础。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Dog milo = new Dog("Sparky");
Dog ruffles = new Dog("Ruffles");
milo.Attack(ruffles);
Console.ReadLine();
}
}
class Dog
{
public string name { get; set; }
public int health = 100;
public Dog(string theName)
{
name = theName;
public void Attack(Dog theDog)
{
Console.WriteLine("{0} attacks {1}.", this.name, theDog);
LoseHealth(theDog);
}
public void LoseHealth()
{
Console.WriteLine("{0} loses health!", theDog);
theDog -= 5;
}
}
}
}
代码根本不起作用。知道我做错了什么吗?谢谢你的帮助。
【问题讨论】:
-
如何它不起作用?会爆炸吗?
-
LoseHealth需要一个Dog参数,并且Attack都需要从构造函数中移出。