夜光序言:
人的一生可能燃烧也可能腐朽,我不能腐朽,我愿意燃烧起来!失败乃成功之母。人只有献身于社会,才能找出那短暂而有风险的生命的意义。物竞天择,适者生存。
正文:
抽象类的例子其实没有体现多态性,可以再加一个继承类(三角形面积),以体现多态。
using System;
namespace GeniusTest01
{
abstract class Shape
{
public abstract int area();
}
class Rectangle : Shape
{
private int length;
private int width;
public Rectangle(int a = 0, int b = 0)
{
length = a;
width = b;
}
public override int area()
{
//Console.WriteLine("Rectangle 类的面积:");
return (width * length);
}
}
class triangle : Shape
{
private int baseLine;
private int height;
public triangle(int a = 0, int b = 0)
{
baseLine = a;
height = b;
}
public override int area()
{
//Console.WriteLine("三角形类的面积:");
return (2 *baseLine* height/4);
}
}
public class Class87
{
static void Main(string[] args)
{
Rectangle r = new Rectangle(10, 7);
double a = r.area();
triangle t = new triangle(10, 7);
double b = t.area();
Console.WriteLine("夜光:长方形面积: {0}, 三角形面积:{1}", a, b);
Console.ReadKey();
}
}
}