继承和接口设计

一.选择题

1.在C#中,一个类【    】。

A) 可以继承多个类                        B) 可以实现多个接口

C) 在一个程序中只能有一个子类                   D) 只能实现一个接口

2.【    】关键字用于在C#中从派生类中访问基类的成员。

A) new                        B) super                       C) this                         D) base

3.在定义类时,若希望类的某个方法能够在派生类中进一步改进,以满足派生类的不同需要,则应将该方法声明为【    】。

A) new                        B) public                      C) virtual                      D) override

4.在C#中,在派生类中对基类的虚方法进行重写,要求在派生类的声明中使用【    】。

A) new                        B) public               C) virtual                      D) override

5.已知类B是由类A继承而来,类A中有一个名为M的非虚方法,现在希望在类B中也定义一个名为M的方法,且两方法的参数个数和类型完全相同,若希望编译时不出现警告信息,则在类B中声明该方法时,应使用【    】关键字。

A) static                      B) new                        C) override                   D) virtual

6.假设类B继承了类A,下列说法错误的是【    】。

A) 类B中的成员可以访问类A中的公有成员

B) 类B中的成员可以访问类A中的保护成员

C) B中的成员可以访问类A中的私有成员

D) 类B中的成员可以访问类A中的静态成员

7.关于多态,下列说法正确的是【    】。

A) 多态实际上就是重载,它们本质上是以一样的

B) 多态可以分为编译时多态和运行时多态。前者的特点是在编译时就能确定要调用成员方法的哪个版本,后者则是在程序运行时才能确定要调用成员方法的哪个版本。

C) 编译时多态是在程序运行时才能确定要调用成员方法的哪个版本,而运行时多态在编译时就能确定要调用成员方法的哪个版本。

D) 多态和重载是两个完全不同的概念,前者是通过定义虚方法和重写虚方法来实现,后者是通过对同一函数名编写多个不同的实现代码来实现。

8.下列关于接口的说法,正确的是【    】。

A) 接口中定义的方法都必须是虚方法

B) 接口中定义的方法可以编写其实现代码

C) 继承接口的类可提供被继承接口中部分成员的实现代码

D) 接口中的所有方法都必须在其派生类中得到实现。

9.下列关于虚方法的描述中,正确的是【    】。

A) 虚方法能在程序执行时动态确定要调用的方法,因此比非虚方法更灵活

B) 在定义虚方法时,基类和派生类的方法定义语句中都要带上virtual修饰符

C) 在重写基类的虚方法时,为消除隐藏基类成员的警告,需要带上new修饰符

D) 在重写基类的虚方法时,需要同时带上virtual和override修饰符

10.在C#中,以下关于抽象类的叙述中错误的是【    】。

A) 抽象类可以包含抽象方法                         B) 含有抽象方法的类一定是抽象类

C) 抽象类不能被实例化                                D) 抽象类可以是密封类

11.在C#中,以下关于抽象类和接口的叙述中正确的是【    】。

A) 抽象类可以包含非抽象方法,而接口不包含任何方法的实现

B) 抽象类可以被实例化,而接口不能被实例化

C) 抽象类不能被实例化,而接口可以被实例化

D) 抽象类能够被继承,而接口不能被继承

12.在C#中,以下关于抽象类和接口的叙述中正确的是【    】。

A) 在抽象类中,所有的方法都是抽象方法

B) 继承自抽象类的子类必须实现其父类(抽象类)中的所有抽象方法

C) 在接口的定义中可以有方法实现,在抽象类的定义中不能有方法实现

D) 一个类可以从多个接口继承,也可以从多个抽象类继承

13.以下类和接口的区别中正确的是【    】。

A) 类可以继承而接口不可以                         B) 类不可以继承而接口可以

C) 类可以多继承而接口不可以                      D) 类不可以多继承而接口可以

14.下列方法定义中,【    】是抽象方法。

A) static void func(){  }                                  B) virtual void func(){  }                  

C) abstract void func(){  }                               D) overridel void func(){  }

15.下列关于接口的说法,错误的是【    】。

A) 一个类可以有多个基类和多个基接口               B) 抽象类和接口都不能被实例化

C) 抽象类自身可以定义成员,而接口不可以        D) 类不可以多重继承,而接口可以。

16.已知类Base、Derived的定义如下:

class Base

{   public void Hello()

{   System.Console.Write(“Hello in Base!”);    }           

}

class Derived: Base

{   public new void Hello()

{   System.Console.Write(“Hello in Derived!”);       }

}

则下列语句段在控制台中的输出结果是【    】。

       Derived x=new Derived();

       x.Hello();

A) Hello in Base!                                             B) Hello in Base! Hello in Derived!

C) Hello in Derived!                                         D) Hello in Derived!Hello in Base!

17.关于继承,下列说法正确的是【    】。

A) 子类将继承父类所有的成员 B) 子类将继承父类的非私有成员

C) 子类只继承父类public成员 D) 子类只继承父类的方法,而不继承属性

18.下列是抽象类的声明的是【    】。

A)  abstract class figure{    }    B)   class  abstract figure{    }                     

C)  seald class figure{    }        D)  static class figure{        }

19.abstract修饰的方法是抽象方法,抽象方法只有声明而无主体,且【    】。

A)  只能存在于抽象类中          B)   可以在任何类中                       

C)  只能在接口中                    D)  只能在密封类中

20.重写一个基类的虚方法时,应与原虚方法的方法名相同,并且【    】。

A)  不能改变方法的参数类型、个数和返回值

B)   可以改变方法的参数类型、个数和返回值                          

C)  可以改变方法的返回值

D)  可以改变方法的参数类型和个数

21.下面有关继承的说法中,正确的是【    】。

A)  A类和B类均有C类需要的成员,因此可以从A类和B类共同派生出C类                

B)   在派生新类时,可以指明是公有派生、私有派生或保护派生                          

C)  派生类可以继承基类中的成员,同时也继承基类的父类中的成员

D)  在派生类中,不能添加新的成员,只能继承基类的成员

22.为了防止从所修饰的类派生出其他类,可以将此类定义为【    】。

A)  抽象类                 B) 密封类                   C)  静态类          D) 私有类

23.若想从派生类中访问基类的成员,可以使用【    】。

A)  this关键字           B) me关键字               C)  base关键字 D) override关键字

24.下面有关派生类的描述中,不正确的是【    】。

A)  派生类可以继承基类的构造函数      B)   派生类可以隐藏和重载基类的成员

C)  派生类不能访问基类的私有成员      D)  派生类只能有一个直接基类

25.C#中的多态不能通过【    】实现。

A)  接口                     B) 抽象类                   C)  密封类   D) 虚方法

26.下列程序错误的原因是【    】。

              sealed  class  SealedClass{        }

              class  Derived:SealedClass{        }

A)  SealedClass类不能被继承  B)   没有Main()入口                

C)  SealedClass没有被实例化   D)  以上皆是

27.下面关于接口的叙述中,不正确的是【    】。

A)  接口只是由方法、属性、索引器或事件组成的框架,并没有描述任何对象的实例代码  

B)   接口的所有成员都被自动定义为公有的,不可使用访问修饰符来定义接口成员            

C)  类可以通过在类型定义语句中包括冒号和接口名来表明它正在实现接口         

D)  一个类只能实现一个接口

28.接口可以包含一个或多个成员,以下选项中【    】不能包含在接口中。

A)  方法                     B) 属性                       C)  事件       D) 常量

29.下列叙述中,正确的是【     】。

A)  接口中可以有虚方法                        B) 一个类可以实现多个接口                         

C)  接口能被实例化          D) 接口中可以包含已实现的方法

30. 关于虚方法实现多态,下列说法错误的是【    】。

A) 定义虚方法使用关键字virtual              B) 关键字virtual可以与override一起使用

C) 虚方法是实现多态的一种应用形式         D) 派生类是实现多态的一种应用形式

31. 以下类MyClass的定义中,【    】是合法的抽象类。

A) abstract  class  MyClass {  public  abstract  int  getCount();  }

B) abstract  class  MyClass {  public  virtual   abstract  int  getCount();  }

C) abstract  class  MyClass {  private  abstract  int  getCount();  }

D) abstract  class  MyClass {  public  static  abstract  int  getCount();   }

32. 分析下列程序中类MyClass的定义

class BaseClass

{   public int i;     }

class MyClass:BaseClass

{   public new int i;     }

则下列语句在Console 上的输出为【    】。

MyClass y = new MyClass();

BaseClass x = y;

x.i = 100;

Console.WriteLine("{0}, {1}",x.i,y.i);

A) 0, 0                         B) 100, 100                  C) 0, 100                      D) 100, 0

33. 在C#程序中定义如下IPlay接口,实现此接口代码正确的是【    】。

interface IPlay

{   void Play();

     void Show(); 

}

A)  class  Teacher :Iplay

{     void  Play(){       //省略部分代码 }

void  Show0{      //省略部分代码}                       

B)  class  Teacher :Iplay

{     public  string  Play(){      //省略部分代码 }

public  void  Show0{        //省略部分代码}         

C)  class  Teacher :Iplay

{     public  void  Play(){        //省略部分代码 }

public  void  Show0{        //省略部分代码}                       

D)  class  Teacher :Iplay

{     public  void  Play();

public  void  Show0{        //省略部分代码}  

34. 以下泛型集合声明中正确的是【    】。

A) List<int> f=new List<int>();                   B) List<int> f=new List (); 

C) List  f=new List ();                                 D) List<int> f=new List<int>; 

34. 以下关于泛型的叙述中错误的是【    】。

A)  泛型是通过参数化类型来实现在同一份代码上操作多种数据类型     

B)   泛型编程是一种编程范式,其特点是参数化类型         

C)  泛型类型和普通类型的区别在于泛型类型与一组类型参数或类型变量关联            

D)  以上都不对

二.填空题

1.在C#中有两种多态性:编译时的多态性和运行时的多态性。编译时的多态性是通过【虚方法和重写虚方法 】实现的,运行时的多态性是通过继承和【重载/隐藏 】来实现的。

2.在声明类时,在类名前使用【abstract】修饰符,则声明的类只能作为其他类的基类,不能被实例化。

3.在声明类时,在类名前使用【 sealed   】修饰符,则声明的类不能作为其他类的基类,不能再派生新的类。

4. 下列程序的运行结果是【AB 】。考察类的继承

class A

{   public A( )

     {     Console.WriteLine("A");      }

}

class B:A

{   public B()

     {     Console.WriteLine("B");      }

}

class Program

{   public static void Main()

     {     B b = new B();

            Console.ReadLine();

     }

}

5. 分析下列程序的运行结果是【   】。考察类的继承中base的使用

              class A

              {     public  int  x=100;            }

              class B:A

              {     new public int x;

public  B(int y, int z){ x=y;        base.x=z;  }        

public int getx1(){ return     base.x; }

public int getx2(){ return     x; }

}

          class Program

       {     public static void Main()

           {     B b = new B(3, 6);

               int n = b.getx1();

               int m = b.getx2();

               Console.WriteLine("m={0},n={1}", m, n);

          }

          }

m=3,n=6

6. 分析下列程序的运行结果是【   】。考察类的继承(隐藏和重写)

abstract class BaseClass

{   public void MethodA()

{   Console.WriteLine("BaseClass.MethodA");        }

  public virtual void MethodB()

  {     Console.WriteLine("BaseClass.MethodB");        }

}

class Class1 : BaseClass

{  new  public  void MethodA()

          {     Console.WriteLine("Class1.MethodA");             }

  public override void MethodB()

{   Console.WriteLine("Class1.MethodB"); }

}

class Class2 : Class1

{   new public void MethodB()

    { Console.WriteLine("Class2.MethodB"); }

}

class MainClass

{   public static void Main(string[] args)

  {     Class2 a = new Class2();

        a.MethodA();        a.MethodB();

      Console.Read();

          }

}

Class1.MethodA

Class2.MethodB

7.下列程序的运行结果是【AA  CC  DD  DD    】。考察类的继承(隐藏和重写)

class A

{  public void F1(){       Console.Write("AA  "); }

    public virtual void F2()     {     Console.Write("BB  "); }

}

class B : A

{ new public void F1(){   Console.Write("CC  "); }

public override void F2(){ Console.Write("DD  "); }

}

class Test

{  static void Main()

  {     B b = new B();

     A a = b;

      a.F1();    b.F1();    a.F2();    b.F2();

         Console.Read();

}

}

8. 阅读下列程序,程序的输出结果是【    】。考察类的继承

public class  A

{    private  int  speed =10;

     public  int  Speed    

     {   get {  return speed;   }

         set {  speed = value;  Console.WriteLine("禁止驶入!");  }

      }

}

public class  B:A

{    public  B()

     {   if(this.Speed>=20)  Console.Write ("机动车");

         else  Console.Write ("非机动车");

      }

}

class Program                                      

{   public  static void Main()

     {   B b=new B();

               b.Speed = 30;

      }

}

非机动车禁止驶入!

9. 阅读下列程序,程序的输出结果是【    】。考察类的继承

public class Person

{     private int age = 0;

     public int Age

     {     get

        {     return age;     }

         set

         {     if(value>=18) Console.Write("成年人");

                else Console.Write("未成年人");

             age = value;

           }

      }

}

public class People : Person

{     public People(int age)

      {    Age = age;

          Console.WriteLine("不得入内");

     }

}

class Program

{     public static void Main()

     {     People  b = new People (25);

          Console .WriteLine("你的年龄是:{0}",b.Age);

         Console.Read();

     }

}

成年人不得入内

你的年龄是:25

10. 阅读下列程序,程序的输出结果是【    】。考察接口的映射

interface IA

{   void Hello(); }

class Base:IA

{     public void Hello()

     {    Console.WriteLine("Hello in Base!");        }

}

class Derived:Base 

{     new public void Hello()

     {     Console.WriteLine("Hello in Derived!");     }

}

class Program

{    public static void Main()

{   Base b=new Base();           

Derived d=new Derived ();

          IA x;

         b.Hello();        d.Hello();      

x=b;        x.Hello();

x=d;        x.Hello();

          Console.Read();

}

}

Hello in Base!

Hello in Derived!

Hello in Base!

Hello in Base!

11. 阅读下列程序,程序的输出结果是【    】。考察接口的映射

interface IA

{   void Hello(); }

class Base:IA

{     public void Hello()

     {    Console.WriteLine("Hello in Base!");        }

}

class Derived:IA 

{     new public void Hello()

     {     Console.WriteLine("Hello in Derived!");     }

}

class Program

{    public static void Main()

{   Base b=new Base();           

Derived d=new Derived ();

          IA x;

x=b;    x.Hello();

x=d;    x.Hello();

          Console.Read();

}

}

Hello in Base!

Hello in Derived!

12. 阅读下列程序,程序的输出结果是【    】。考察接口的映射

interface Ia                       //接口Ia声明

    {   double fun1();                 //接口成员声明

        int fun2();                     //接口成员声明

    }

    class A                          //声明基类A

    {   public int fun2()           //隐式实现接口成员fun2

        {  return 2;    }

    }

    class B : A, Ia                   //类B从基类A和接口继承

    {   double x;

        public B(double y)          //构造函数

        {  x = y;       }

        public double fun1()         //隐式实现接口成员fun1

        {  return x;    }

    }

    class Program

    {   static void Main(string[] args)

        {   B b = new B(2.5);

            Console.WriteLine("{0} ", b.fun1());

            Console.WriteLine("{0} ", b.fun2());

            Console.Read();

        }

    }

2.5

2

12. 阅读下列程序,程序的输出结果是【    】。考察类的实现与泛型

class Stack<T>                              //声明栈泛型类

    {   int maxsize;                               //栈中元素最多个数

        T[] data;                                    //存放栈中T类型的元素

        int top;                                      //栈顶指针

        public  Stack()                          //构造函数

        {     maxsize = 10;   data = new T[maxsize];

               top = -1;

        }

        public  Stack(int n)                    //重载构造函数

        {     maxsize = n;     data = new T[maxsize];

               top = -1;

        }

        public bool StackEmpty()            //判断栈空方法

        {   return top == -1; }

        public bool Push(T e)               //元素e进栈方法

        {    if (top == maxsize - 1)          return false;

            top++;    data[top] = e;

            return true;

        }

        public bool Pop(ref  T e)            //元素出栈方法

        {    if (top == -1)         return false;

            e = data[top];        top--;

            return true;

        }

    }

    class Program

    {   static void Main(string[] args)

        {    int e = 0;

            Stack<int> s = new Stack<int>();             //定义整数栈

            s.Push(1);      s.Push(3);     s.Push(2);

            Console.Write("整数栈出栈次序:");

            while (!s.StackEmpty())                        //栈不空时出栈元素

            {      s.Pop(ref e);     Console.Write("{0}  ", e);     }

            Console.WriteLine();

            string e1 = "";

            Stack<string> s1 = new Stack<string>();     //定义字符串栈

            s1.Push("Mary");          s1.Push("John");         s1.Push("Simth");

            Console.Write("字符串栈出栈次序:");

            while (!s1.StackEmpty())                           //栈不空时出栈元素

            { s1.Pop(ref e1); Console.Write("{0} ", e1); }

            Console.ReadLine();

           }

       }

整数栈出栈次序:2  3  1

字符串栈出栈次序:Simth John Mary

三.改错题

(说明:下列程序中部分下划线的代码有错误,请将有错误的部分改正过来,并说明原因)

1.         class A

              {     public  A(int x){ }        }

              class B:A

              {     public  B(int x){ }        }

答:“public B(int x){ }”应改为“public B(int x):base(x){ }”。因为基类A中没有无参数的构造函数。

2.         class A

              {     void A(){ }                  

                     private A(int x){ }

                     private A(int y){ }        

              }

答:“void A(){ }”应改为“A(){}”,因为构造函数不能带任何返回类型修饰符。“private A(int x){ }”“private A(int y){ }”实际上是同一个构造函数,应删除其中一个。

3.         interface I

            {     int x;

                   public void f(int x);

                   void g(int x);

                   int h(int x){ return 1; }

            }

class A:I

              {     public void f(int x){  }

                     public int h(int x){ }

              }

答:“int x;”应予以删除,因为接口中不允许存在成员变量。“public void f(int x);”中的修饰符“public”应予以删除,因为接口的方法前不允许存在任何修饰符;“int h(int x){ return 1; }” 应改为“int h(int x),因为不能在接口声明的函数中编写实现代码;应该在类A中添加函数“void g(int x);”的实现代码。

4.         class A{   }

              class B{   }

              class C: A, B  {            }

答:必须将“class C:A,B”中的“A”“B”其中之一去掉,因为C#中不允许类进行多重继承。

5.         class A

              {     protected static void f(){  }

protected void g(){  }

              }

              class B:A

              {     new public static void f() {  }

                     new public void g(){  }

public void h()

{     base.f();         base.g();        f();        g();         }

}

答:“base.f();”应改为“A.f();”,原因是类A中的方法“f()”是静态方法,它不能被实例化,只能通过类名来访问。

四.问答题

1.什么是类的继承?怎样定义派生类?

2.简述创建派生类时,构造函数的调用。

3.怎样定义基类虚方法,并在派生类中重写基类虚方法?

4.抽象方法和虚方法有什么异同?

5.什么是抽象类?它有什么特点?它和接口有何异同?

6.什么是方法重载?重载的方法有何特点?

五.编程题

1. 编写一个控制台应用程序项目,实现学生和教师数据输入和显示功能。学生类Student有编号、姓名、班号和成绩等字段,教师类有编号、姓名、职称和部门等字段。要求将编号、姓名输入和显示设计成一个类Person,并作为Student和Teacher的基类。需用相关数据进行测试。

    public class Person                             //人类

    {   private int no;                             //编号

        private string name;                        //姓名

        public void input()

        {   Console.Write("  编号:");        no = int.Parse(Console.ReadLine());

            Console.Write("  姓名:");        name = Console.ReadLine();

        }

        public void disp()

        {   Console.WriteLine("  编号:{0}",no);

            Console.WriteLine("  姓名:{0}",name);

        }

    }

    public class Student : Person                   //学生类

    {   private string sclass;                      //班号

        private int degree;                              //成绩

        public void input()

        {   base.input();

            Console.Write("  班号:");        sclass = Console.ReadLine();

            Console.Write("  成绩:");         degree = int.Parse(Console.ReadLine());

        }

        new public void disp()

        {   base.disp();

            Console.WriteLine("  班号:{0}",sclass);

            Console.WriteLine("  成绩:{0}",degree);

        }

    }

    public class Teacher : Person                   //教师类

    {   private string prof;                       //职称

        private string depart;                    //部门

        public void input()

        {   base.input();

            Console.Write("  职称:");       prof = Console.ReadLine();

            Console.Write("  部门:");       depart = Console.ReadLine();

        }

        new public void disp()

        {   base.disp();

            Console.WriteLine("  职称:{0}", prof);

            Console.WriteLine("  部门:{0}", depart);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {   Student s1 = new Student();

            Teacher t1 = new Teacher();

            Console.WriteLine("输入一个学生数据:");       s1.input();

            Console.WriteLine("输入一个教师数据:");       t1.input();

            Console.WriteLine("显示一个学生数据:");       s1.disp();

            Console.WriteLine("显示一个教师数据:");       t1.disp();

        }

    }

2. 设计一个控制台应用程序项目b,设计一个普通职工类Employee,其工资为基本工资(1000)加上工龄工资(每年增加30元)。从Employee类派生出一个本科生类UEmployee,其工资为普通职工算法的1.5倍。并用相关数据进行测试,测试效果图如下图所示。

控制台应用程序项目b的执行结果

public class Employee                            //普通职工类

{  

private double bsalary= 1000;                 //基本工资

        private double psalary;                //实际工资

        private int n;                        //工作年数

        public int pn

        {   get { return n;       }

            set { n = value;     }

        }

        public double compsalary()              //计算普通员工工资

        {   Console.Write("工作年数:");

            pn = int.Parse(Console.ReadLine());

            psalary = bsalary+30*pn;

            return psalary;

        }

    }

    public class UEmployee : Employee         //本科生职工类

    {   new public double compsalary()

        {   return 1.5 * base.compsalary();     }

    }

    class Program

    {  static void Main(string[] args)     

        {   Console.WriteLine("普通职工:");

            Employee emp1 = new Employee();

            Console.WriteLine("该普通职工工资:{0}", emp1.compsalary());

            Console.WriteLine("本科生职工:");

            UEmployee emp2 = new UEmployee();

            Console.WriteLine("该本科生职工工资:{0}", emp2.compsalary());

        }

    }

说明:本题需采用继承方法,用虚函数等均可。

3.定义一个Shape抽象类,在该类中定义两个抽象方法GetArea和GetPerim。然后以Shape抽象类作为基类派生出Rectangle和Circle类,在这两个类中分别对GetArea和GetPerim方法进行重写,实现求特定形状的面积和周长。

抽象类方法实现

public  abstract class Shape

    {   public abstract double GetArea();

        public abstract double GetPerim();       

    }

    class Rectangle : Shape                                     //派生出来的Rectangle

    {   private double Width;                                //矩形的宽度

        private  double Length;                           //矩形的长度

        public Rectangle(double a, double b)        //构造函数

        {   Width = a;      Length = b;     }

        public override double GetArea()             //重载的求面积方法

        {   return (Width * Length);        }

        public override double GetPerim()            //重载的求周长方法

        {   return (2 * (Length + Width));    }

    }   

    class Circle : Shape                                        //派生出的Circle

    {   private  double Radius;

        public Circle(double r)                            //构造函数

        {   Radius = r;         }

        public override double GetArea()             //重载的求面积方法

        {    return (Math.PI * Radius * Radius);    }

        public override double GetPerim()            //重载的求周长方法

        {   return (2 * Math.PI * Radius);        }

    }

接口方法实现

public interface Shape

    {   double GetArea();

        double GetPerim();

    }

    class Rectangle : Shape                                  //派生出来的Rectangle

    {   public double Width;                              //矩形的宽度

        public double Length;                             //矩形的长度

        public Rectangle(double a, double b)           //构造函数

        {   Width = a;      Length = b;    }

        public  double GetArea()                    //重载的求面积方法

        {   return (Width * Length);         }

        public  double GetPerim()                  //重载的求周长方法

        {   return (2 * (Length + Width));    }

    }   

    class Circle : Shape                                     //派生出的Circle

    {   public double Radius;

        public Circle(double r)                         //构造函数

        {    Radius = r;        }

        public  double GetArea()                    //重载的求面积方法

        {    return (Math.PI * Radius * Radius);   }

        public  double GetPerim()                  //重载的求周长方法

        {    return (2 * Math.PI * Radius);           }

    }

4.设计一个抽象类Calculate(计算),该类包含OptA、OptB、OptC三个double类型的字段,包含一个带有两个double类型参数的构造函数(用于给字段OptA、OptB赋值)和一个名为SqrtForSum的抽象方法,该方法带有三个double类型参数、返回值类型为double型。再设计一个继承于Calculate的派生类Cal,该类包含一个带有三个double类型参数的构造函数,并重写SqrtForSum方法用于计算三数和的平方根。

public abstract class Calculate

    {   protected double OptA, OptB, OptC;

        public Calculate(double a, double b)

        {   OptA = a;     OptB = b;      }

         public abstract double SqrtForSum(double a,double b,double c);

    }   

    class Cal:Calculate

    {   public Cal(double a, double b, double c): base(a, b)

        {     OptC = c;      }

        public override  double SqrtForSum(double a, double b, double c)

        {  double sum;

            sum = a + b + c;

            return  Math .Sqrt (sum );

        }

    }

class Program

    {  static void Main(string[] args)     

        {   double a, b, c,sum;

            a = Convert.ToDouble(Console.ReadLine());

            b = Convert.ToDouble(Console.ReadLine());

            c = Convert.ToDouble(Console.ReadLine());

Cal cc = new Cal(a,b,c);

            sum=cc.SqrtForSum(a,b,c);

            Console.WriteLine("sum={0}", sum);

        }

    }

5.编写一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等字段。然后通过对Person类的继承得到一个学生类(Student)。学生类能够存放学生5门课的成绩,具有重载构造函数用于对学生的信息进行赋值(构造函数进行重载,至少给出三种形式),并有求平均成绩的方法和显示学生信息的方法,最后编程对Student类的功能进行验证。

public class Person

    {   public string Name;

        public int Age;

        public char Sex;

    }

    public class Student : Person

    {   public double[] Score;

        public double Aver;

        public Student(string xm, int nl, char xb)

        {   Name = xm;  Age = nl;    Sex = xb;

            Score = new double[5];

        }

        public Student()

        {   Name = "王平";   Age = 18;    Sex = '';

            Score = new double[5];

        }

        public Student(string xm, int nl, char xb, double[] cj)

        {   Name = xm;      Age = nl;     Sex = xb;

            Score = new double[5];

            for (int i = 0; i < 5; i++)   Score[i] = cj[i];

        }

        public double CalAver()

        {   double sum = 0;

            for (int i = 0; i < 5; i++)   sum = sum + Score[i];

            Aver = sum / 5;

            return (Aver);

        }

        public void display()

        {   Console.WriteLine("姓名:{0}", Name);

            Console.WriteLine("年龄:{0}", Age);

            Console.WriteLine("性别:{0}", Sex);

            Console.Write("成绩:");

            for (int i = 0; i < 5; i++)  Console.Write("{0} ", Score[i]);

            Console.WriteLine("\n平均成绩:{0}", CalAver());

        }

    }

    class Program

    {   public static void Main()

        {   double[] cj = { 87, 78, 90, 67, 89 };

            Student stu;

            stu = new Student();    stu.display();

            stu = new Student("张三", 20, '');    stu.display();

            stu = new Student("张三", 20, '', cj);       stu.display();

            Console.Read();

        }

    }

6设计一个控制台应用程序,定义如下接口Ia

interface Ia                                             //声明接口Ia

{   float getarea();                                    //求面积

}

从它派生Rectangle(长方形类)和Square(正方形)两个类,包含getarea方法的实现。并分别输出长23的长方形和长5的正方形的面积。

    interface Ia                                               //声明接口Ia

    {  float getarea();                               //接口成员声明

    }

    public class Rectangle : Ia                    //Rectangle继承接口Ia

    {   float x, y;

        public Rectangle(float x1, float y1)  //构造函数

        {  x = x1;     y = y1;        }

        float Ia.getarea()           //显式接口成员实现,带有接口名前缀,不能使用public

        {  return x * y;        }

    }

    public class Square : Ia                             //Square继承接口Ia

    {   float x;

        public Square(float x1)                 //构造函数

        {   x = x1;           }

        float Ia.getarea()           //显式接口成员实现,带有接口名前缀,不能使用public

        {   return x * x;      }

    }

    class Program

    {   static void Main(string[] args)

        {   Rectangle box1 = new Rectangle(2.0f, 3.0f);                 //定义一个类实例

            Ia ia = (Ia)box1;                                        //定义一个接口实例

            Console.WriteLine("长方形面积: {0}", ia.getarea());

            Square box2 = new Square(5.0f);                               //定义一个类实例

            Ia ib = (Ia)box2;                                        //定义一个接口实例

            Console.WriteLine("正方形面积: {0}", ib.getarea());

        }

    }

7设计一个控制台应用程序,含一个学生类Stud(每个学生有学号xh、姓名xm和分数fs私有字段),向ArrayList对象中添加若干个学生对象,并按分数从高到低输出所有学生信息。要求比较采用继承Icomparer接口的方式实现。

class Program

    {   class Stud

        {   int xh;                     //学号

            string xm;                  //姓名

            int fs;                     //分数

            public int pfs                //pfs属性

            {   get { return fs; }            }

            public Stud(int no, string name, int degree)

            {   xh = no; xm = name;    fs = degree;          }

            public void disp()

            {   Console.WriteLine("\t{0}\t{1}\t{2}", xh, xm, fs);       }

        }

        public class myCompareClassfs : IComparer

        {   int IComparer.Compare(object x, object y)

            {   Stud a = (Stud)x;         Stud b = (Stud)y;

                if (a.pfs < b.pfs) return 1;

                else if (a.pfs == b.pfs) return 0;

                else return -1;

            }

        }

        static void disparr(ArrayList myarr, string  str)

        {   Console.WriteLine(str);

            Console.WriteLine("\t学号\t姓名\t分数");

            foreach (Stud s in myarr)    s.disp();

        }

        static void Main(string[] args)

        {   int i, n = 4;

            IComparer myComparerfs = new myCompareClassfs();

            ArrayList  myarr = new ArrayList();

            Stud[] st = new Stud[4] { new Stud(1, "Smith", 82), new Stud(4, "John", 88),

new Stud(3, "Mary", 95), new Stud(2, "Cherr", 64) };

            for (i = 0; i < n; i++)  myarr.Add(st[i]);

            myarr.Sort(myComparerfs);

            disparr(myarr, "按分数降序排序后:");

        }

    }

8.设计一个控制台应用程序项目,输入若干个学生的英语和数学成绩,求出总分,并按总分从高到低排序。要求设计一个学生类Student,所有学生对象存放在一个Student对象数组中,通过一个方法对其按照总分进行降序排序,最出输出排序后的结果。要求比较采用继承继承Icomparable接口的方式实现。

public class Student:IComparable

    {   private string name;

        private int eng, math, sum;

        public int psum

        {  get { return sum; }       }

        public void inscore()

        {   Console.Write("姓名:");       name = Console.ReadLine();

            Console.Write("英语:");     eng = int.Parse(Console.ReadLine());

            Console.Write("数学:");      math = int.Parse(Console.ReadLine());

            sum = eng + math;

        }

        public void display()

        {     Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", name, eng, math, sum);    }

        public int CompareTo(object obj)             //实现接口方法

        {     Student s = (Student)obj;              //转换为Student实例

            if (psum < s.psum)        return 1;

            else if (psum == s.psum)      return 0;

            else return -1;

        }

    }

    class Program

    {     static void Main(string[] args)

        {    int  n, i;

            ArrayList myarr = new ArrayList();

            Student p;                            //定义对象引用

            Console.Write("输入学生人数:");

            n = int.Parse(Console.ReadLine());

            for (i = 0; i < n; i++)

            {    Console.WriteLine("输入第{0}个学生数据:", i + 1);

                p = new Student();                  //创建对象引用实例

                p.inscore();  

myarr.Add(p);

            }

            Console.WriteLine("排序前:");

            Console.WriteLine("\t姓名\t英语\t数学\t总分");

            i = 1;

            foreach(Student s in myarr)

            {     Console.Write("序号{0}:", i++);

                s.display();

            }

            myarr.Sort();                      //按总分降序排序

            Console.WriteLine("排序后:");

            Console.WriteLine("\t姓名\t英语\t数学\t总分");

            i = 1;

            foreach (Student s in myarr)

            {     Console.Write("{0}:", i++);

                s.display();

            }

        }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值