资源描述
习题5 面向对象的高级编程
1、简要回答下列问题。
(1)举例说明new关键字可用于哪些方面?
(2)sealed关键字的作用是什么?什么情况下需要使用sealed关键字?
(3)哪些关键字可以用于版本控制?
答:1)在C#中,new关键字可用作运算符或修饰符。作为运算符用于在堆上创建对象和调用构造函数。作为修饰符用于隐藏基类成员的继承成员。
2) 在类声明中使用sealed修饰符可防止其它类继承此类。在方法声明中使用sealed修饰符可防止扩充类重写此方法。sealed修饰符主要用于防止非有意的派生,但是它还能促使某些运行时优化。具体说来,由于密封类永远不会有任何派生类,所以对密封类的实例的虚拟函数成员的调用可以转换为非虚拟调用来处理。
3) override关键字和new关键字均可用于版本控制。在C#中,默认情况下方法不是虚拟的。若要使方法成为虚拟方法,必须在基类的方法声明中使用virtual修饰符。然后,派生类可以使用override关键字重写基类中的虚拟方法,或使用new关键字隐藏基类中的虚拟方法。如果override关键字和new关键字均未指定,编译器将发出警告,并且派生类中的方法将隐藏基类中的方法。
2、简要回答抽象类和接口的主要区别。
答:抽象类和接口的一个主要差别是:类可以实现多个接口,但仅能从一个抽象类或任何其它类型的类继承。
3、编写一个控制台应用程序,完成下列功能,并回答提出的问题。
(1)创建一个类A,在构造函数中输出“A”,再创建一个类B,在构造函数中输出“B”。
(2)从A继承一个名为C的新类,并在C内创建一个成员B。不要为C创建构造函数。
(3)在Main方法中创建类C的一个对象,写出运行程序后输出的结果。
(4)如果在C中也创建一个构造函数输出“C”,整个程序运行的结果又是什么?
答:(1)、using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
public class A
{
public A()
{
Console.WriteLine("A");
}
}
public class B:A
{
public B()
{
Console.WriteLine("B");
}
}
class Program
{
static void Main(string[] args)
{
B shu = new B();
Console.ReadLine();
}
}
}
(2)、using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
public class A
{
public A()
{
Console.WriteLine("A");
}
}
public class B
{
public B()
{
Console.WriteLine("B");
}
}
public class C : A
{
B newc = new B();
}
class Program
{
static void Main(string[] args)
{
B shu = new B();
Console.ReadLine();
}
}
}
(3)、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
public class A
{
public A()
{
Console.WriteLine("A");
}
}
public class B : A
{
public B()
{
Console.WriteLine("B");
}
}
public class C : A
{
B newc = new B();
}
class Program
{
static void Main(string[] args)
{
B shu1 = new B();
Console.WriteLine("\n");
C shu2 = new C();
Console.ReadLine();
}
}
}
(4)、using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
public class A
{
public A()
{
Console.WriteLine("A");
}
}
public class B : A
{
public B()
{
Console.WriteLine("B");
}
}
public class C : A
{
public C()
{
Console.WriteLine("C");
}
B newc = new B();
}
class Program
{
static void Main(string[] args)
{
B shu1 = new B();
Console.WriteLine("\n");
C shu2 = new C();
Console.ReadLine();
}
}
}
4、编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。
(1)创建一个类A,在A中编写一个可以被重写的带int类型参数的方法MyMethod,并在该方法中输出传递的整型值加10后的结果。
(2)再创建一个类B,使其继承自类A,然后重写A中的MyMethod方法,将A中接收的整型值加50,并输出结果。
(3)在Main方法中分别创建类A和类B的对象,并分别调用MyMethod方法。
答:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication7
{
public class A
{
public virtual void MyMethod(int sum)
{
sum += 10;
Console.WriteLine(sum);
}
}
public class B : A
{
public override void MyMethod(int sum)
{
sum += 50;
Console.WriteLine(sum);
}
}
class Program
{
static void Main(string[] args)
{
A shu1 = new A();
B shu2 = new B();
Console.WriteLine("请?输º?入¨?一°?个?数ºyA:êo");
int sum = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0}加¨®10后¨®的Ì?结¨¢果?为a:êo",sum);
shu1.MyMethod(sum);
Console.WriteLine("{0}加¨®50后¨®的Ì?结¨¢果?为a:êo", sum);
shu2.MyMethod(sum);
Console.ReadLine();
}
}
}
5、假设Node类的每一个节点包括有两个字段:m_data(引用节点的数据)和m_next(引用链接列表中的下一项)。这两个字段都是由构造函数方法设置的。该类有两个功能,第一个功能是通过名为Data和Next的只读属性访问m_data和m_next字段。第二个功能是对System.Object的ToString虚拟方法进行重写。试分别用类和泛型两种方法编写程序实现上述功能。
答:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication7
{
class Node
{
Object m_data;
Node m_next;
public Node(Object data, Node next)
{
m_data = data;
m_next = next;
}
// 访问结点数据
public Object Data
{
get { return m_data; }
}
// 访问下一个结点
public Node Next
{
get { return m_next; }
}
// 获取结点数据描述
public override String ToString()
{
return m_data.ToString();
}
}
// 链表结点类的泛型定义
class Node<T>
{
T m_data;
Node<T> m_next;
public Node(T data, Node<T> next)
{
m_data = data;
m_next = next;
}
// 访问结点数据
public T Data
{
get { return m_data; }
set { m_data = value; }
}
// 访问下一个结点
public Node<T> Next
{
get { return m_next; }
set { m_next = value; }
}
// 获取结点数据描述
public override String ToString()
{
return m_data.ToString();
}
}
// 使用结点类型或泛型结点类型
class LinkedList
{
static void Main(string[] args)
{
//// 创建整数链表
//Node head = new Node(5, null);
//head = new Node(10, head);
//head = new Node(15, head);
////遍历链表求整数和
//Int32 sum = 0;
//for (Node current = head; current != null;
// current = current.Next)
//{
// sum += (Int32)current.Data;
//}
//// 输出结果?
//Console.WriteLine("Sum of nodes = {0}", sum);
// 用泛型创建整数链表
Node<Int32> head = new Node<Int32>(5, null);
head = new Node<Int32>(10, head);
head = new Node<Int32>(15, head);
// 遍历求和
Int32 sum = 0;
for (Node<Int32> current = head; current != null;
current = current.Next)
{
sum += current.Data;
}
// 输出?
Console.WriteLine("Sum of nodes = {0}", sum.ToString());
Console.ReadLine();
}
}
}
展开阅读全文