C# 如何将接口用作字段? 名称空间TestOOP { 使用制度; 使用System.Collections.Generic; 使用System.Linq; 内部密封班学生 { 私有字符串名称; } 内部密封班课程 { 私立学院学生; 公共信息学院学生 { 获取{返回this.students;} 设置{this.students=students;} } } 班级计划 { 静态void Main() { var课程=新课程(); course.Students.Add(newstudent()); Console.WriteLine(course.Students.Count()); } } }
这是我的密码。当运行它时,我得到的对象没有设置为我试图将学生添加到课程的行中的对象实例。我需要帮助来解释如何将接口用作字段。对于集合属性,在构造过程中初始化它们,并通过C# 如何将接口用作字段? 名称空间TestOOP { 使用制度; 使用System.Collections.Generic; 使用System.Linq; 内部密封班学生 { 私有字符串名称; } 内部密封班课程 { 私立学院学生; 公共信息学院学生 { 获取{返回this.students;} 设置{this.students=students;} } } 班级计划 { 静态void Main() { var课程=新课程(); course.Students.Add(newstudent()); Console.WriteLine(course.Students.Count()); } } },c#,C#,这是我的密码。当运行它时,我得到的对象没有设置为我试图将学生添加到课程的行中的对象实例。我需要帮助来解释如何将接口用作字段。对于集合属性,在构造过程中初始化它们,并通过只读getter公开它们是一种很好的做法: namespace TestOOP { using System; using System.Collections.Generic; using System.Linq; internal sealed class Student {
只读
getter公开它们是一种很好的做法:
namespace TestOOP
{
using System;
using System.Collections.Generic;
using System.Linq;
internal sealed class Student
{
private string name;
}
internal sealed class Course
{
private ICollection<Student> students;
public ICollection<Student> Students
{
get { return this.students; }
set { this.students = Students; }
}
}
class Program
{
static void Main()
{
var course = new Course();
course.Students.Add(new Student());
Console.WriteLine(course.Students.Count());
}
}
}
对于集合属性,最好在构造过程中初始化它们,并通过
readonly
getter:
namespace TestOOP
{
using System;
using System.Collections.Generic;
using System.Linq;
internal sealed class Student
{
private string name;
}
internal sealed class Course
{
private ICollection<Student> students;
public ICollection<Student> Students
{
get { return this.students; }
set { this.students = Students; }
}
}
class Program
{
static void Main()
{
var course = new Course();
course.Students.Add(new Student());
Console.WriteLine(course.Students.Count());
}
}
}
问题不在于接口,而在于没有为变量赋值
internal sealed class Course
{
public ICollection<Student> Students { get; } = new List<Student>();
}
私立i学院学生;
这将解决它:
private ICollection<Student> students;
private ICollection students=new List();
问题不在于接口,而在于您没有为变量赋值
internal sealed class Course
{
public ICollection<Student> Students { get; } = new List<Student>();
}
私立i学院学生;
这将解决它:
private ICollection<Student> students;
private ICollection students=new List();
您需要创建属性Students的实际实例,例如使用类课程的构造函数:
private ICollection<Student> students = new List<Student>();
内部密封类课程
{
私立学院学生;
公共信息学院学生
{
获取{返回this.students;}
设置{this.students=students;}
}
公共课程()
{
this.Students=newlist();
}
}
接口必须由实际类实现。您需要创建属性Students的实际实例,例如使用类课程的构造函数:
private ICollection<Student> students = new List<Student>();
内部密封类课程
{
私立学院学生;
公共信息学院学生
{
获取{返回this.students;}
设置{this.students=students;}
}
公共课程()
{
this.Students=newlist();
}
}
接口必须由实际类实现。特别是在这种情况下,您没有将值设置为
Students
属性(或backfieldStudents
),因此它是null
,并且Students.Count()
将抛出。特别是在这种情况下,您没有将值设置为Students
属性(或者将students
),因此它是null
和students.Count()
将抛出。如果我不想使用列表的所有功能,只想添加/删除/计数怎么办?我是否必须创建一些自定义类来继承ICollection?在这种情况下,我建议使用组合而不是继承,即将学生
设为私有字段,完全删除公共属性,并在如果您继承自ICollection
,则必须自己实现该接口中的所有方法,但这可能是错误的方法(除非您想要一个自定义的StudentsCollection
,它将在集合更改时触发事件,但即使如此,课程也不仅仅是一个“学生集合”)。如果我不想使用列表的所有功能,只想添加/删除/计数怎么办?我是否必须创建一些自定义类来继承ICollection?在这种情况下,我建议使用组合而不是继承,也就是说,将学生
设为私有字段,完全删除公共属性,并仅公开那些公共属性您需要的方法(将在私有列表上操作)。如果您继承自ICollection
,则必须自己实现该接口中的所有方法,但这可能是错误的方法(除非您想要一个自定义的StudentsCollection
,它将在集合更改时触发事件,但即使如此,课程也不仅仅是一个“学生集合”)。