【问题标题】:C# How to make an object from class Person kill another?C#如何让Person类中的一个对象杀死另一个?
【发布时间】:2019-04-14 03:47:02
【问题描述】:

在下面的代码中,我试图学习如何让对象相互交互,因为我觉得这比我现在所做的更重要一点,即收集一堆分配给每个对象的变量.

为了好玩,我希望这些不同的物体做的就是互相残杀。名叫杰克的人可以杀人。其他两个不行。我想让 Jack 做的是打击另外两个,让他们多次失去 1、5 或 10 个生命值,直到他们死亡,然后将他们的 Alive 设置为 false。

我什至不知道如何开始,但我认为这将是一个非常有趣和有趣的练习。

这样做最重要的是了解一个对象如何直接更改另一个对象的某些内容,因为它可以,并且它随后更改的对象将受到此操作的影响。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OOP_Learning
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1; 
            p1 = new Person("Jack", 27, true, true, 10);

            Person p2;
            p2 = new Person("Vincent", 63, true, false, 10);

            Person p3;
            p3 = new Person("Tim", 13, true, false, 10);

            Console.ReadLine();
        }

    }
    public class Person
    {
        public string Name { get; set; }    
        public int Age { get; set; }
        public bool Alive { get; set; }
        public bool AbilityToKill { get; set; }
        public int HitPoints { get; set; }
        public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
        {
            HitPoints = hitPoints;
            AbilityToKill = abilityToKill;
            Alive = alive;
            Name = name;
            Age = age;
        }
    }
}

【问题讨论】:

  • 您问题的某些部分有简单的答案。例如, p2.HitPoints -= 10 将带走生命值。而 p3.Alive = false 会杀死某人。但是你可以让它更复杂,比如 Alive 可以是一个计算字段:get { return this.HitPoints > 0; }。像那样的东西。或者你可以有一个 TakeHit 方法,它将命中计数减少给定的数字,甚至计算一个随机数。梦想大
  • 另外,我建议您更改此问题的标题,因为“kill”可能会被误解为终止进程,这也是可能的。您只想对对象进行更改
  • @baileyrt 这些都是很好的观点。我确实喜欢梦想远大,随着时间的推移变得紧密而复杂,但我也想从小处着手,以获得更好的基本理解。我相信我要做的第一件事就是了解 this 关键字。我也稍微编辑了这个问题,所以我认为现在更容易理解了

标签: c# oop object


【解决方案1】:

Person 类中需要 2 个方法。

  1. Hit -> 此方法在每次点击时减少 self 对象上的 HitPoints。当HitPoints 变为零时,状态Alive 设置为false。

  2. Kill -> 这会将人作为参数并在该人上调用Hit 方法,直到该人为Alive。

将以下方法添加到Person 类:

    public void Hit()
    {
        if(Alive)
        {
            if(HitPoints > 0)
            {
                HitPoints -= 1;
            }
            else
            {
                Alive = false;
            }
        }
    }

    public bool Kill(Person person)
    {
        if(!AbilityToKill)
        {
            Console.WriteLine("You don't have ability to kill! You cannont kill {0}.", person.Name);
            return false;
        }

        while(person.Alive)
        {
            person.Hit();
        }

        Console.WriteLine("{0} is dead.", person.Name);

        return true;
    }

在Main方法中调用Kill方法。

    p2.Kill(p3);
    p1.Kill(p2);
    p1.Kill(p3);

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    你的意思是这样的吗?

    public class Person
    {
        public string Name { get; private set; }    
        public int Age { get; private set; }
        public bool Alive { get; private set; }
        public bool AbilityToKill { get; private set; }
        public int HitPoints { get; private set; }
    
        public void Hit(int points)
        {
            this.HitPoints -= points;
            if (this.HitPoints <= 0)
            {
                this.HitPoints = 0;
                this.Alive = false;
            }
        }
    
        public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
        {
            this.HitPoints = hitPoints;
            this.AbilityToKill = abilityToKill;
            this.Alive = alive;
            this.Name = name;
            this.Age = age;
        }
    }
    

    现在检查AbilityToKill:

    public class Person
    {
        public string Name { get; private set; }
        public int Age { get; private set; }
        public bool Alive { get; private set; }
        public bool AbilityToKill { get; private set; }
        public int HitPoints { get; private set; }
    
        public int Attack(Person victim, int points)
        {
            var hp = victim.HitPoints;
            if (this.AbilityToKill)
            {
                victim.HitPoints -= points;
                if (victim.HitPoints <= 0)
                {
                    victim.HitPoints = 0;
                    victim.Alive = false;
                }
            }
            hp -= victim.HitPoints;
            return hp;
        }
    
        public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
        {
            this.HitPoints = hitPoints;
            this.AbilityToKill = abilityToKill;
            this.Alive = alive;
            this.Name = name;
            this.Age = age;
        }
    }
    

    可以这样使用:

    var jack = new Person("Jack", 27, true, true, 10);
    var vincent = new Person("Vincent", 63, true, false, 10);
    var tim = new Person("Tim", 13, true, false, 10);
    
    var damage_done = jack.Attack(vincent, 20);
    Console.WriteLine(damage_done);
    

    Attack 方法返回被攻击减少的实际命中点数 - 造成的伤害。


    这是一个更强类型的版本。对属性使用bool 并不总是最清晰的编码方式。通过一些枚举,它就更清楚了。

    public class Person
    {
        public string Name { get; private set; }
        public int Age { get; private set; }
        public Alive Alive { get; private set; }
        public AbilityToKill AbilityToKill { get; private set; }
        public int HitPoints { get; private set; }
    
        public int Attack(Person victim, int points)
        {
            var hp = victim.HitPoints;
            if (this.AbilityToKill == AbilityToKill.Yes)
            {
                victim.HitPoints -= points;
                if (victim.HitPoints <= 0)
                {
                    victim.HitPoints = 0;
                    victim.Alive = Alive.No;
                }
            }
            hp -= victim.HitPoints;
            return hp;
        }
    
        public Person(string name, int age, Alive alive, AbilityToKill abilityToKill, int hitPoints)
        {
            this.HitPoints = hitPoints;
            this.AbilityToKill = abilityToKill;
            this.Alive = alive;
            this.Name = name;
            this.Age = age;
        }
    }
    
    public enum Alive { Yes, No }
    public enum AbilityToKill { Yes, No }
    

    它是这样使用的:

    var jack = new Person("Jack", 27, Alive.Yes, AbilityToKill.Yes, 10);
    var vincent = new Person("Vincent", 63, Alive.Yes, AbilityToKill.No, 10);
    var tim = new Person("Tim", 13, Alive.Yes, AbilityToKill.No, 10);
    
    var damage_done = jack.Attack(vincent, 20);
    Console.WriteLine(damage_done);
    

    【讨论】:

    • @AntonínLejsek - 我特意采用了一种简单的方法,因为 OP 似乎没有很多经验。
    • @AntonínLejsek - 请不要删除您的 cmets。这使得理解对话变得更加困难。
    • 由于我的评论与状态不再存在有关,我不认为清理是一件坏事。
    • @AntonínLejsek - 但它确实与我的评论直接相关。所以现在我的评论没有上下文。我确实发现你所说的“清理”会造成很大的混乱。
    • 无论如何,在这次对话中对读者没有任何附加价值,我认为在这种情况下将页面视为只写是没有意义的。现在情况发生了变化,现在我们正在谈论删除对读者没有任何帮助的帖子。这完全是题外话,但是好的,它有一些意义,在这种情况下,我同意删除 cmets 会导致问题。
    猜你喜欢
    • 2013-05-22
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 2011-06-28
    相关资源
    最近更新 更多