代码参考:
using System; namespace InterdependDemo { class SchoolInfo { private StudentInfo studentInfo; private TeacherInfo teacherInfo; public string SchoolName = "西安航天技师学院"; public StudentInfo StudentInfo { set { studentInfo = value; } } public TeacherInfo TeacherInfo { set { teacherInfo = value; } } public void ShowInfo() { studentInfo.ShowInfo(this); teacherInfo.ShowInfo(this); } } class StudentInfo { private string info; public string Info { set { info = value; } } public void ShowInfo(SchoolInfo s) { Console.WriteLine(s.SchoolName + " " + info); } } class TeacherInfo { private string info; public string Info { set { info = value; } } public void ShowInfo(SchoolInfo s) { Console.WriteLine(s.SchoolName + " " + info); } } class Program { static void Main(string[] args) { StudentInfo s = new StudentInfo { Info = "学生信息" }; TeacherInfo t = new TeacherInfo { Info = "教师信息" }; SchoolInfo sch = new SchoolInfo { StudentInfo = s, TeacherInfo = t }; sch.ShowInfo(); } } }
标签:info,TeacherInfo,ShowInfo,演示,string,关联,StudentInfo,双向,public From: https://www.cnblogs.com/exesoft/p/16720509.html