基地电话 [英] base call

查看:105
本文介绍了基地电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public Connector(IShape source,IShape desti, double cost, IShape sourceentrance, IShape destientrance) : base(source.Center, desti.Center)
{
  this._cost = cost;
  this._source = source;
  this._desti = desti;
  this._entrancesourceID = sourceentrance;
  this._entrancedestiID = destientrance;
}


类名是Connector,这是一个构造函数调用
我要这样做:


Class name is Connector and this is a constructor call
I want to do to this:

if(source == null)
  base(sourceentrance.Centre, desti.Centre)
else 
  base(source.Centre, desti.Centre)


推荐答案

您不能这样做.

在执行任何派生类构造函数代码之前必须调用基类构造函数-否则,从基类派生的类不能依赖或使用任何基类元素.

因此,通过使用冒号和base关键字提供固定的构造函数引用,您只能从类的定义访问基本构造函数.

要执行所需的操作,必须将基本构造函数中的代码提取到公共/受保护的方法中,并从派生的构造函数中访问该代码.
You cannot do that.

The Base class constructor has to be called before the execution of any derived class constructor code - otherwise classes derived from a base class could not rely on or use any of the base class elements.

As a result, you can only access the base constructor from the definition of your class, by providing a fixed construtor reference using a colon and the base keyword.

To do what you want, you will have to extract the code from the base constructor into a public/protected method and access that from your derived constructor.


最简单的方法是更改​​基础类构造函数采用三个参数并使其决定要做什么.电话是:

The simplest way is to change the base class constructor to take three parameters and make it decide what to do. The call would be:

base(source, sourceentrance, desti)



如果您不想更改任何内容,可以尝试:



If you don''t want to change anything you could try:

base( (source != NULL) ? source.Centre : sourceentrance.Centre, desti.Centre)


这篇关于基地电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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