Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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# MVC C3继承类的正确格式是什么?_C#_Oop - Fatal编程技术网

C# MVC C3继承类的正确格式是什么?

C# MVC C3继承类的正确格式是什么?,c#,oop,C#,Oop,我创建了以下类,它描述了用户可以上载的任何类型的文件: namespace MyModels.Models { public class File { public string FileName { get; set; } public string FileTypeId { get; set; } public string URLFileName { get; set; } //cleaned for web

我创建了以下类,它描述了用户可以上载的任何类型的文件:

namespace MyModels.Models
{
    public class File
    {
        public string FileName { get; set; }
        public string FileTypeId { get; set; }
        public string URLFileName { get; set; } //cleaned for web
        public string Dir { get; set; } //which directory is located in
        public long FileSize { get; set; } //size in bytes
        public DateTime DateAdded { get; set; } //date uploaded
        public int UploadedByUser {get; set;} //UserID of user
        public bool inCloud { get; set; } // moved to cloud
        public bool inGlacier { get; set; } // moved to glaciers
        public DateTime? DateTrashed { get; set; } //date user deleted
        public int TrashedByUser { get; set; } //UserID of user
        public List<FileDescendent> Descendents { get; set; } //List of copies
    }
}
还是这个

namespace MyModels.Models
{
    public class Image
    {
        public int OrigHeight { get; set; }
        public int OrigWidth { get; set; }
        public File File { get; set; }
    }
}
请问有什么区别


谢谢

我认为区别在于API。您希望将inCloud查找为
image.inCloud
还是
image.File.inCloud


在大多数情况下,我更喜欢
image.inCloud
。但是,例如,如果实例化文件的成本很高,则可能首选第二个访问器模式。

第一个示例是继承,第二个示例不是

在第二个示例中,您还将拥有文件属性,但将其捆绑在
文件
属性中。例如,要访问
FileName
属性,可以使用
obj.File.FileName
而不是继承类时获得的
obj.FileName


另外,由于
文件
图像
类中的一个属性,因此在创建
图像
类的实例时,不会自动创建它。您还必须创建
文件
类的实例,并将其放入
图像
实例的
文件
属性中。

我选择继承。它将允许您为这两种应用程序使用依赖项注入模型。MVC3非常有用的东西

第二大优势是,您可以拥有显示文件列表的共享视图,并且可以传入这两种类型的集合。更不用说你写的任何东西了。

这是一本书。在这种情况下,图像是一个文件

public class Image : File
{
    public int OrigHeight { get; set; }
    public int OrigWidth { get; set; }
}
public class Image
{
    public int OrigHeight { get; set; }
    public int OrigWidth { get; set; }
    public File File { get; set; }
}
这是一个很好的例子。在这种情况下,图像有一个文件

public class Image : File
{
    public int OrigHeight { get; set; }
    public int OrigWidth { get; set; }
}
public class Image
{
    public int OrigHeight { get; set; }
    public int OrigWidth { get; set; }
    public File File { get; set; }
}

对我来说,图像是一个文件。因此,继承在这里更合适。

我认为这是一个一般的对象设计问题,而不是特定于MVC3。谢谢,这是一个非常清楚的答案。从概念上讲,我现在可以理解其中的区别了。非常感谢。谢谢你的回复!他们都很有帮助。谢谢你!