C# 两个泛型与无穷大
我有两个接口。我想将它们用于列表和数组C# 两个泛型与无穷大,c#,C#,我有两个接口。我想将它们用于列表和数组 public interface IBook<T> { string Name { get; set; } T Authors { get; set; } int PagesCount { get; set; } } public interface IAuthor<T> { string Name { get; set; } T Books { get; set; } } class A
public interface IBook<T>
{
string Name { get; set; }
T Authors { get; set; }
int PagesCount { get; set; }
}
public interface IAuthor<T>
{
string Name { get; set; }
T Books { get; set; }
}
class Author<T> : IAuthor<IBook<T>[]>
where T : IAuthor<IBook<T>[]>
{
public string Name { get; set; }
public IBook<T>[] Books { get; set; }
}
class Book<T> : IBook<IAuthor<T>[]>
where T : IBook<IAuthor<T>[]>
{
public string Name { get; set; }
public IAuthor<T>[] Authors { get; set; }
public int PagesCount { get; set; }
public static void Main(string[] args)
{
Author<IBook<IAuthor<...>[]>[]> a = new Author<>();
}
}
公共接口IBook
{
字符串名称{get;set;}
T作者{get;set;}
int pagesunt{get;set;}
}
公共接口作者
{
字符串名称{get;set;}
T书籍{get;set;}
}
类作者:IAuthor
作者在哪里
{
公共字符串名称{get;set;}
公共IBook[]书籍{get;set;}
}
教材:IBook
T:IBook在哪里
{
公共字符串名称{get;set;}
公共IAuthor[]作者{get;set;}
公共int pageScont{get;set;}
公共静态void Main(字符串[]args)
{
作者a=新作者();
}
}
有没有办法在main中创建这样的对象。编译器说接口和类的描述没有错误。请帮帮我。我个人认为,您可能在这里过度使用泛型,使其变得更复杂。
评估您的标准: 作者(一本->多本)书
书(一人->多人)作者 您可以通过使用以下类来实现这一点:
public class Book
{
public string Name { get; set; }
public Author[] Authors { get; set; }
public int PageCount { get; set; }
}
public class Author
{
public string Name { get; set; }
public Book[] Books { get; set; }
}
如果您这样做,您可以通过在父类中包含所有书籍和作者,并使用linq查询来确定书籍/作者与哪个对象相关,从而使您的生活更加轻松:
public class BookStore
{
public List<Book> Books { get; set; }
public List<Author> Authors { get; set; }
public Book GetBook(string name)
{
var query = Books.Where(b => b.Name.Equals(name));
if (query.Count() == 1)
return query.ElementAt(0);
else return null;
}
public Author GetAuthor(string name)
{
var query = Authors.Where(a => a.Name.Equals(name));
if (query.Count() == 1)
return query.ElementAt(0);
else return null;
}
}
公共类书店
{
公共列表书籍{get;set;}
公共列表作者{get;set;}
公共图书GetBook(字符串名称)
{
var query=Books.Where(b=>b.Name.Equals(Name));
if(query.Count()==1)
返回查询.ElementAt(0);
否则返回null;
}
公共作者GetAuthor(字符串名称)
{
var query=Authors.Where(a=>a.Name.Equals(Name));
if(query.Count()==1)
返回查询.ElementAt(0);
否则返回null;
}
}
就个人而言,我认为您可能在这里过度使用泛型,使其变得更为复杂。评估您的标准: 作者(一本->多本)书
书(一人->多人)作者 您可以通过使用以下类来实现这一点:
public class Book
{
public string Name { get; set; }
public Author[] Authors { get; set; }
public int PageCount { get; set; }
}
public class Author
{
public string Name { get; set; }
public Book[] Books { get; set; }
}
如果您这样做,您可以通过在父类中包含所有书籍和作者,并使用linq查询来确定书籍/作者与哪个对象相关,从而使您的生活更加轻松:
public class BookStore
{
public List<Book> Books { get; set; }
public List<Author> Authors { get; set; }
public Book GetBook(string name)
{
var query = Books.Where(b => b.Name.Equals(name));
if (query.Count() == 1)
return query.ElementAt(0);
else return null;
}
public Author GetAuthor(string name)
{
var query = Authors.Where(a => a.Name.Equals(name));
if (query.Count() == 1)
return query.ElementAt(0);
else return null;
}
}
公共类书店
{
公共列表书籍{get;set;}
公共列表作者{get;set;}
公共图书GetBook(字符串名称)
{
var query=Books.Where(b=>b.Name.Equals(Name));
if(query.Count()==1)
返回查询.ElementAt(0);
否则返回null;
}
公共作者GetAuthor(字符串名称)
{
var query=Authors.Where(a=>a.Name.Equals(Name));
if(query.Count()==1)
返回查询.ElementAt(0);
否则返回null;
}
}
如果不使用这样的泛型类,肯定会对您有好处,因为每次尝试初始化对象时,这都会让您头疼。至少为泛型类创建了一个默认实现,在这个实现中,不需要为泛型类指定作者类型。您肯定应该为您的应用程序实施某种策略,这取决于它的可伸缩性和易于维护的程度,@Nathangrad为您提供了一个很好的示例
我认为您的对象应该是这样的,您的书的作者不能直接更改,作者的书也不能直接更改:
public interface IBook
{
string Name { get; set; }
ICollection<IAuthor> GetAuthors();
void AddAuthor(IAuthor book);
int PagesCount { get; set; }
}
public interface IAuthor
{
string Name { get; set; }
ICollection<IBook> GetBooks();
void AddBook(IBook book);
}
public class Author : IAuthor
{
private ICollection<IBook> books;
public Author()
{
this.books = new HashSet<IBook>();
}
public Author(ICollection<IBook> books)
{
this.books = books;
}
public string Name { get; set; }
public void AddBook(IBook book)
{
this.books.Add(book);
}
public ICollection<IBook> GetBooks()
{
return this.books;
}
}
public class Book : IBook
{
private ICollection<IAuthor> authors;
public Book()
{
this.authors = new HashSet<IAuthor>();
}
public Book(ICollection<IAuthor> Authors)
{
this.authors = Authors;
}
public void AddAuthor(IAuthor author)
{
this.authors.Add(author);
}
public ICollection<IAuthor> GetAuthors()
{
return this.authors;
}
public string Name { get; set; }
public int PagesCount { get; set; }
}
公共接口IBook
{
字符串名称{get;set;}
i收集GetAuthors();
无效添加作者(作者书);
int pagesunt{get;set;}
}
公共接口作者
{
字符串名称{get;set;}
i收集GetBooks();
无效地址簿(IBook book);
}
公共类作者:IAuthor
{
私人收藏图书;
公共作者()
{
this.books=new HashSet();
}
公共作者(ICollection图书)
{
这本书=书;
}
公共字符串名称{get;set;}
公共无效地址簿(IBook book)
{
this.books.Add(book);
}
公共ICollection GetBooks()
{
归还这本书;
}
}
公共类书籍:IBook
{
私人收藏作者;
公共书籍()
{
this.authors=new HashSet();
}
公共图书(ICollection作者)
{
this.authors=作者;
}
public void AddAuthor(IAauthor author)
{
this.authors.Add(author);
}
公共ICollection GetAuthors()
{
将此文件返回给作者;
}
公共字符串名称{get;set;}
公共int pageScont{get;set;}
}
如果不使用这样的泛型类,肯定会对您有好处,因为每次尝试初始化对象时,这都会让您头疼。至少为泛型类创建了一个默认实现,在这个实现中,不需要为泛型类指定作者类型。您肯定应该为您的应用程序实施某种策略,这取决于它的可伸缩性和易于维护的程度,@Nathangrad为您提供了一个很好的示例
我认为您的对象应该是这样的,您的书的作者不能直接更改,作者的书也不能直接更改:
public interface IBook
{
string Name { get; set; }
ICollection<IAuthor> GetAuthors();
void AddAuthor(IAuthor book);
int PagesCount { get; set; }
}
public interface IAuthor
{
string Name { get; set; }
ICollection<IBook> GetBooks();
void AddBook(IBook book);
}
public class Author : IAuthor
{
private ICollection<IBook> books;
public Author()
{
this.books = new HashSet<IBook>();
}
public Author(ICollection<IBook> books)
{
this.books = books;
}
public string Name { get; set; }
public void AddBook(IBook book)
{
this.books.Add(book);
}
public ICollection<IBook> GetBooks()
{
return this.books;
}
}
public class Book : IBook
{
private ICollection<IAuthor> authors;
public Book()
{
this.authors = new HashSet<IAuthor>();
}
public Book(ICollection<IAuthor> Authors)
{
this.authors = Authors;
}
public void AddAuthor(IAuthor author)
{
this.authors.Add(author);
}
public ICollection<IAuthor> GetAuthors()
{
return this.authors;
}
public string Name { get; set; }
public int PagesCount { get; set; }
}
公共接口IBook
{
字符串名称{get;set;}
i收集GetAuthors();
无效添加作者(作者书);
int pagesunt{get;set;}
}
公共接口作者
{
字符串名称{get;set;}
i收集GetBooks();
无效地址簿(IBook book);
}
公共类作者:IAuthor
{
私人收藏图书;
公共作者()
{
this.books=new HashSet();
}
公共作者(ICollection图书)
{
这本书=书;
}
公共字符串名称{get;set;}
公共无效地址簿(IBook book)
{
this.books.Add(book);
}
公共ICollection GetBooks()
{
归还这本书;
}
}
公共类书籍:IBook
{
私人ICollectio