10-2.cs

来自「java基础方面的一些实例代码」· CS 代码 · 共 108 行

CS
108
字号
//程序10-2
using System;

namespace School
{
	public class Student
	{ 
		protected string studName;
		public Student()
		{}
		public Student(string name)
		{
			studName=name;
			
		}
		public virtual string TheName
		{
			get{return studName;}
		}
		public virtual void PhyEdu()
		{
			Console.WriteLine("  In good health.");
		}
		public virtual void Commonweal()
		{
			Console.WriteLine("  Be of virtue.");
		}
		public virtual void GradePointAverage()
		{
			Console.WriteLine("  The GPA is very high.");
		} 
	
	}
	public class CompStudent:Student
	{ 
		double gpa;
		int Comnweal;
		double phy;
		public CompStudent()
		{}
		public CompStudent(string name, int c, double p,double g):base(name)
		{
			studName=name;
			Comnweal=c;
			phy=p;
			gpa=g;
		}
		public override string TheName
		{
			get{return studName;}
		}
		public override void Commonweal()
		{
			if(Comnweal>=40) Console.WriteLine("  Always helps others.");
		}
		public override void GradePointAverage()
		{
			if(gpa>=3.5)Console.WriteLine("  Excellent GPA: {0} ", gpa);
			
		} 
	
	}
	public class ArtStudent:Student
	{ 
		double gpa;
		int Comnweal;
		double phy;
		public ArtStudent()
		{}
		public ArtStudent(string name, int c, double p,double g):base(name)
		{
			studName=name;
			Comnweal=c;
			phy=p;
			gpa=g;
		}
		public override string TheName
		{
			get{return studName;}
		}
		public override void PhyEdu()
		{
			if(phy>=3.5)	Console.WriteLine("  Good at sports.");
		}
		public override void Commonweal()
		{
			if(Comnweal>=50) Console.WriteLine("  Full of artist's virtue.");
		}
	}
	public class Test
	{
		public static void Evaluate(Student stu)
		{
			Console.WriteLine("The student's name is "+stu.TheName);
			stu.Commonweal();
			stu.PhyEdu();
			stu.GradePointAverage();
		}
		public static void Main()
		{
			Student [] Students=new Student [2];
			Students[0]=new CompStudent("Li",50,3.8,3.7);
			Students[1]=new ArtStudent("Zhang",82,3.5,3.9);
			foreach(Student stud in Students )Evaluate(stud);
		}
	}
}

⌨️ 快捷键说明

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