第一次发布一个问题,因为直到现在我总能找到我在这里寻找的答案所以请耐心等待。 :)
关于这个问题,我正在测试我在控制台应用程序中制作的界面,以便在游戏中使用。 我有一个角色的界面,也为它做了一个类。在我的主要代码中,我可以成功地调用我的" sCharacterName"来自我的班级(Character1Class)。
在我的(SwordClass)我有装备武器的空白,我希望它显示如下:"(sCharacterName)装备(sWeaponName)。" 但每当我尝试它时,它会显示为空格而不是名称。 这是我的代码的一部分:
namespace ConsoleInterfaceTest
{
class Program
{
public interface ICharacters
{
// The name of the character
string sCharacterName { get; set; }
}
public interface IWeapon
{
// The name of the weapon
string sWeaponName { get; set; }
void Equip();
}
public class Character1Class : ICharacters
{
// The name of the character
public string sCharacterName { get; set; }
public Character1Class(string v)
{
sCharacterName = ("ZeeAars");
}
}
public class SwordClass : IWeapon, Characters.ICharacters
{
// The name of the weapon
public string sWeaponName { get; set; }
public string sCharacterName { get; set; }
public SwordClass(string sName)
{
sWeaponName = sName;
}
public void Equip()
{
Console.WriteLine(sCharacterName + " equiped " + sWeaponName + ".");
}
}
public static void Main (string[] args)
{
Characters.Character1Class character1 = new Characters.Character1Class("");
character1.CharacterDamage(50);
Weapons.SwordClass sword = new Weapons.SwordClass("TestSword");
sword.Equip ();
Console.ReadKey();
}
}
}
先谢谢,这个问题真的让我头疼了几天:D
答案 0 :(得分:1)
我同意其他人的意见,你需要重新思考如何使用界面以及如何构建对象层次结构,而只是回答问题;
Weapons.SwordClass sword = new Weapons.SwordClass("TestSword");
// Add the following line to your code
sword.sCharacterName = character1.sCharacterName;
sword.Equip();
答案 1 :(得分:0)
绝对要考虑一下结构。问问题。一些可能的问题包括。
武器是否有角色或角色拥有武器? 角色拥有武器。
所以现在我们知道武器应该是角色的属性。
角色拥有多少武器? 一个角色可以拥有许多武器,但只能装备尽可能多的武器。
所以现在我们知道我们的武器属性需要存储多个武器但可装备少量武器。
public interface ICharacters
{
string sCharacterName { get; set; }
List<IWeapon> Weapons { get; }
void Equip(IWeapon toEquip);
}
public interface IWeapon
{
string sWeaponName { get; }
}
public class Character1Class : ICharacters
{
public string sCharacterName { get; set; }
public List<IWeapon> Weapons { get; }
public Character1Class(string v)
{
Weapons = new List<IWeapon>();
sCharacterName = v;
}
public void Equip(IWeapon toEquip)
{
if (Weapons.Contains(toEquip))
Console.WriteLine(sCharacterName + " equiped " + toEquip + ".");
else Console.WriteLine(sCharacterName + " doesn't have " + toEquip + " to equip.");
}
}
public class SwordClass : IWeapon
{
public string sWeaponName { get; }
public SwordClass(string sName)
{
sWeaponName = sName;
}
}
class Program
{
public static void Main(string[] args)
{
Character1Class character1 = new Character1Class("Frodo");
SwordClass sword = new SwordClass("TestSword");
character1.Weapons.Add(sword);
character1.Equip(sword);
Console.ReadKey();
}
}
答案 2 :(得分:0)
我认为您正在寻找这样的事情:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
Character1Class character = new Character1Class("Zoltan the Powerful");
SwordClass sword = new SwordClass("Holy Avenger");
character.Equip(sword);
character.Attack();
Console.ReadKey();
}
public interface ICharacters
{
// The name of the character
string sCharacterName { get; set; }
void Equip(IWeapon weapon);
}
public interface IWeapon
{
// The name of the weapon
string sWeaponName { get; set; }
int Attack();
}
public class Character1Class : ICharacters
{
// The name of the character
public string sCharacterName { get; set; }
public Character1Class(string charName)
{
sCharacterName = charName;
}
public void Equip(IWeapon weapon)
{
MyWeapon = weapon;
Console.WriteLine(sCharacterName + " equiped " + weapon.sWeaponName + ".");
}
public IWeapon MyWeapon { get; set; }
public void Attack()
{
if (MyWeapon == null)
{
Console.WriteLine(sCharacterName + " punches for 1 pt of damage.");
}
else
{
Console.WriteLine(string.Format("{0} swings for {1} pt of damage.", sCharacterName, MyWeapon.Attack()));
}
}
}
public class SwordClass : IWeapon
{
// The name of the weapon
public string sWeaponName { get; set; }
public SwordClass(string sName)
{
sWeaponName = sName;
}
public int Attack()
{
return 50;
}
}
}
}