如何自动为字段添加值 [英] How do I add a value to a field automaticly

查看:85
本文介绍了如何自动为字段添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我希望我能够解释这个问题。我有两个班class1和class2



宠物和宠物主人。



Hi Guys

I hope I will be able to explain this problem. I have two classes class1 and class2

Pet and pet owner.

public class Petowner
    {
        private String Name;
        private int age;

        public Petowner()
        {
            Name = "Joppe";
            age = 100;

        }
    }






and

class Pet

    {
        private string name;
        private int hunger;

        public Pet()
        {
            name = "Pelle";
            hunger = 100;
        }

        public void SetHunger(int _hunger)
        {
            hunger = hunger - _hunger;
        }
    }





我的尝试:



我希望每个班级都有一个实例



然后我运行程序(用instanses宠物和petowner)。然后我想让饥饿场在程序运行时自动脱脂。这可能吗?



谢谢



Johan



What I have tried:

I want to have one instance of each class

then while i run the program (doing stuff with the instanses pet and petowner). then I want to make the hunger field to degrease automaticly while the program is runing. Is this possible?

Thanks

Johan

推荐答案

你好,是的,有一种方法,使用计时器。



C#中有几个计时器类,最好查看所有这些类并选择适合您需求的产品!

所有关于。 NET计时器 - 比较 [ ^ ]



更新:

Hello, yes, there is a way, using timers.

There are several timer classes in C#, best to look on all of them and choose the one that suits your needs!
All about .NET Timers - A Comparison[^]

Update:
private static Pet vov;

static void Main(string[] args)
{
    vov = new Pet();
    var myTimer = new System.Timers.Timer();
    myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
    myTimer.Interval = 1000;
    myTimer.Start();

    while (Console.Read() != 'q')
    {

    }
    Console.ReadKey();
}

public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
{
    vov.SetHunger(10);
    Console.Write("\r{0}", DateTime.Now);
}


除了解决方案1之外,你可能想要维护一份宠物清单,而不是只为一只宠物工作



Further to Solution 1 you might want to maintain a list of pets rather than working on just one

public class Pet
{
    public string Name { get; set; }
    public int Hunger { get; private set; }
    public bool IsAlive { get; private set; }

    public Pet(string name)
    {
        Name = name;
        Hunger = 100;
        IsAlive = true;
    }

    public void SetHunger(int _hunger)
    {
        Hunger = Hunger - _hunger;

        if (Hunger <= 0)
        {
            IsAlive = false;
        }
    }
}







private static List<Pet> pets = new List<Pet>();

static void Main(string[] args)
{
    var myTimer = new System.Timers.Timer();
    myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
    myTimer.Interval = 2000;
    myTimer.Start();

    string line;

    // Type a pet name followed by [RETURN] to create a pet of that name
    while ((line = Console.ReadLine()) != "q")
    {
        Pet pet = CreatePet(line);
        // you can do something with pet here if you want
    }
}

public static Pet CreatePet(string name)
{
    Pet pet = new Pet (name);

    pets.Add(pet);

    return pet;
}

public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
{
    foreach (Pet pet in pets)
    {
        pet.SetHunger(10);

        if (pet.IsAlive)
        {
            Console.WriteLine("Pet:{0} Hunger:{1}", pet.Name, pet.Hunger);
        }
        else
        {
            Console.WriteLine("{0} died", pet.Name);
        }
    }

    pets.RemoveAll(p => p.IsAlive == false);
}


这篇关于如何自动为字段添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆