C# 在多个类中使用某些类型
我有一门课,像:C# 在多个类中使用某些类型,c#,class,C#,Class,我有一门课,像: public class Soru { public void SoruKaydet(){...} private static void SoruEtiketKaydet(Guid soruId, Etiket etiket, DbManager db){...} public Guid? SoruId { get; set; } public Guid? SoranId { get; set; } public int? Baki
public class Soru
{
public void SoruKaydet(){...}
private static void SoruEtiketKaydet(Guid soruId, Etiket etiket, DbManager db){...}
public Guid? SoruId { get; set; }
public Guid? SoranId { get; set; }
public int? BakilmaSayisi { get; set; }
public string HtmlGovde { get; set; }
public string MarkdownGovde { get; set; }
public string Baslik { get; set; }
public int? KategoriId { get; set; }
public DateTime OlusturulmaTarihi { get; set; }
public List<Etiket> Etiketler { get; set; }
}
公共类Soru
{
公共无效SoruKaydet(){…}
私有静态void SoruEtiketKaydet(Guid soruId,Etiket-Etiket,DbManager-db){…}
公共Guid?SoruId{get;set;}
公共Guid?SoranId{get;set;}
公共int?BakilmaSayisi{get;set;}
公共字符串HtmlGovde{get;set;}
公共字符串MarkdownGovde{get;set;}
公共字符串Baslik{get;set;}
公共int?KategoriId{get;set;}
公共日期时间OlusturulmaTarihi{get;set;}
公共列表Etiketler{get;set;}
}
我需要另一个类应该包含类“Soru”中的一些变量。我有两个场景
第一种情况:
public class SoruSayfa
{
public static List<SoruSayfa> SoruSayfaGetir(){...}
public Guid? SoruId { get; set; }
public Guid? SoranId { get; set; }
public int? BakilmaSayisi { get; set; }
public string Baslik { get; set; }
public int? KategoriId { get; set; }
public int DurumId { get; set; }
public double SoruPuani { get; set; }
public int CevapSayisi { get; set; }
public int BakilmaSayisi { get; set; }
public string KullaniciAdi { get; set; }
public double? KisiAlanPuani { get; set; }
}
public class SoruSayfa
{
public static List<SoruSayfa> SoruSayfaGetir(){...}
// Refers the class Soru instead of some variables of it
public Soru MySoru { get; set; }
public int DurumId { get; set; }
public double SoruPuani { get; set; }
public int CevapSayisi { get; set; }
public int BakilmaSayisi { get; set; }
public string KullaniciAdi { get; set; }
public double? KisiAlanPuani { get; set; }
}
公共类SoruSayfa
{
公共静态列表SoruSayfaGetir(){…}
公共Guid?SoruId{get;set;}
公共Guid?SoranId{get;set;}
公共int?BakilmaSayisi{get;set;}
公共字符串Baslik{get;set;}
公共int?KategoriId{get;set;}
公共int DurumId{get;set;}
公共双SoruPuani{get;set;}
公共int-cevapsaysi{get;set;}
公共int BakilmaSayisi{get;set;}
公共字符串KullaniciAdi{get;set;}
公共双?KisiAlanPuani{get;set;}
}
第二种情况:
public class SoruSayfa
{
public static List<SoruSayfa> SoruSayfaGetir(){...}
public Guid? SoruId { get; set; }
public Guid? SoranId { get; set; }
public int? BakilmaSayisi { get; set; }
public string Baslik { get; set; }
public int? KategoriId { get; set; }
public int DurumId { get; set; }
public double SoruPuani { get; set; }
public int CevapSayisi { get; set; }
public int BakilmaSayisi { get; set; }
public string KullaniciAdi { get; set; }
public double? KisiAlanPuani { get; set; }
}
public class SoruSayfa
{
public static List<SoruSayfa> SoruSayfaGetir(){...}
// Refers the class Soru instead of some variables of it
public Soru MySoru { get; set; }
public int DurumId { get; set; }
public double SoruPuani { get; set; }
public int CevapSayisi { get; set; }
public int BakilmaSayisi { get; set; }
public string KullaniciAdi { get; set; }
public double? KisiAlanPuani { get; set; }
}
公共类SoruSayfa
{
公共静态列表SoruSayfaGetir(){…}
//引用类Soru,而不是它的某些变量
公共Soru MySoru{get;set;}
公共int DurumId{get;set;}
公共双SoruPuani{get;set;}
公共int-cevapsaysi{get;set;}
公共int BakilmaSayisi{get;set;}
公共字符串KullaniciAdi{get;set;}
公共双?KisiAlanPuani{get;set;}
}
在第一个场景中,没有未使用的额外变量,但在第二个场景中,MySoru的一些变量未使用(HtmlGovde、MarkdownGovde、OlusturulmaTarihi、Etiketler)。此外,Soru和SoruSayfa类将用作asp.NETMVC中不同操作的模型。它们包含不同的方法。哪种情况更好 试试第三种情况;)
公共类SoruBase
{
公共Guid?SoruId{get;set;}
公共Guid?SoranId{get;set;}
公共int?BakilmaSayisi{get;set;}
公共字符串Baslik{get;set;}
公共int?KategoriId{get;set;}
}
公共类Soru:SoruBase
{
公共字符串HtmlGovde{get;set;}
公共字符串MarkdownGovde{get;set;}
公共日期时间OlusturulmaTarihi{get;set;}
公共列表Etiketler{get;set;}
}
公共类SoruSayfa:SoruBase
{
公共int DurumId{get;set;}
公共双SoruPuani{get;set;}
公共int-cevapsaysi{get;set;}
公共int BakilmaSayisi{get;set;}
公共字符串KullaniciAdi{get;set;}
公共双?KisiAlanPuani{get;set;}
}
我可以说第一种情况是最糟糕的,但这并不意味着第二种情况是最好的。您应该更好地解释第二个类的使用上下文:这些类是什么?它们应该是子类吗?如果是,它们是否满足?它们不是子类,它们将在.net mvc中用作模型以满足不同的目标。@RogiervanhetSchip我正在搜索LSP,谢谢。如果有两个以上的类,它们具有共享属性,但其中三个不共享这些属性(因此不能有基类),该怎么办?您可以在类中对这些共享属性进行分组,并在需要时将该类用作新属性。还可以尝试将单个/多个类继承与按类分组的属性相结合。