【问题标题】:Basic C# object vs object interaction基本 C# 对象与对象交互
【发布时间】: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 都需要从构造函数中移出。

标签: c# oop object


【解决方案1】:

狗类的代码有点乱。

Attack 和 LoseHealth 方法在 构造函数中。

你只提到狗而不是健康和名字。

看看这个

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.name);
        LoseHealth(theDog);
    }

    public void LoseHealth(Dog theDog)
    {
        Console.WriteLine("{0} loses health!", theDog.name);
        theDog.health -= 5;
    }
}

额外的OO提示:

像这样更改攻击和 LoseHealth 方法会更有意义:

public void Attack(Dog theDog)
{
    Console.WriteLine("{0} attacks {1}.", this.name, theDog.name);
    theDog.LoseHealth(5);
}

public void LoseHealth(int damage)
{
    Console.WriteLine("{0} loses health!", name);
    this.health -= damage;
}

【讨论】:

    【解决方案2】:

    你做了theDog -= -5。但theDog 不是数字。您需要参考狗的健康状况。您还需要将theDog 传递给LoseHealth() 函数。改成这样:

    theDog.health -= 5;
    

    这使用点符号来访问theDog 的健康状况。

    而且,您的函数嵌套在构造函数中。移动 Attack()LoseHealth() 使它们在类中,而不是在构造函数主体中。你应该得到这样的结果:

    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(Dog theDog)
                {
                    Console.WriteLine("{0} loses health!", theDog);
                    theDog.health -= 5;
                }
        }
    

    顺便说一句,永远不要只说“我的代码不起作用”。解释它是如何不起作用的。包括相关的异常消息(如果有)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 2016-06-09
      相关资源
      最近更新 更多