C# 从主程序中的子类中检索信息

C# 从主程序中的子类中检索信息,c#,inheritance,console,C#,Inheritance,Console,所以,我正在尝试设置一个基本的计时器功能,用于治疗HP伤害。我已经让计时器工作,但它是从父字符类而不是分配给单个字符的子类调用的,因此从零开始计数,而不是该字符的剩余HP数。如何让它访问子级而不是父级 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Timers; namespace h

所以,我正在尝试设置一个基本的计时器功能,用于治疗HP伤害。我已经让计时器工作,但它是从父字符类而不是分配给单个字符的子类调用的,因此从零开始计数,而不是该字符的剩余HP数。如何让它访问子级而不是父级

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

namespace hpTimer_Test
{
    class Program
    {

        static Timer timer = new Timer(3000);        
        static int i = Knight.HP;

        static void Main(string[] args)
        {
            timer.Elapsed += timer_Elapsed;
            timer.Start();            
            Console.Read();

        }
        private static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            i++;

            //Console.Clear();
            Console.WriteLine("=================================================");
            Console.WriteLine("");
            Console.WriteLine("                Health:  " + i.ToString());
            Console.WriteLine("");
            Console.WriteLine("=================================================");

            if (i == 20)
            {
                timer.Stop();
            }
            GC.Collect();
        }
    }
}
----------字符类

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

namespace hpTimer_Test
{
    class Character
    {
        public static int HP = 0;
        public static int atkP = 0;
        public static bool Dash = false;
        public static double Target; //Equal to chosen enemy Loc
        public static bool isAttacking = false;
        public static int LocX = 1;
        public static int LocY = 1;

        public Character()
        {
        }

        public void HandleInput()
        {

        }

        //public override void Attack()
        //{
        //    if (isAttacking == true)
        //    {
        //        HP -= atkP;                
        //    }
        //}
    }    
}
----------骑士阶级

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

namespace hpTimer_Test
{
    class Knight : Character
    {
        public Knight()
        {
            HP = 10;
            atkP = 10;
            Dash = true;
            isAttacking = false;         
        }
    }
}

这是因为您在第一次使用之外定义了它

首先,不要对Character类的成员使用static,否则每个人都会共享该值。 其次,不要在main之外声明int
i=Knight.H
P,因为您尚未创建Knight类型的对象

class Character
{
    public int HP;
    public int atkP = 0;
    public bool Dash = false;
    public double Target; //Equal to chosen enemy Loc
    public bool isAttacking = false;
    public int LocX = 1;
    public int LocY = 1;

    public Character()
    {
    }

    public virtual void HandleInput()
    {

    }

    //public override void Attack()
    //{
    //    if (isAttacking == true)
    //    {
    //        HP -= atkP;                
    //    }
    //}
}
相反,您的程序应该更像:

class Program 
{
    private static Character Player;

    public static void Main(string[] args) 
    {
        Player = new Knight();
        timer.Elapsed += timer_Elapsed;
        timer.Start();            
        Console.Read();
    }

    private static void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Player.HP++;

        //Console.Clear();
        Console.WriteLine("=================================================");
        Console.WriteLine("");
        Console.WriteLine("                Health:  " + Player.HP);
        Console.WriteLine("");            Console.WriteLine("=================================================");

        if (i == 20)
        {
            timer.Stop();
        }
    }
}

正如MethodMan所提到的,你肯定应该重新编写你的程序,你不应该只使用计时器来控制一个角色的健康状况,相反,你可以设置它来处理游戏中需要处理的任何事情。如果你有一个以上的角色,你可以让他们独立地增加他们的生命值,但现在这只是一个开始。

那么你有没有阅读/看过你发布的代码
class Program
是具有入口点的父类,因此请删除计时器的代码并将其放置在子类中。。另外,在何处创建其他类的实例。。阅读
实例类和静态类之间的区别以及方法。。