实验3 正确
大学 C#程序设计 实验(实习) 报告
实验(实习) 名称 多态 实验(实习) 日期 5.22 得分 指导老师 系 专业 班级 姓名 学号
一、实验目的
1、掌握派生类的设计方法。
2、掌握虚拟方法(属性)和重写方法(属性)的设计方法。
3、掌握将基类对象引用到派生类实例上的方法。
4、理解运行时多态的含义。
二、实验原理
当基类中存在抽象方法或虚拟方法时,在派生类中可以重写同名的方法,抽象方法用abstract 说明,虚拟方法用virtual 说明,重写方法用override 说明。
在使用基类定义对象时,基类对象可以引用到基类的实例上也可以引用到派生类的实例上。
如果程序中存在基类对象调用方法,那么编译时程序无法确定是调用基类的(虚拟)方法还是调用哪个派生类的(重写)方法,只有在程序执行时才能够确定到底调用哪个方法。由于是在运行时才区分出来的程序中出现的多态,所以这样的多态称为运行时多态。
三、实验内容与步骤
1、编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。
(1)创建一个类A ,在A 中编写一个可以被重写的带int 类型参数的方法MyMethod ,并在该方法中输出传递的整型值加10后的结果。
(2)再创建一个类B ,使其继承自类A ,然后重写A 中的MyMethod 方法,将A 中接收的整型值加50,并输出结果。
(3)在Main 方法中分别创建类A 和类B 的对象,并分别调用MyMethod 方法。
代码:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
int c;
A a = new A();
B b = new B();
Console.WriteLine("输入值为:");
c = int.Parse(Console.ReadLine());
a.MyMethod(c);
b.MyMethod(c);
}}}
A.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class A
{
public virtual void MyMethod( int x)
{
Console.WriteLine("{0}", x + 10);
}}}
B.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class B:A
{
public override void MyMethod(int x)
{
Console.WriteLine("{0}", x + 50);
}}}
2、创建一个控制台应用程序项目,项目名为ShapeSample 。
(1)创建代表‘形状’的Shape 类,其中包含如下操作:
Draw 方法接收两个参数x 和y ,分别是形状的X 轴和Y 轴的位置,虚函数,能被派生类重写。 GetArea 方法返回当前形状的面积,返回类型为double ,虚函数,能被派生类重写。
注:在Shape 类中的函数不执行任何操作,函数体为空。
(2)创建代表‘圆形’的Circle 类,该类从Shape 类派生,该类的构造函接收一个类型为double 的参数作为圆的半径,该类中重写了基类中的如下操作:
Draw 方法重写基类中的Draw 函数,使其能够输出:形状被绘制位置的坐标和圆的半径。
GetArea 方法返回当前圆形的面积,返回类型为double 。
(3)创建代表‘正方形’的Square 类,该类从Shape 类派生,该类的构造函接收一个类型为double 的参数作为正方形的边长,该类中重写了基类中的如下操作:
Draw 方法重写基类中的Draw 函数,使其能够输出:形状被绘制位置的坐标和正方形的边长。 GetArea 方法返回当前正方形的面积,返回类型为double 。
(4)创建应用类Program ,在该类中创建用来绘制形状的DrawShape 静态函数。
该函数有三个参数:
x ,y :形状绘制的位置。
shape: 将要绘制的形状。
该函数完成如下功能:
使用接收来的坐标值调用shape 对象的Draw 函数,完成绘制操作。
调用shape 对象的GetArea 函数并输出其结果。
(4)在Program 类的Main 函数中输入如下代码,查看运行结果
DrawShape(10, 10, new Circle(11.4f));
DrawShape(100, 100, new Square(5f));
代码:
Program.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShapeSample
{
class Program
{
static void Main(string [] args)
{
Shape shape=new Shape (); DrawShape(10, 10, new Circle (11.4f));
DrawShape(100, 100, new Square (5f));
}
static void DrawShape(double x, double y,Shape shape)
{
shape.Draw(x, y);
shape.GetArea(x);
}
}
}
Shape.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShapeSample
{
class Shape
{
public virtual void Draw(double x, double y){ } public virtual void GetArea( double area) { }
}
}
Circle.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShapeSample
{
class Circle :Shape
{
double r;
public Circle(double riduse)
{
r = riduse;
}
public override void Draw(double x, double y) {
Console .WriteLine(" 圆心坐标:({0},{1})",x,y); Console .WriteLine(" 半径:{0}", r);
}
public override void GetArea(double area)
{
Console .WriteLine(" 面积:{0}", 3.14*r*r);
}
}
}
Square.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShapeSample
{
class Square :Shape
{
double l;
public Square(double lenght)
{
l = lenght;
}
public override void Draw(double x, double y)
{
Console .WriteLine(" 正方形顶点坐标({0},{1})", x, y); Console .WriteLine(" 边长{0}", l);
}
public override void GetArea(double area)
{
Console .WriteLine(" 面积{0}", l*l);
}
}
}