早教吧作业答案频道 -->其他-->
一个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#多态的例子下面是一个多...的网友还看了以下:
初中化学常见的点燃反应,生成2物质的A和B点燃生成C和D.D+K生成C和G,C+E生成F,G和F生 2020-04-26 …
某班进行数学测试,A,B,C,D,E,F其中一人得199.老师说你们猜猜是谁得99.5A:或者是E 2020-05-13 …
A能转化成B和C,B能转化成C,D能转化成B,且能与C发生反应,C或D能转化成E,F能转化为E,G 2020-05-22 …
现有abcde五种物质,在常温下ABC为气体,D是黑色固体,E是液体,C和D含同一种元素,B和E含 2020-06-12 …
A,B,C,D,E五名同学的生日正好是相连的五天,A的生日比C早的天数与E的生日比B早的天数相同, 2020-06-15 …
下列哪一项不是生物适应环境的事例()A.蛇在冬季会冬眠B.春天到了,小燕子从遥远的南方飞回来了C.生 2020-11-20 …
你想对如图漫画《谁的错?》中的同学说()A.不负责任要受到法律制裁B.大错承认,小错就不管了C.学生 2020-12-01 …
关于图中各圈层的叙述正确的是()A.A、B、C、D为地球外部圈层,C为生物圈B.地球内部圈层由E、F 2020-12-17 …
A、B、C、D、E都是含碳元素的物质,它们之间的变化关系是:D在氧气充足时燃烧生成C,C和D在高温下 2020-12-20 …
请根据人与自然资源部分“成员”的对话,回答下列问题.对话:A这块土地不错,“开发”占用它!B人均耕地 2021-01-02 …