9-1.cs

来自「这是学习java基础的好的源代码的学习资料」· CS 代码 · 共 60 行

CS
60
字号
//程序9-1
using System;
namespace School
{
	public class Student 
	{
		protected string studName;
		protected	int studID;
		public Student()
		{  }
		public Student(string str, int id)
		{
			studName=str;
			studID=id;
		}
		public void PrintBase()
		{
			Console.WriteLine("在基类: Student.");
		}
		public void FindInfo(int id)
		{
			if(id==studID)
				Console.WriteLine ("{0}is a student. ", studName);
		}
	} 
	public class CollegeStudent :Student
	{        
		string department; 
		new string  studName; 
		public CollegeStudent(string deptmnt,string str, int id)
		{
			department=deptmnt;
			studName=str;
			studID=id;
		}
		public void PrintDerived()
		{
			Console.WriteLine("在派生类: CollegeStudent.");
		}
		public  new void FindInfo(int id)
		{
			if(id==studID)
				Console.WriteLine ("{0}is a student in the {1} Department. ", studName,department);
		}
	}
	public class Test
	{
		public static void Main()
		{
			Student AStudent = new Student("李四",0518277);
			CollegeStudent CollegeStudent=new CollegeStudent("计算机","张三", 0518288);
			AStudent.FindInfo(0518277);
			AStudent.PrintBase();
			CollegeStudent.FindInfo(0518288);
			CollegeStudent.PrintBase();
			CollegeStudent.PrintDerived();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?