通过类获取对象的值 [英] Get values from object through class
问题描述
public class BlockType
{
public static BlockType Dirt_Block = new Block("Blah! values here");
}
public struct Block
{
public static string Value;
public Block(string value)
{
value = value;
}
}
有什么方法可以从DirtBlock ? BlockType.Dirt_Block.Value Dosent工作,我不知道这是否可能,如果没有任何方法获得相同的结果? (Theres更多的值,但我缩短它的大小)Btw,Im访问BlockType.Dirt.value从anouther类。我只想通过
Is there any way I can get the value from DirtBlock? BlockType.Dirt_Block.Value Dosent work, Im not sure if this is possible, if not any ways to get the same result? (Theres more values, but I shortened it for size) Btw, Im accessing BlockType.Dirt.value from anouther class. I only want to get one value out of it though
推荐答案
获得一个值。
我认为这个Approah提供了一种更简单的方法来加载/保存tilemaps,并根据需要从文本中定义块属性。
I think this approah provides an easier way to load/save tilemaps, and let defining block properties from text if needed.
方法和密集型继承对于可能需要优化运行的游戏来说不是一个好主意。
Strict OOP with virtual methods and intensive inheritance is not a good idea for a game that may need optimizations to run fine.
当然可以通过其他方式改进或完成。 ;)
Of course it can be improved or done in other way. ;)
public struct BlockProperty
{
public Texture2D Texture;
public string Name;
}
public class BlockProperties
{
static readonly List<BlockProperty> Blocks;
public static readonly BlockProperty Dirt;
public static readonly BlockProperty Grass;
public static readonly BlockProperty Wall;
static BlockProperties( )
{
ContentManager Content = Game1.Instance.Content;
Blocks = new List<BlockProperty>( )
{
(Dirt = new BlockProperty( ) { Name = "Dirt", Texture = Content.Load<Texture2D>( "textures/dirt" ) }),
(Grass = new BlockProperty( ) { Name = "Grass", Texture = Content.Load<Texture2D>( "textures/grass" ) }),
(Wall = new BlockProperty( ) { Name = "Wall", Texture = Content.Load<Texture2D>( "textures/wall" ) }),
};
}
public static BlockProperty ByName( string name )
{
return Blocks.FirstOrDefault( b => b.Name == name );
}
}
public class Block
{
public BlockProperty BlockType;
pulic Vector2 Position;
public Block( BlockProperty block )
{
this.BlockType = block;
}
public Block( string block )
{
this.BlockType = BlockProperties.ByName(block);
}
}
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
public static Game1 Instance { get; private set; }
Block Block;
public Game1( )
{
Instance = this;
graphics = new GraphicsDeviceManager( this );
}
protected override void LoadContent( )
{
Block = new Block("Dirt") { Position = new Vector2(100,100) };
}
}
这篇关于通过类获取对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!