我正在做一个c#项目,我正在编写一些小游戏,目的是使c#变得更好和更自信。游戏是关于英雄的,我想在列表框中显示英雄的统计信息。我已经为每个变量获取并设置了方法,但是如果我想将它们添加到字符串中,则只需为int变量添加“”或0。
我改变了一些方法,但是没有帮助(我对编程很陌生)
英雄阶层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
class Hero : Character
{
int health, max_health, dmg, gold, rüstung;
string name;
public Hero(string name, int health, int max_health, int dmg, int rüstung, int gold) : base(name, health, dmg)
{
this.name = name;
this.health = health;
this.max_health = max_health;
this.dmg = dmg;
this.gold = gold;
this.rüstung = rüstung;
}
public void getsDamaged(int d)
{
d = d - rüstung;
if (health - d > 0) health -= d;
else health = 0;
}
public void heal(int h)
{
if (health + h < max_health) health += h;
else health = max_health;
}
public int Gold
{
get { return gold; }
set { gold = gold + value; }
}
public int Health { get; set; }
public string Name { get; set; }
public int Max_health{get;set;}
public int Dmg { get; set; }
public int Protection { get; set; }
}
}
以及应该显示统计信息的方法:
private void show_stats(Hero h)
{
string hero_name;
hero_name = "Name: "+ h.Name;
listBox1.Items.Add(hero_name);
listBox1.Items.Add("HP: " + Convert.ToString(h.Health) + "/" +Convert.ToString(h.Max_health));
listBox1.Items.Add("Dmg: " + Convert.ToString(h.Dmg));
listBox1.Items.Add("Protection: " + Convert.ToString(h.Protection));
listBox1.Items.Add("Gold: " + Convert.ToString(h.Gold);
}//
正如我所说,与其说显示实际统计信息,不如说是
Name:
HP: 0/0
Dmg: 0
Protection: 0
Gold: 0
我添加的英雄在0上没有这些统计数据
答案 0 :(得分:3)
问题是您的构造函数正在设置成员变量,但是您正在使用自动属性。这些自动属性不使用成员变量。
要解决此问题,请删除成员变量并直接使用属性。您的课程如下所示:
class Hero : Character
{
int rüstung, gold;
public Hero(string name, int health, int max_health, int dmg, int rüstung, int gold) : base(name, health, dmg)
{
this.Name = name;
this.Health = health;
this.Max_health = max_health;
this.Dmg = dmg;
this.Gold = gold;
this.rüstung = rüstung;
}
public void getsDamaged(int d)
{
d = d - rüstung;
if (Health - d > 0) Health -= d;
else Health = 0;
}
public void heal(int h)
{
if (Health + h < Max_health) Health += h;
else Health = Max_health;
}
public int Gold
{
get { return gold; }
set { gold = gold + value; }
}
public int Health { get; set; }
public string Name { get; set; }
public int Max_health{get;set;}
public int Dmg { get; set; }
public int Protection { get; set; }
}
答案 1 :(得分:2)
您从未触摸过您的媒体资源。更改属性以表示字段数据:
namespace Game
{
Public class Hero : Character
{
int health, max_health, dmg, gold, rüstung;
string name;
public Hero(string name, int health, int max_health, int dmg, int rüstung, int gold) : base(name, health, dmg)
{
this.name = name;
this.health = health;
this.max_health = max_health;
this.dmg = dmg;
this.gold = gold;
this.rüstung = rüstung;
}
public void getsDamaged(int d)
{
d = d - rüstung;
if (health - d > 0) health -= d;
else health = 0;
}
public void heal(int h)
{
if (health + h < max_health) health += h;
else health = max_health;
}
public int Gold
{
get { return gold; }
set { gold = gold + value; }
}
public int Health { get { return this.heath; } }
public string Name { get { return this.name; } }
public int Max_health { get { return this.max_health; } }
public int Dmg { get { return this.dmg; } }
public int Protection { get; set; }
}
}