C#是参考吗?

时间:2014-06-04 17:35:26

标签: c#

在课程顶部,在字段成员中,教师教师的名称是什么;

我知道这允许我们访问Teacher类;它的属性和方法。我想知道它是否被称为引用或类的实例,所以我可以阅读更多内容以澄清我对它的理解。

感谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace P3_O_O_P
{
    public class Classroom
    {
        private int roomNo;
        private string className;
        Teacher teacher;
        public List<Student> stuList = new List<Student>();

        public Classroom(int newRoomNo, string newRoomName)
        {
            roomNo = newRoomNo;
            className = newRoomName;
        }

        public Classroom()
        {
        }

        public void addStudent(string studentName, int studentID)
        {
            stuList.Add(new Student(studentName, studentID));
        }

        public void addTeacher(string teacherName, int teacherID)
        {
            teacher = new Teacher(teacherName, teacherID);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\nTeacher name: " + teacherName + "\nTeacher ID: " + teacherID + "\nSystem updated!\n");
        }

        public void viewClass()
        {
            for (int i = 0; i < stuList.Count; i++)
            {
                Console.WriteLine(stuList[i].toString());
            }
            if (teacher != null)
            {
                Console.WriteLine(teacher.toString());
            }
        }

        public void removeTeacher()
        {
            teacher = null;
        }

        public string ClassName
        {
            get
            {
                return className;
            }
            set
            {
                className = value;
            }
        }

        public int ClassNumber
        {
            get
            {
                return roomNo;
            }
            set
            {
                roomNo = value;
            }
        }

        public void removeStudent(int id)
        {
            for (int i = 0; i < stuList.Count(); i++)
            {
                int ID = stuList[i].ID;
                if (id == ID)
                {
                    Console.WriteLine("\nStudent name: {0}\nStudent number: {1}\n\nStudent removed from classroom\n\nSystem updated!\n\n", stuList[i].Name, stuList[i].ID);

                    stuList.RemoveAt(i);
                }

            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

Teacher是该类的名称。 teacher是对教师班级实例的引用。

另一种思考方式:teacherTeacher类型的变量。

答案 1 :(得分:1)

它会创建teacher Teacher类型。通过创建对象,我们可以访问其公共成员。我们也说我们为一个类创建一个实例。

答案 2 :(得分:1)

teacherTeacher类型的成员变量,它是引用类型(不是类型)。实例teacher后,通过addTeacher()方法,它将引用Teacher类的特定实例。