【发布时间】:2013-10-10 15:28:28
【问题描述】:
我有一个名为 ImageProperty 的实用程序类,用于存储特定图像属性的类型和值。
类型只能是以下枚举值之一:
public enum ImagePropertyType { SIZE, H_RES, V_RES, BIT_COUNT, IS_ALPHA }
这些值都是不同的类型(Size、float、float、int、布尔)。
我的 ImageProperty 类的简化形式如下:
public class ImageProperty
{
private ImagePropertyType type;
private object value;
public ImageProperty(ImagePropertyType type, object value)
{
this.type = type;
this.value = value;
}
public ImagePropertyType getType()
{
return this.type;
}
public void setType(ImagePropertyType type)
{
this.type = type
}
public object getValue()
{
return this.value;
}
public void setValue(object value)
{
this.value = value;
}
}
注意使用 object 来获取/设置值(因为类型不同)。
我想让我的类通用,因为我不喜欢使用对象,所以我对类和方法进行了一些更改:
public class ImageProperty<T>
{
...
private T value;
...
public ImageProperty(ImagePropertyType type, T value)
...
public T getValue()
...
public void setValue(T value)
...
}
我在另一个类中有一个函数,它需要根据给定的类型返回 ImageProperty 的实例。
public ???? getImageProperty(ImagePropertyType type, Bitmap bitMap)
{
switch(type)
{
case SIZE:
return new ImageProperty<Size>(type, bitMap.Size);
case H_RES:
return new ImageProperty<float>(type, bitMap.HorizontalResolution);
case V_RES:
return new ImageProperty<float>(type, bitMap.VerticalResolution);
...
...
}
}
我不确定该方法的返回类型是什么(因此是 ????)。
我不能只说:
public ImageProperty getImageProperty(ImagePropertyType type, Bitmap bitMap)
因为 ImageProperty 类需要参数化。
显然,如果值类型总是,比如说,int,我会将返回类型设置为:
public ImageProperty<int> getImageProperty(ImagePropertyType type, Bitmap bitMap)
有没有办法将 getImageProperty 的返回类型定义为“任何或未知的参数化值”?
类似:
public ImageProperty<?> getImageProperty(ImagePropertyType type, Bitmap bitMap)
参数化类不是一个好主意,因为我不知道要返回的值的类型吗?
我是否应该将 ImageProperty 类设为非泛型(如我帖子中的第一个类)并返回使用 object 作为返回类型,如果我需要知道值类型,我可以使用它类型?
object value = getImageProperty(ImagePropertyType.SIZE, Bitmap bitMap).getValue();
Type t = typeof(value);
谢谢。
----更新---------------------------------
根据 Knaģis 的建议和进一步阅读,我决定保留原始类,然后创建一个扩展 ImageProperty 的通用类:
public class ImageProperty<T> : ImageProperty
{
private T propertyValue;
public ImageProperty()
: base()
{
}
public ImageProperty(ImagePropertyType propertyType, T propertyValue)
: base(propertyType, propertyValue)
{
this.propertyValue = propertyValue;
}
public T getPropertyValue()
{
return propertyValue;
}
public void setPropertyValue(T propertyValue)
{
this.propertyValue = propertyValue;
}
}
不过,有一件事。我收到一个编译器警告,上面写着:
ImageProperty<T>.getPropertyValue() hides inherited member ImageProperty.getPropertyValue(). Use the new keyword if hiding was intended.
我添加了 new 关键字:
public new T getPropertyValue()
奇怪,我从来不知道在方法声明中使用了 new 关键字以及它是如何使用的。有人愿意解释吗?
【问题讨论】:
-
new关键字仅用于开发人员可以通知编译器您实际上打算让该方法与基本方法具有相同的名称(新方法无法访问) .即使没有关键字,该方法的工作原理也是一样的。
标签: c# class generics undefined parameterized