Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:将派生类作为参数传递_C#_Derived Class - Fatal编程技术网

C#:将派生类作为参数传递

C#:将派生类作为参数传递,c#,derived-class,C#,Derived Class,我有一个基类,可以计算图像大小。我将从中派生一个类,并具有预定义的图像大小,这些图像大小将在我的代码中使用。虽然我所做的一切都有效,但我有一种强烈的感觉,那就是我做得不好 理想情况下,我只想将DerviedClass.PreviewSize作为参数传递给GetWidth,而不必创建它的实例 class Program { static void Main(string[] args) { ProfilePics d = new ProfilePics();

我有一个基类,可以计算图像大小。我将从中派生一个类,并具有预定义的图像大小,这些图像大小将在我的代码中使用。虽然我所做的一切都有效,但我有一种强烈的感觉,那就是我做得不好

理想情况下,我只想将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; } }
    }
}

根据您的需要,有几种方法可以做到这一点。我会考虑做一个不同的界面,设置。像这样的

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
    }
}
ISizedPics picInstance = new ProfilePics;
Guid myId = Guid.NewGuid();
picInstance.Save(myId);
这样,你就可以像这样工作了

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
    }
}
ISizedPics picInstance = new ProfilePics;
Guid myId = Guid.NewGuid();
picInstance.Save(myId);

这只是一种方法,我喜欢这种方法,因为您可以很容易地围绕它创建一个工厂类,帮助您根据需要声明实例。

在我看来,您需要一个更灵活的
ISize
实现—拥有一个总是返回相同值的实现似乎毫无意义。另一方面,我可以看出,您需要一种简单的方法来获取预览时经常使用的大小。我会这样做:

// 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;
    }
}
然后你可以写:

ProfilePics d = new ProfilePics();
Guid userId = Guid.NewGuid();

d.Save(userId, FixedSize.Preview);

无论何时调用它,都会重复使用相同的
FixedSize

您能给出一个更完整的示例来说明如何使用该类吗?感谢Neil的回复。我已经更新了示例,以更准确地反映我的情况。(ProfilePicsBase是单独项目中的一个类,因此我从中派生它,使其成为项目特定的)事实上,您使用Pascal case(大写字母)作为变量,这让我很困惑……这正是我想要的。非常感谢!我不知道为什么我不能全神贯注于它。Jon-对于ProfilePics类来说,尽管必须存在公共只读属性,但仍然可以访问宽度和高度。