[英]C#: Pass derived class as parameter
I have a base class that does calculations on image sizes. 我有一个基类,可以对图像大小进行计算。 I'm deriving a class from that and have predefined image sizes that will be used in my code.
我从中派生一个类,并具有将在我的代码中使用的预定义图像大小。 While what I have works, I have a strong feeling that I'm not doing it properly.
虽然我的作品行之有效,但我有一种强烈的感觉,那就是我做得不好。
Ideally, I'd like to just pass DerviedClass.PreviewSize as the parameter to GetWidth without having to create an instance of it. 理想情况下,我只想将DerviedClass.PreviewSize作为参数传递给GetWidth,而不必创建它的实例。
class Program
{
static void Main(string[] args)
{
ProfilePics d = new ProfilePics();
Guid UserId = Guid.NewGuid();
ProfilePics.Preview PreviewSize = new ProfilePics.Preview();
d.Save(UserId, PreviewSize);
}
}
class ProfilePicsBase
{
public interface ISize
{
int Width { get; }
int Height { get; }
}
public void Save(Guid UserId, ISize Size)
{
string PicPath = GetTempPath(UserId);
Media.ResizeImage(PicPath, Size.Width, Size.Height);
}
}
class ProfilePics : ProfilePicsBase
{
public class Preview : ISize
{
public int Width { get { return 200; } }
public int Height { get { return 160; } }
}
}
It seems to me that you want a more flexible implementation of ISize
- having an implementation which always returns the same value seems fairly pointless. 在我看来,您想要一个更灵活的
ISize
实现-具有始终返回相同值的实现似乎毫无意义。 On the other hand, I can see that you want an easy way of getting the size that you always use for a preview. 另一方面,我可以看到,您想要一种简单的方法来获取始终用于预览的尺寸。 I would do it like this:
我会这样做:
// Immutable implementation of ISize
public class FixedSize : ISize
{
public static readonly FixedSize Preview = new FixedSize(200, 160);
private readonly int width;
private readonly int height;
public int Width { get { return width; } }
public int Height { get { return height; } }
public FixedSize(int width, int height)
{
this.width = width;
this.height = height;
}
}
You could then write: 然后,您可以编写:
ProfilePics d = new ProfilePics();
Guid userId = Guid.NewGuid();
d.Save(userId, FixedSize.Preview);
This would reuse the same instance of FixedSize
whenever you called it. 每当您调用它时,它将重用相同的
FixedSize
实例。
There are a few ways that you could do this, depending on your needs. 有几种方法可以执行此操作,具体取决于您的需求。 I would look at doing a different interface, setup.
我会考虑做一个不同的界面,设置。 Something like this.
这样的事情。
public interface ISizedPics
{
int Width {get; }
int Height {get; }
void Save(Guid userId)
}
public class ProfilePics, iSizedPics
{
public int Width { get { return 200; } }
public int Height { get { return 160; } }
public void Save(Guid UserId)
{
//Do your save here
}
}
Then, with this done, you could actually work with it like this. 然后,完成此操作,您实际上可以像这样使用它。
ISizedPics picInstance = new ProfilePics;
Guid myId = Guid.NewGuid();
picInstance.Save(myId);
This is just one way of doing it, I like this way, as you can easily create a factory class around this that helps you declare the instances as needed. 我喜欢这种方式,这只是其中一种方式,因为您可以轻松地围绕它创建一个工厂类,以帮助您根据需要声明实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.