有关构造函数的问题 [英] Questions about constructors

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

问题描述

先谢谢了.


1)在类继承中有多重层次结构……...我正在使类c成为对象,并且如果有一种方法不调用其父类默认构造函数

Thanks in advance.


1)There is multi hirarchy in class inheritance.................. I am making the object of class c ,& if there is a way not to call its parents class default constructor.Is it possible?

推荐答案

是:
public class MyParent
    {
    public MyParent()
        {
        Console.WriteLine("MyParent Default");
        }
    public MyParent(string s)
        {
        Console.WriteLine(s);
        }
    }
public class MyClass : MyParent
    {
    public MyClass(string s)
        : base("the string is: " + s)
        {
        Console.WriteLine(s);
        }
    public MyClass(int i)
        : base()
        {
        Console.WriteLine(i);
        }
    }


基本:

Basically:

Public BaseClass
{
  Public BaseClass()
  [
    //Do Something
  }
}

Public Derived : BaseClass
{
  Public Derived()
  { 
    Dp Something Else, or do nothing
  }
}



如果不执行此类操作,则不会调用基类的构造函数,只需重写基类的构造函数即可:




If you do not do something like this, the base class constructor will not be called, just have to override the constructor of the base class:


Public Derived : BaseClass
{
  Public Derived() : base()
  {
    Dp Something Else, or do nothing
  }
}


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

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