C# 不确定是否使用字符串或类
将一系列整数作为一个组存储在一起并与其他整数组进行比较的最佳方法是什么。我正在用C#编写一个RPG,作为一个类项目,并试图编写自己的代码。我只比较了3个属性,一个攻击属性,一个防御属性和一个速度属性。我还有四个角色类别。每个都有三个属性。我应该为字符类使用单独的类,还是字符串更适合我?我将在注释中列出我的意思。使用基本C# 不确定是否使用字符串或类,c#,string,class,C#,String,Class,将一系列整数作为一个组存储在一起并与其他整数组进行比较的最佳方法是什么。我正在用C#编写一个RPG,作为一个类项目,并试图编写自己的代码。我只比较了3个属性,一个攻击属性,一个防御属性和一个速度属性。我还有四个角色类别。每个都有三个属性。我应该为字符类使用单独的类,还是字符串更适合我?我将在注释中列出我的意思。使用基本字符类: public abstract class Character { public int Attack { get; set; } public int
字符
类:
public abstract class Character
{
public int Attack { get; set; }
public int Defense { get; set; }
public int Speed { get; set; }
}
然后,类将继承字符
。例如,法师的防御能力较低:
public class Mage : Character
{
public Mage()
{
Defense = 1;
Attack = 5;
Speed = 5;
}
}
始终保持代码干爽我找到了自己更简单的方法。类的复杂性有点超出了我的技能障碍(因为现在我将有一个编码类,它将正确地指导我们如何使用类)。我想出了我认为可能是解决我的问题最简单最简单的答案。只需使用stats作为玩家的静态int,然后让readline使用While循环根据readline输入添加或减去stats
{
int GoblinKingHealth = 100;
int GoblinKingAtk = 7;
int GoblinKingDef = 0;
int GoblinKingSpd = 3;
int PlayerHealth = 100;
int PlayerAtk = 0;
int PlayerDef = 0;
int PlayerSpd = 0;
Console.WriteLine( "Chose your class." );
Console.WriteLine( "Fighter" );
Console.WriteLine( "Ninja" );
Console.WriteLine( "Mage" );
Console.WriteLine( "Knight" );
while( true )
{
string line = Console.ReadLine();
if( line == "Fighter" )
{
PlayerAtk = +4;
PlayerDef = +4;
PlayerSpd = +2;
Console.WriteLine( "You have chosen the Fighter class. The Fighter class is the most well balanced class with average attack and defense and slightly under average speed" );
break;
}
if( line == "Ninja" )
{
PlayerAtk = +5;
PlayerDef = +2;
PlayerSpd = +4;
Console.WriteLine("You have chosen the Ninja class. The Ninja class is the fastest of the classes and has above average attack but a lower defense than the fighter.");
break;
}
if( line == "Mage" )
{
PlayerAtk = +9;
PlayerDef = +0;
PlayerSpd = +3;
Console.WriteLine( "You have chosen the Mage class. The Mage class has the strongest attack but the weakest defense and above average speed." );
break;
}
if( line == "Knight" )
{
PlayerAtk = +1;
PlayerDef = +6;
PlayerSpd = +1;
Console.WriteLine( "You have chosen the Knight class. The Knight class has near impenitrable defense at a heavy cost of speed and attack power." );
break;
}
如果这些是数字,那么字符串是最糟糕的选择。使用
列表
我投票使用角色类你怎么知道@cricket_007?没有代码,不要假设!为什么是角色课?我被否决有什么原因吗?这不是一个合适的问题格式还是什么?使用3种不同的int
属性、攻击
、防御
和速度
。将数字存储在字符串甚至列表中是没有意义的。每一个数字都意味着一些特定的东西,为什么要让它变得更复杂呢?听起来您需要一个具有这3个属性的基本Character
类(或接口),然后是继承Character
的4个类,我在这里避免了固有性。这样的层次结构在游戏中往往会出现问题。相反,用作文:public-class-FighterStats{public-int-Health;…}
,然后public-class-Mage{public-FighterStats-FighterStats;}
由于课程的复杂性,我的老师特别不使用这些课程。我能推断出的答案是更简单的版本。只使用静态整数而不使用类。使用类只为变量分配不同的值是一个非常糟糕的主意。在玩家
上使用静态整数意味着对一个玩家的能力的任何更新都会用完全相同的值更新其他玩家的能力。根据您自己的回答,这里没有任何静态
字段,即PlayerAtk
是类上的实例字段。更多信息:请记住,将来像这样的代码是非常糟糕的。代码重复,while(true)
,看起来像一个无限循环,变量命名和用法等。