using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ex3_zuoye3_4
{
class CArea
{
public abstract class Cshape
{
public abstract void SetData();
public abstract double Area();
}
public class CTriangle : Cshape
{
double s, h;
public override void SetData(double s, double h)
{
this.s = s;
this.h = h;
}
public override double Area()
{
return s * h / 2;
} }
public class CRect : Cshape
{
double a, b;
public override void SetData(double a, double b)
{
this.a = a;
this.b = b;
}
public override double Area()
{
return a * b;
}
}
public class CCirced : Cshape
{
double r;
public override void SetData(double r)
{
this.r = r;
}
public override double Area()
{
return Math.PI * r * r;
}
}
CTriangle a = new CTriangle();
CRect b = new CRect();
CCirced c = new CCirced();
public CArea()
{
a.SetData(2.0,3.0);
b.SetData(2.0, 3.0);
c.SetData(3.0);
}
public double sum()
{
return a.Area() + b.Area() + c.Area();
}
static void Main(string[] args)
{
CArea d = new CArea();
Console.WriteLine("面积之和为" + d.sum());
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ex3_zuoye3_4
{
class CArea
{
public abstract class Cshape
{
public abstract void SetData();
public abstract double Area();
}
public class CTriangle : Cshape
{
double s, h;
public override void SetData(double s, double h)
{
this.s = s;
this.h = h;
}
public override double Area()
{
return s * h / 2;
} }
public class CRect : Cshape
{
double a, b;
public override void SetData(double a, double b)
{
this.a = a;
this.b = b;
}
public override double Area()
{
return a * b;
}
}
public class CCirced : Cshape
{
double r;
public override void SetData(double r)
{
this.r = r;
}
public override double Area()
{
return Math.PI * r * r;
}
}
CTriangle a = new CTriangle();
CRect b = new CRect();
CCirced c = new CCirced();
public CArea()
{
a.SetData(2.0,3.0);
b.SetData(2.0, 3.0);
c.SetData(3.0);
}
public double sum()
{
return a.Area() + b.Area() + c.Area();
}
static void Main(string[] args)
{
CArea d = new CArea();
Console.WriteLine("面积之和为" + d.sum());
}
}
}
{
public abstract void SetData();
public abstract double Area();
}你的子类并没有实现父类的方法。你的子类为带有参数的方法,而父类的抽象方法中并没有带参数的方法。
将父类定义为:
public abstract class Cshape
{
public abstract void SetData(double s,double h);
public abstract void SetData(double r);
public abstract double Area();
}字类中再具体去实现三个方法。
CArea.CRect”不实现继承的抽象成员
CArea.CCirced”不实现继承的抽象成员
还是不行
public abstract class Cshape
{
public abstract void SetData(double s,double h);
public abstract void SetData(double r);
public abstract double Area();
}
所以你必须在你的字类中去实现上面三个方法class A : Cshape
{
public void SetData();
}
完整代码:
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication2
{
class CArea
{
public abstract class Cshape
{
public abstract void SetData(double s, double h);
public abstract void SetData(double r);
public abstract double Area();
} public class CTriangle : Cshape
{
double s, h;
public override void SetData(double s, double h)
{
this.s = s;
this.h = h;
}
public override double Area()
{
return s * h / 2;
}
public override void SetData(double r)
{
throw new Exception("The method or operation is not implemented.");
}
}
public class CRect : Cshape
{
double a, b;
public override void SetData(double a, double b)
{
this.a = a;
this.b = b;
}
public override double Area()
{
return a * b;
} public override void SetData(double r)
{
throw new Exception("The method or operation is not implemented.");
}
}
public class CCirced : Cshape
{
double r;
public override void SetData(double r)
{
this.r = r;
}
public override double Area()
{
return Math.PI * r * r;
}
public override void SetData(double s, double h)
{
throw new Exception("The method or operation is not implemented.");
}
}
CTriangle a = new CTriangle();
CRect b = new CRect();
CCirced c = new CCirced();
public CArea()
{
a.SetData(2.0, 3.0);
b.SetData(2.0, 3.0);
c.SetData(3.0);
}
public double sum()
{
return a.Area() + b.Area() + c.Area();
}
}
class Program
{
static void Main(string[] args)
{
CArea d = new CArea();
Console.WriteLine("面积之和为" + d.sum());
Console.ReadLine();
}
}
}========================================
如果你的父类中这样定义:
public abstract class Cshape
{
public abstract void SetData();
public abstract double Area();
}
那你的子类中必须有:
public class CTriangle : Cshape
{
public override void SetData()
{
// 实现代码
throw new Exception("The method or operation is not implemented.");
} public override double Area()
{
// 实现代码
throw new Exception("The method or operation is not implemented.");
}
}