C尖的构造函数 [英] Constructor in C sharp

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

问题描述

案例1:



case 1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check
{
    class Baseclass
    {
        //public Baseclass()
        //{

        //    Console.WriteLine("Now i am in default constructor");
        //}
        public Baseclass(int a)
        {
            Console.WriteLine("Value of Baseclass a is : {0}", a + 10);
        }
    }

    class Derivedclass:Baseclass
    {

        //public Derivedclass()
        //{

        //}

        public Derivedclass(int a)
            : base(a)
        {
            Console.WriteLine("Value of Derived Class a is : {0}", a);

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Derivedclass d = new Derivedclass(5);
            Console.ReadLine();
        }
    }
}







- 现在这个给我输出

Baseclass a的值是:15

Derivedclass a的值是:5



- ----------------------------------------

案例2 :






--Now this gives me output
Value of Baseclass a is : 15
Value of Derivedclass a is :5

-----------------------------------------
case 2 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check
{
    class Baseclass
    {
        //public Baseclass()
        //{

        //    Console.WriteLine("Now i am in default constructor");
        //}
        public Baseclass(int a)
        {
            Console.WriteLine("Value of Baseclass a is : {0}", a + 10);
        }
    }

    class Derivedclass:Baseclass
    {

        public Derivedclass()
        {

        }

        public Derivedclass(int a)
            : base(a)
        {
            Console.WriteLine("Value of Derived Class a is : {0}", a);

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Derivedclass d = new Derivedclass(5);
            Console.ReadLine();
        }
    }
}





- 现在这给了我错误

'check.Baseclass'不包含带'0'参数的构造函数



--------------- -------------------------------------------------- --------------

案例3:





--Now this gives me error
'check.Baseclass' does not contain a constructor that takes '0' arguments

-------------------------------------------------------------------------------
case 3:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check
{
    class Baseclass
    {
        //public Baseclass()
        //{

        //    Console.WriteLine("Now i am in default constructor");
        //}
        //public Baseclass(int a)
        //{
        //    Console.WriteLine("Value of Baseclass a is : {0}", a + 10);
        //}
    }

    class Derivedclass:Baseclass
    {

        public Derivedclass()
        {

        }

        public Derivedclass(int a)

        {
            Console.WriteLine("Value of Derived Class a is : {0}", a);

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Derivedclass d = new Derivedclass(5);
            Console.ReadLine();
        }
    }
}







现在这给了我输出:

衍生等级a的值是:5



---------------- ----------------------



案例4:






Now this gives me output:
Value of Derived class a is : 5

--------------------------------------

case 4:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check
{
    class Baseclass
    {
        //public Baseclass()
        //{

        //    Console.WriteLine("Now i am in default constructor");
        //}
        public Baseclass(int a)
        {
            Console.WriteLine("Value of Baseclass a is : {0}", a + 10);
        }
    }

    class Derivedclass:Baseclass
    {

        //public Derivedclass()
        //{
        //    Console.WriteLine("anurag");

        //}

        public Derivedclass(int a)

        {
            Console.WriteLine("Value of Derived Class a is : {0}", a);

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Derivedclass d = new Derivedclass(5);
            Console.ReadLine();
        }
    }
}







o / p

这给了我错误

-------------------------------- ------



1)

a)如果我们正在创建baseclass的参数化构造函数。

b)如果我们尝试创建派生类的默认构造函数

c)并且如果没有基类的默认构造函数

d)创建派生对象上课



显示错误。



2)

a)如果没有基类的构造函数

b)如果派生类的默认构造函数是

c)创建派生类的对象。



然后我没有得到错误



因此,当我们创建baseclass的参数化构造函数时,它只查找默认构造函数,并且仅在派生类时有没有构造函数?



请解释为什么会以这种方式发生?




o/p
This gives me error
--------------------------------------

1)
a)if we are creating parametrized constructor of baseclass.
b)And if we try to create default constructor of derived class
c)And if no default constructor of base class
d)creating object of derived class

it is showing error.

2)
a)If no constructor of baseclass is there
b)If default constructor of derived class is there
c)creating a object of derived class.

then i dont get error

So is it like when we create parametrized constructor of baseclass , then it looks for default constructor only and only if derived class constructor is there ?

Kindly explain why it is happening in such a way ?

推荐答案

1)在基类中创建单个参数构造函数时。你实际上只是有一种方法来初始化你的基类,它应该有一个参数。因此所有子类构造函数都必须用这个参数调用它的基类构造函数。
1) When you created single parameter constructor in base class. You are actually telling there is only one way to initialize your base class and it should have a parameter.Thus All child class constructor must call its base class constructor with this parameter.


...编辑#1 ...



OP询问案例#4,其中基类和派生类都有一个构造函数匹配参数签名,不会编译,抛出错误消息:不包含带0参数的构造函数。



如果是#4,构造函数在派生类不调用:base(a)... so ...编译器为基类查找无参数构造函数,并在找不到基类时抛出错误。



imho,这里有一个案例,其中C#行为的原因反映了语言设计者的选择,就像允许没有构造函数的基类一样,只有函数定义......如下图所示......与所有派生的c一起工作正常lasses无论他们实施什么构造器都是一种设计选择。



无论我们喜欢与否,我们是否将这种设计选择称为任意或开明爱他们,或恨他们:这就是语言的运作方式。



...结束编辑#1 ...



我会用两种方式解释这个:考虑'Baseclass:
... edit #1 ...

The OP asked why case #4, in which both base class and derived class each have one constructor with matching parameter signatures, will not compile, throwing an error message: "does not contain a constructor that takes 0 arguments."

In case #4, the constructor in the derived class does not call :base(a) ... so ... the compiler looks for a parameterless constructor for the base class, and throws an error when it does not find one.

imho, here is a case where the "why" of how C# behaves reflects the choices of the language designers, just as allowing a base class with no constructors, and only function definitions ... as shown below ... to work fine with all derived classes no matter what constructors they implement is a design choice.

Whether we like it or not, whether we call such design choices "arbitrary," or "enlightened," love them, or hate them: that's the way the language works.

... end edit #1 ...

I would explain this in two ways: consider this definition of 'Baseclass:
public class Baseclass
{
    public double hyp(int x, int y)
    {
        return (Math.Sqrt((x * x) + (y * y)));
    }
}

这里没有定义(正式)构造函数。任何继承自'Baseclass的类都可以使用不同数量的参数定义构造函数(尽可能多):

Here there are no (formal) constructors defined. Any class that inherits from 'Baseclass can define constructors (as many as you like) with different numbers of parameters:

public class Derivedclass: Baseclass
{
    public double hypo { get; set; }

    public Derivedclass(int x, int y)
    {
        hypo = this.hyp(x,y);
    }
}

请注意,在'Derivedclass'的定义中没有调用:base。实质上,'Baseclass充当函数的存储库'hyp:并且,如您所见,可以调用该函数而无需以通常的方式调用'Baseclass中的构造函数。



一旦你开始使用调用:base在派生类中,那么,一般来说,你应该确保你的基类中的构造函数的签名与'Derivedclass中的每个构造函数匹配,并且,你发现,你必须在'BaseClass中提供一个无参数的构造函数。



你在基类中放什么,应该仔细选择哪些数据,哪些方法来满足需要你的申请。在基类中有一个看起来像是一个从未使用过的无参数构造函数,不会烧掉你的cpu!



我们只是说,就像一个好父母,一个基类,有时候,渴望从孩子那里听到:)

Notice here that there is no call to :base in the definition of 'Derivedclass. In essence, 'Baseclass acts as a repository for a function 'hyp: and, as you see, the function can be invoked without calling a constructor in 'Baseclass in the usual way.

Once you start using calls to :base in a derived class, then, in general, you should make sure you have constructors in the base class whose signature match each of the constructors in 'Derivedclass, and, as you found out, you must provide a parameterless constructor in 'BaseClass.

What you put in a base class, what data, what methods, should be carefully selected to meet the needs of your application. Having what appears to be a never-used parameterless constructor hanging there, in the base class, will not burn-up your cpu !

Let's just say that, like a good parent, a base class, at times, yearns to hear from its children :)


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

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