Get values from object through class

谁说我不能喝 提交于 2019-12-12 02:28:15

问题


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;
    }
}

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


回答1:


Other way to achieve it.

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) };
    }
}



回答2:


That's because Dirt_Block is a BlockType, not a Block. In fact, I wouldn't expect this code to compile, as there is no way of converting a Block to a BlockType.

I'm not quite sure what you're after, but it sounds like you have a collection of block types that would suit an enumeration:

public enum BlockType
{
   Dirt,
   Wood,
   Metal
}

and you have a collection of Blocks that each have a block type:

public class Block
{
   public BlockType Type { get; set; }
}

Here, we are using a public auto property. You should avoid using public fields (no getters or setters).

Now, to create a new Block, you can use object initialisation:

var block = new Block { Type = BlockType.Metal };

Alternatively, you could create a Block constructor which takes a BlockType parameter.




回答3:


Assuming you mean Dirt_Block to be of type Block - your Block Value field is static - that means it is associated with the Block type, not any particular instance. Make it a public field (or property) and your access will work:

public class BlockType
{
     public static Block Dirt_Block = new Block("Blah! values here");
}

public struct Block
{
  public string Value;

  public Block(string value)
  {
    value = value;
  }
}

Also if there is no particular reason you are going with a struct here, I would suggest making Block a class and expose Value as a public property.




回答4:


Your doubt isn't very clear... but I think what you want is something like this... using OOP paradigm on the block classes.

public class BlockType
{
    public virtual string Value;

    public static BlockType Dirt = new Block("Blah! values here");
}

public class Block : BlockType
{
    private string value;

    public Block(string value)
    {
        this.value = value;
    }

    public string Value 
    {
        get { return value; }
    }
}

Right?



来源:https://stackoverflow.com/questions/9557712/get-values-from-object-through-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!