如何告诉继承类不调用其基类无参数构造函数? [英] How can I tell the inheriting class to not call its base class' parameter-less constructor?

查看:386
本文介绍了如何告诉继承类不调用其基类无参数构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我惊讶地发现,无论何时我在派生类中调用任何构造函数,我的基类的无参数构造函数都会被调用。我认为这是:base()是为了显式地调用基本构造函数,如果和当我想。



如何防止在实例化派生类时调用基础构造函数?

 使用系统; 

namespace TestConstru22323
{
class Program
{
static void Main(string [] args)
{
=新客户(Jim,Smith);
客户c = new Customer();
Console.WriteLine(customer.Display());

Console.ReadLine();
}
}

public class Person
{
public Person()
{
Console.WriteLine总是想让这个构造函数被调用,我想明确地调用它。
}
}

public class Customer:Person
{
private string _firstName;
private string _lastName;

// public Customer(string firstName,string lastName):base()//显式调用基础空构造函数
public Customer(string firstName,string lastName)//但是这样调用它反正,为什么?
{
_firstName = firstName;
_lastName = lastName;
}

public string Display()
{
return String.Format({0},{1},_lastName,_firstName);
}
}
}


解决方案>

唯一的方法是明确告诉它你希望它调用哪个其他这当然意味着你必须选择一些基础ctor来调用。



不能基本ctor - 概念,构建 Customer 是通过首先构造一个 Person ,然后在其上面执行 Customer 具体构造。例如,假设 Person private 字段 - 如果构建客户允许先构建


I was surprised to find out that the parameter-less constructor of my base class is called any time I call any constructor in a derived class. I thought that is what : base() was for, to explicitly call the base constructor if and when I want to.

How can I prevent the base constructor from being called when I instantiate a derived class?

using System;

namespace TestConstru22323
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer("Jim", "Smith");
            Customer c = new Customer();
            Console.WriteLine(customer.Display());

            Console.ReadLine();
        }
    }

    public class Person
    {
        public Person()
        {
            Console.WriteLine("I don't always want this constructor to be called. I want to call it explicitly.");
        }
    }

    public class Customer : Person
    {
        private string _firstName;
        private string _lastName;

        //public Customer(string firstName, string lastName): base()  //this explicitly calls base empty constructor
        public Customer(string firstName, string lastName) //but this calls it anyway, why?
        {
            _firstName = firstName;
            _lastName = lastName;
        }

        public string Display()
        {
            return String.Format("{0}, {1}", _lastName, _firstName);
        }
    }
}

解决方案

The only way is to explicitly tell it which other base ctor you want it to call; which of course means you must choose some base ctor to call.

You can't have it call no base ctor at all - conceptually, constructing a Customer is done by first constructing a Person, and then doing Customer specific construction on top of it. For example, suppose Person had private fields - how would these be correctly constructed if construction of a Customer was allowed to not first construct a Person?

这篇关于如何告诉继承类不调用其基类无参数构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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