c#错误 1 “Graphics”方法没有采用“0”个参数的重载 ,z这是为什么呢

来源:百度知道 编辑:UC知道 时间:2024/06/14 16:43:39
using System;
using System.Collections.Generic;
using System.Text;

namespace __3
{
public abstract class Graphics
{
public double a, b, c;
public Graphics(double x, double y, double z)
{
a = x;
b = y;
c = z;
}
public virtual void Area()
{
}
}
class Trapezia : Graphics
{
public Trapezia(double x, double y, double z)
{
a = x;
b = y;
c = z;
}

public override void Area()
{
double s;
s = (a + b) * c / 2;
Console.WriteLine("梯形的面积是{0}", s);
}
}
class Triangle : Graphics
{
public Triangle(double x, double y, double z)
{
a =

因为在默认情况下子类/派生类要调用父类/基类的构造函数(不指明就是无参构造函数)
一个类,在你不定义构造函数的情况下,会有默认的无参构造函数。当你定义了有参的构造函数时,默认的无参构造函数不会被系统自动定义。

在以上两点的基础上,所以当你定义Triangle(double x, double y, double z)这个构造函数的时候,实际上它相当于
Triangle(double x, double y, double z) : base()
由于没有Graphics()这个构造函数,所以报错。

解决方法以下两种:
1.定义Graphics()
2.修改Triangle的构造函数为
Triangle(double x, double y, double z) : base(x, y, z)

这类问题都是方法有参数,在你调用的时候没给参数,就报这样的错!