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

时间:2018-11-11 01:05:53

标签: c# oop object

在下面的代码中,我正在尝试学习如何使对象彼此交互,因为我觉得这比我现在所做的要重要得多,后者只是收集分配给每个对象的一堆变量。

为了娱乐,我想要这些不同的对象做的是互相残杀。叫杰克的人可以杀死。其他两个不能。我要杰克做的是打击其他两个,使它们多次失去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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这是你的意思吗?

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

答案 1 :(得分:0)

varchar(max)类中需要2个方法。

  1. Guid?->此方法会在每次命中时减少自身对象上的protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Model1>().Property(e => e.Property1).HasConversion(p => p.ToString(), p => (Guid?)Guid.Parse(p)); } 。当Person变为零时,状态Hit设置为HitPoints

  2. 杀死->这将以人员为参数,并对该人员调用HitPoints方法,直到该人员为Alive

false类添加以下方法:

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

希望这会有所帮助!