这道题没什么意思,就是让你掌握继承和重写,很简单。
using System;
using System.Collections.Generic;
using System.Text;
namespace ShapesDemo
{
abstract class Shape
{
public int _color;
public void getColor()
{
Console.WriteLine("输入颜色值:");
this._color = int.Parse(Console.ReadLine());
}
public abstract void getValues();
public abstract double getArea();
}
class Circle : Shape
{
protected double _radius;
public override void getValues()
{
Console.WriteLine("输入半径值:");
this._radius = double.Parse(Console.ReadLine());
}
public override double getArea()
{
double area = this._radius * this._radius * 3.14;
return area;
}
}
class Square : Shape
{
protected double _sideLen;
public override void getValues()
{
Console.WriteLine("输入边长值:");
this._sideLen = double.Parse(Console.ReadLine());
}
public override double getArea()
{
double area = this._sideLen * this._sideLen;
return area;
}
}
class shapeTest
{
static void Main(string[] args)
{
Shape s;
Console.WriteLine("开始绘制圆形");
s = new Circle();
s.getColor();
s.getValues();
Console.WriteLine("填充颜色值为{0},圆形面积为{1}",s._color,s.getArea());
Console.WriteLine("\n开始绘制正方形");
s = new Square();
s.getColor();
s.getValues();
Console.WriteLine("填充颜色值为{0},正方形面积为{1}", s._color, s.getArea());
}
}
}
作业?晕~ 你说的数据成员要什么类型也没说,方法要不要实现也没说 那就写个空方法让你自己来写吧!别再问如何实现方法里面的代码的,自己做吧 不做永远不会的!
public abstract class Shape
{
private string _color;//存放颜色值
//获取颜色值方法
public string getColor()
{
return this._color;
}
//抽象方法
public abstract double getArea();
}
public class Circle : Shape
{
private string _radius;
private string _sideLen;
//方法的实现
public override double getArea()
{
}
}
public class Square : Shape
{
private string _radius;
private string _sideLen;
//方法的实现
public override double getArea()
{
}
}