早教吧作业答案频道 -->其他-->
一个C#多态的例子下面是一个多态的例子,还有个被它引用的程序集,因为太长我就不写了.程序集说Shape是基类,其它的类都是派生类,并给出了计算每个形状的面积计算公式.我对这个例子很多地
题目详情
一个C#多态的例子
下面是一个多态的例子,还有个被它引用的程序集,因为太长我就不写了.程序集说Shape是基类,其它的类都是派生类,并给出了计算每个形状的面积计算公式.我对这个例子很多地方不太明白,请告诉我每句程序的意思,
using System;
using System.Drawing;
using _05_Base;
namespace _05_03
{
public class Class_05_03
{
public static void PrintArea(Shape shape)
{
Console.WriteLine("Area of {0} is :{1}",shape.Name,shape.Area);
}
public static void Main(String[] args)
{
Point p = new Point(0,0);
_05_Base.Rectangle r = new _05_Base.Rectangle();
r.Name = "Rectangle r";
r.Width = 10;
r.Height = 20;
Square s = new Square();
s.Name = "Square s";
s.Side = 15;
Ellipse e = new Ellipse();
e.Name = "Ellipse e";
e.SemiMajorAxis = 10;
e.SemiMinorAxis = 5;
Circle c = new Circle();
c.Name = "Circle c";
c.Radius = 6;
Triangle t = new Triangle();
t.Name = "Triangle t";
t.Point1 = new Point(3,3);
t.Point2 = new Point(3,0);
Shape[] shapes = new Shape[5];
shapes[0] = r;
shapes[1] = s;
shapes[2] = e;
shapes[3] = c;
shapes[4] = t;
foreach(Shape shape in shapes)
{
shape.Position = p;
PrintArea(shape);
}
}
}
}
下面是一个多态的例子,还有个被它引用的程序集,因为太长我就不写了.程序集说Shape是基类,其它的类都是派生类,并给出了计算每个形状的面积计算公式.我对这个例子很多地方不太明白,请告诉我每句程序的意思,
using System;
using System.Drawing;
using _05_Base;
namespace _05_03
{
public class Class_05_03
{
public static void PrintArea(Shape shape)
{
Console.WriteLine("Area of {0} is :{1}",shape.Name,shape.Area);
}
public static void Main(String[] args)
{
Point p = new Point(0,0);
_05_Base.Rectangle r = new _05_Base.Rectangle();
r.Name = "Rectangle r";
r.Width = 10;
r.Height = 20;
Square s = new Square();
s.Name = "Square s";
s.Side = 15;
Ellipse e = new Ellipse();
e.Name = "Ellipse e";
e.SemiMajorAxis = 10;
e.SemiMinorAxis = 5;
Circle c = new Circle();
c.Name = "Circle c";
c.Radius = 6;
Triangle t = new Triangle();
t.Name = "Triangle t";
t.Point1 = new Point(3,3);
t.Point2 = new Point(3,0);
Shape[] shapes = new Shape[5];
shapes[0] = r;
shapes[1] = s;
shapes[2] = e;
shapes[3] = c;
shapes[4] = t;
foreach(Shape shape in shapes)
{
shape.Position = p;
PrintArea(shape);
}
}
}
}
▼优质解答
答案和解析
using System; //调用system类 和C下的include一样
using System.Drawing; //调用system.drawing类
using _05_Base; //调用自建类05_base
namespace _05_03 //命名空间 05_03
{
public class Class_05_03 //公有类05_03
{
public static void PrintArea(Shape shape)
//公有类 静态 无返回值类 PrintArea(内部数据成员shape,把shape这个单词指定为shape类型,就象int i)
{
Console.WriteLine("Area of {0} is : {1}", shape.Name, shape.Area);
//控制台输出 Area of {0} is : {1} 其中0为shape.Name的数据 1 为shape.Area的数据
}
public static void Main(String[] args)
// 公有类 静态无返回值 主函数()
{
Point p = new Point(0, 0);
为point 分配一新的空间p(0,0)
_05_Base.Rectangle r = new _05_Base.Rectangle();
// 分配一新的空间r ,用05_Base.Rectangle类化
r.Name = "Rectangle r";
r.Width = 10;
r.Height = 20;
//以上3行为新分配的空间r 写入其类的对应值
Square s = new Square();
s.Name = "Square s";
s.Side = 15;
//用Square 类化s 分配空间 赋值
Ellipse e = new Ellipse();
e.Name = "Ellipse e";
e.SemiMajorAxis = 10;
e.SemiMinorAxis = 5;
//用Ellipse类化e 分配空间 赋值
Circle c = new Circle();
c.Name = "Circle c";
c.Radius = 6;
Triangle t = new Triangle();
t.Name = "Triangle t";
t.Point1 = new Point(3, 3);
t.Point2 = new Point(3, 0);
// 这两个自己会看了把?
Shape[] shapes = new Shape[5];
shapes[0] = r;
shapes[1] = s;
shapes[2] = e;
shapes[3] = c;
shapes[4] = t;
foreach(Shape shape in shapes)
//Shape shape in shapes不懂这是什么意思
{
shape.Position = p;
PrintArea(shape);
}
}
}
}
using System.Drawing; //调用system.drawing类
using _05_Base; //调用自建类05_base
namespace _05_03 //命名空间 05_03
{
public class Class_05_03 //公有类05_03
{
public static void PrintArea(Shape shape)
//公有类 静态 无返回值类 PrintArea(内部数据成员shape,把shape这个单词指定为shape类型,就象int i)
{
Console.WriteLine("Area of {0} is : {1}", shape.Name, shape.Area);
//控制台输出 Area of {0} is : {1} 其中0为shape.Name的数据 1 为shape.Area的数据
}
public static void Main(String[] args)
// 公有类 静态无返回值 主函数()
{
Point p = new Point(0, 0);
为point 分配一新的空间p(0,0)
_05_Base.Rectangle r = new _05_Base.Rectangle();
// 分配一新的空间r ,用05_Base.Rectangle类化
r.Name = "Rectangle r";
r.Width = 10;
r.Height = 20;
//以上3行为新分配的空间r 写入其类的对应值
Square s = new Square();
s.Name = "Square s";
s.Side = 15;
//用Square 类化s 分配空间 赋值
Ellipse e = new Ellipse();
e.Name = "Ellipse e";
e.SemiMajorAxis = 10;
e.SemiMinorAxis = 5;
//用Ellipse类化e 分配空间 赋值
Circle c = new Circle();
c.Name = "Circle c";
c.Radius = 6;
Triangle t = new Triangle();
t.Name = "Triangle t";
t.Point1 = new Point(3, 3);
t.Point2 = new Point(3, 0);
// 这两个自己会看了把?
Shape[] shapes = new Shape[5];
shapes[0] = r;
shapes[1] = s;
shapes[2] = e;
shapes[3] = c;
shapes[4] = t;
foreach(Shape shape in shapes)
//Shape shape in shapes不懂这是什么意思
{
shape.Position = p;
PrintArea(shape);
}
}
}
}
看了一个C#多态的例子下面是一个多...的网友还看了以下:
数学选择题.急!下列说法正确的:A正方体是圆柱B电视机的形状类似于球体C六角螺母形状类似于圆柱D鸡 2020-05-13 …
动宾结构和逆序状中结构以“冲喜”、“打围”等词为例,说明这一类词的构成.有人分析为动宾结构,有人分 2020-05-13 …
状语从句的引导词有哪些?包括时间,条件,目的,结果,原因,让步,方式,地点,比较的状语从句的引导词 2020-06-05 …
现在的生物分类是根据进化分类学派还是分支(支序)分类学派的原理分类的?在一本《进化生物学》看到说Wi 2020-11-03 …
我国古代志怪小说的开篇第一句罗列几个就OK了.我是指有序章或总起之类的那种,如果开篇就是故事的就不用 2020-11-23 …
下列说法中正确的是A.气体向真空中扩散时,气体分子变成均匀分布的可能性最大B.我们所说的有序状态,指 2020-12-05 …
序可分为“书序”和“赠序”两类,下列各种序中,与其他三项不同类的是()A《滕王阁序》B《伶官传序》C 2020-12-05 …
自然过程的方向性是从有序状态自发地转向无序状态.如何根据这种理论说明热传递和功变热两种过程的不可逆性 2020-12-05 …
已知概率密度函数和概率分布函数,如何产生一个符合概率密度函数图形的时间序列?X为随机序列,概率密度函 2020-12-24 …
英语句子中多人物排列顺序比如说:aregoodfriends.A.You,LilyandIB.Lil 2020-12-27 …