C#如何使类中的一个对象Person杀死另一个



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

为了好玩,我想让这些不同的物体互相残杀。那个叫杰克的人会杀人。其他两个不能。我想让杰克攻击另外两个,让他们多次失去1、5或10点生命值,直到他们死亡,然后将他们的生存设置为假。

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

做这件事最重要的是了解一个对象如何直接改变另一个对象的某些东西,因为它可以,而且它改变的对象将因此而受到影响。

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;
}
}
}

Person类中需要两个方法。

  1. Hit->这种方法每次命中都会减少自身对象上的HitPoints。当HitPoints变为零时,状态Alive被设置为false

  2. Kill->这将以person为参数,并对该person调用Hit方法,直到该person为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);

希望这能有所帮助!

这就是你的意思吗?

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);

最新更新