一个C#多态的例子下面是一个多态的例子,还有个被它引用的程序集,因为太长我就不写了.程序集说Shape是基类,其它的类都
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/12 02:14:21
一个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#多态的例子下面是一个多态的例子,还有个被它引用的程序集,因为太长我就不写了.程序集说Shape是基类,其它的类都
谁能帮我用C#做一下下面的程序
用C#编写一个求解一元二次方程的程序
用c#编写一个冒泡法排序的程序
求一个C#静态函数和动态函数的例子
古诗词是可以活用的,《成功》一文中引用的王国维的“境界说”,就是一个著名的例子.
举一个例子,说明一个程序可能同时属于多个进程
举三个以上例子人因为失去竞争精神危机意识而失败的例子多一个,
我是初学者,跪求会C#的帮忙写个程序,统计一个单词表里“ture”结尾的单词数量
C#设计一个控制台程序,将接收的一个整数写成若干连续整数之和
C语言中,开方函数sqrt()怎么用?本人是初学者,举个简单的例子,下面这个程序,怎么输出a的开方数?
请帮写一个Matlab程序,给我个大概的思路