📄 abstractshapestest.cs
字号:
// Fig. 10.8: AbstractShapesTest.cs
// Demonstrates polymorphism in Point-Circle-Cylinder hierarchy.
using System;
using System.Windows.Forms;
namespace AbstractShapes
{
public class AbstractShapesTest
{
public static void Main( string[] args )
{
// instantiate Point2, Circle2 and Cylinder2 objects
Point2 point = new Point2( 7, 11 );
Circle2 circle = new Circle2( 22, 8, 3.5 );
Cylinder2 cylinder = new Cylinder2( 10, 10, 3.3, 10 );
// create empty array of Shape base-class references
Shape[] arrayOfShapes = new Shape[ 3 ];
// arrayOfShapes[ 0 ] refers to Point2 object
arrayOfShapes[ 0 ] = point;
// arrayOfShapes[ 1 ] refers to Circle2 object
arrayOfShapes[ 1 ] = circle;
// arrayOfShapes[ 1 ] refers to Cylinder2 object
arrayOfShapes[ 2 ] = cylinder;
string output = point.Name + ": " + point + "\n" +
circle.Name + ": " + circle + "\n" +
cylinder.Name + ": " + cylinder;
// display Name, Area and Volume for each object
// in arrayOfShapes polymorphically
foreach( Shape shape in arrayOfShapes )
{
output += "\n\n" + shape.Name + ": " + shape +
"\nArea = " + shape.Area().ToString( "F" ) +
"\nVolume = " + shape.Volume().ToString( "F" );
}
MessageBox.Show( output, "Demonstrating Polymorphism" );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -