继承和构造函数错误? [英] Inheritence and constructor errors?

查看:59
本文介绍了继承和构造函数错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在尝试使用继承和多态性,但是当我尝试从Quadrilaterall基类制作派生类平行四边形时,出现构造函数错误,不可访问性错误.
代码如下:

Hi all,
I''m trying to use inheritance and polymorphism but when I try to make a derived class parallelogram from a Quadrilaterall base class I get a constructor error inaccessibility error.
The code is below:

namespace InheritenceANDPolymorphism
{
   public  class Quadrilateral
    {
        private double s1;
        private double s2;
        private double s3;
        private double s4;
        Quadrilateral(double side1, double side2, double side3, double side4)
        {
            this.s1 = side1;
            this.s2 = side2;
            this.s3 = side3;
            this.s4 = side4;
        }
        public double getPerimeter()
        {
            double result = s1 + s2 + s3 + s4;
            return result;
        }
        public virtual double getArea()      // pure virtual - this makes the class abstract
                                      // forcing use of this class through inheritance
        {
            double result = s1 * s2;
            return result;
        }
    }
}


class Parallelogram : Quadrilateral
   {
       private double height;
      // public virtual void Parallelogram(double s13, double s24, double h)
      // {
      // }
       Parallelogram() //I get the error here...
       {
       }



有任何建议吗?



Any suggestions?

推荐答案

使Quadrilateral.Quadrilateral public(可从任何地方访问),protected(可从派生类访问),internal(可从任何地方访问) (来自同一程序集)或internal protected(可从同一程序集的派生类访问),但不能private(如您所做的那样).因此,访问修饰符有五种变体.只有一个会导致和出错;您选择了唯一的一个;这可能是因为它是默认设置-您未使用任何访问说明符.您应该记住所有这些内容,并养成在所有情况下都使用最少的必需访问权限的习惯.

现在,为什么构造派生类需要访问基类的构造函数?因为它被隐式调用.

—SA
Make Quadrilateral.Quadrilateral public (accessible from anywhere), protected (accessible from derived classes), internal (accessible from anywhere from the same assembly) or internal protected (accessible from derived classes in the same assembly), but not private (as you did). So, there are five variants of access modifiers; and only one would cause and error; you''ve chosen the only one which does; this is probably because it is a default — you did not use any access specifiers. You should remember them all and develop a habit to use minimal required access in all cases.

Now, why construction of a derived class requires access to a constructor of a base class? Because it is called implicitly.

—SA


四边形的构造函数应该是公共的/受保护的.如果不指定访问修饰符,则默认情况下为私有.

另一个问题是,您需要调用基本构造函数并将参数从parallelogram类传递给它.
The constructor for quadilateral should be public/protected. When you do not specify an access modifier it is private by default.

The other issue is, you need to call the base constructor and pass the arguments to it from the parallelogram class.


嘿,

是的,您需要将"public"添加到类定义和"Parallelogram"类的构造函数定义中. (由于.NET引发错误,表明继承的类比基类更难访问,因此无法将其标记为受保护").除了他们提到的内容外,请注意以下几点.

类"Quadrilateral"具有重写的构造函数,该构造函数删除了类的默认构造函数.如果要返回默认构造函数,则需要再次显式定义它.

类平行四边形"是从四边形"继承的,并且只有默认的构造函数(即没有参数).当您创建平行四边形"对象时,.NET在基类中查找具有相同数量参数的构造函数,因此会向您抛出错误.您可以通过将代码更改为下面类似的代码,或将平行四边形"的构造函数更改为具有类似的参数并调用基本构造函数来克服此问题.

Hey there,

Yes, you need to add "public" to the class definition and the constructor definition of the class "Parallelogram". (It cannot be marked "Protected" since .NET throw an error saying the inherited class is less accessible than the base class). In addition to what they mentioned, please take note of below points as well.

The class "Quadrilateral" has an overridden constructor which removes the default constructor of a class. If you want the default constructor back, you need to explicitly define it again.

The class "Parallelogram" inherits from the "Quadrilateral" and has only a default constructor (i.e. no arguments). What happens here is when you create an object of "Parallelogram", .NET looks for the constructor with the same number of arguments in the base class, hence would throw an error to you. You can overcome this by changing the code to something similar below, or changing the constructor of "Parallelogram" to have similar arguments and call the base constructor.

public Parallelogram() : base(0, 0, 0, 0)
{
}




Or

public Parallelogram(double side1, double side2, double side3, double side4) : base(side1, side2, side3, side4)
{
}

public Parallelogram() : this(0, 0, 0, 0)
{
}



希望对您有所帮助:)问候



Hope this helps you :) Regards


这篇关于继承和构造函数错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆