paixu(t, stu); Console.Read(); }
public static void paixu(int t, score[] stu) {
score stud = new score(); if (stu.Length <= 0) return; for (int i = 0; i < t; i++) {
for (int j = 0; j < t - i - 1; j++) {
if (stu[j].Avg > stu[j + 1].Avg)
{ stud = stu[j + 1]; stu[j + 1] = stu[j]; stu[j] = stud; } } }
Console.WriteLine(\ 姓名 学号 C++ 英语 数学 平均成绩\); foreach (score x in stu) x.display(); } } class score {
private string name; private int ID; private double c; private double english; private double math; double avg; public score() { this.avg = 0; } public string Name
{ get { return name; } set { name = value; } } public int id
{ get { return ID; } set { ID = value; } } public double C
{ get { return c; } set { c = value; } } public double English
{ get { return english; } set { english = value; } } public double Math
{ get { return math; } set { math = value; } } public double Avg
{ get { return avg; } set { avg = value; } } public void init()
{
Console.Write(\学号:\);
this.ID = Convert.ToInt32(Console.ReadLine()); Console.Write(\姓名:\);
this.name = Convert.ToString(Console.ReadLine()); Console.Out.Write(\);
this.c = Convert.ToDouble(Console.ReadLine()); Console.Write(\英语:\);
this.english = Convert.ToDouble(Console.ReadLine()); Console.Write(\数学:\);
this.math = Convert.ToDouble(Console.ReadLine()); }
public void AVG() {
this.avg = (this.math + this.english + this.c) / 3; }
public void display() {
Console.WriteLine(\, name, ID, c, english, math, avg); } } }
第四章 面向对象编程进阶习题
1、 举现实世界中继承的一个例子,用类的层次图表示出来
交通工具
陆上交通工具 海上交通工具 空中交通工具
飞机 自行车 轮船 汽车
2、 什么是抽象类和密封类,它们有什么不同
一个包含一个或多个纯虚函数的类叫抽象类,抽象类不能被实例化,进一步 一个抽象类只能通过接口和作为其它类的基类使用,使用abstract修饰符,若类中的方法或属性为abstract,类必须用abstract修饰。只能用作基类,抽象方法没有实现代码(无大括号!) 抽象类和非抽象类的区别:
(1)抽象类不能直接被实例化,只能在扩充类中通过继承使用 (2)抽象类可以包含抽象成员,非抽象类不能包含抽象成员
当从抽象类派生非抽象类时,非抽象类必须实现抽象类的所有(必须是所有!)抽象成员,当然,如果继承抽象类的也是抽象类就不必实现其抽象成员,由最后一个非抽象类实现。 密封类是不能被其他类继承的类,用sealed关键字声明。sealed关键字也可以限制基类中的方法,防止被扩充类重写,若类中的方法是sealed,该类不是必须用sealed来修饰的。带有sealed修饰符的方法称为密封方法,sealed方法必须和override关键字一起使用。 一般情况下,只有在某个方法重写了基类中同名的方法,但又不希望该方法所在的类的扩充类重写该方法,就可以在该方法前使用修饰符sealed。 3、 分别在什么情况下使用隐式数值转换和显示数值转换
4、 什么是程序集,它的作用有哪些
4.程序运行结果__。 using System; public class Test {
public void change1(string s) {
s = s + \; }
public void change2(ref string s) {
s = s + \; }
public void change3(string s1, out string s2) {
s1 = s1 + \; s2 = s1; } }
public class Exe8 {
public static void Main()
{string s1,s2;
s1-\;
Text t1=new Text (); t1.change1(s1);
Console.WriteLine (\,s1 ); t1.change2(ref s1);
Console.WriteLine (\,s1 ); t1.change3(s1,out s2);
Console.WriteLine (\,s1 ); Console.WriteLine (\,s2 ); Console .Read(); } }
5.程序运行结果__。 using System; public class Test {
public void change(string s) {
s = s + \; }
public void change2(ref string s) {
s = s + \; }
public void change3(string s1, out string s2) {
s1 = s1 + \; s2 = s1; } }
public class Exe9 {
public static void Main()
{string s1,s2; s1-\;
Text t1=new Text (); t1.change(s1);
Console.WriteLine (\,s1 ); t1.change(ref s1);
Console.WriteLine (\,s1 ); t1.change(s1,out s2);
Console.WriteLine(\, s1,s2 );
相关推荐: