派生类访问基类构造函数 [英] derived class accessing base class constructor

查看:134
本文介绍了派生类访问基类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使派生类调用类基类的构造函数

rectstyle = new Rectangle(double a,double b);



rectstyle是派生类的对象

矩形是基类

is it posible for a derive class to call a constructr of base class like that
rectstyle = new Rectangle(double a,double b);

rectstyle is object of derive class
rectangle is base class

推荐答案

派生类只能在自己的基础上调用基础构造函数构造函数,请参阅:

Derived class can call base constructor only in their own constructor, see, for instance:
  • "Calling base constructor in c#" at Stack Overflow[^]
  • "Initializing Bases and Members" at MSDN[^]


只需这样做......



在你的rectstyle类中创建一个这样的构造函数



public rectstyle(double a,double b)

:base(a,b)

{



}



然后只需创建这样的对象.. 。



rectstyle = new rectstyle(double a,double b);
Just do this...

In your rectstyle class create a constructor like this

public rectstyle(double a, double b)
:base (a,b)
{

}

Then just create the object like this...

rectstyle = new rectstyle (double a,double b);


这篇关于派生类访问基类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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