如何做“静态重载常量"?在C#中? [英] How to do "static overloaded const" in C#?

查看:129
本文介绍了如何做“静态重载常量"?在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#创建游戏.每个关卡都由几个图块组成.每个图块是某种类型的,例如草地板,木墙等.

I'm creating a game in C#. Each level is made up of several tiles. Each tile is of a certain type e.g. grass floor, wooden wall, etc.

理想情况下,我希望有一个基类"Tile"并从中继承以为每种类型的图块创建类.我想拥有子类的某种static/const/etc成员的tile属性,因为每种类型的tile只会具有相同的属性. IE.我不希望100个Tiles具有一个都具有相同值的属性,这似乎效率很低.

Ideally I would like to have one base class "Tile" and inherit from it to create classes for each type of tile. I would like to have the properties of the tile some kind of static/const/etc member of the sub-class since each type of tile is only ever going to have the same properties. I.e. I don't want 100 Tiles have a property which all have the same value, that seems rather inefficient.

问题是您无法在C#中做到这一点.有什么方法可以实现我想要的?

The problem is you can't do that in C#. Are there any ways to achieve what I want?

我的另一种想法是将其全部分解为树,一个树只有一个代表实例的类"Tile",另一个只有一个"TileType",从中我为每种类型实例化一个对象,并可能通过某种"TileTypeCollection".不过,这感觉很奇怪,我宁愿以第一种方式来做.

My alternative idea was to separate it all into to trees, one with only the class "Tile" which represents an instance and another "TileType" from which I instanciate one object for each type and maybe access them through some kind of "TileTypeCollection". This feels strange though and I would rather do it in the first way.

在处理这种情况时,是否有任何一般性准则?

Are there any general guidelines when dealing with a situation like this?

推荐答案

您正在寻找FlyWeight设计模式:

You are looking for the FlyWeight design pattern:

http://en.wikipedia.org/wiki/Flyweight_pattern

Flyweight是一种软件设计 图案.轻量级物体是 通过共享为最大程度地减少内存使用 与其他人尽可能多的数据 类似物体这是一种使用方式 一个简单的大量对象 重复表示将使用 不能接受的内存量.

Flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory.

您在此处有一些C#示例: http://www.dofactory.com/模式/PatternFlyweight.aspx#_self2

You have some C# samples here: http://www.dofactory.com/Patterns/PatternFlyweight.aspx#_self2

这篇关于如何做“静态重载常量"?在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆