在C#中认识的基础 [英] understanding base in c#

查看:96
本文介绍了在C#中认识的基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解的基础构造函数实现。考虑这种情况
。如果我有基类运动

 公共类运动
{
公INT标识{搞定;组; }
公共字符串名称{;组; }

公共体育()
{
Console.WriteLine(运动对象刚刚创建);
}

公共体育(INT ID,字符串名称)
{
Console.WriteLine(与创建两个参数运动对象);
}
}

现在我有inherhit体育类和我的篮球类。想在篮球对象初始化使用第二个构造带两个参数

 公共类篮球:体育
{
公共篮球:基地(????)
{
?????
}
}



首先,我想用私有字段诠释_id和串_Name并在构造函数调用中使用它们。

 公共篮球:基地(INT _id,串_Name)
{
n = _id;
名称= _Name;
}



但是,这并不让使用继承感,所以请对我解释这个例子。



更新
谢谢大家,我用这样的代码,它的确定。

 公共篮球(INT ID,字符串名称):基地(ID,姓名)
{
n = ID;
名称=名称;
}



只是为了确保,在这个行公共篮球(INT ID,字符串名称):基地(ID,姓名)我声明变量ID,姓名,因为我原来的予以资本化增值经销商,并利用作为PARAMS在基地(ID,姓名)打我的基础构造器。



感谢大家,非常有帮助/


解决方案

你必须通过值或变量>派生类到基类的构造如果基类的构造函数没有任何默认的无参数的构造函数



您不需要在基础构造函数调用声明任何



base关键字的主要目的是调用基类的构造



在一般情况下,如果你不声明自己的任何构造函数,编译器创建一个默认的构造函数为您服务。



参数 >,那么编译器不创建默认的无参数的构造函数



因此,在情况下,你没有一个默认的构造函数在基类中声明,并要拨打其参数的基类构造函数,你必须调用基类的构造函数,并通过关键字

做这样

 公共篮球():基地(1,用户)

 公共篮球(INT ID,字符串n):基地(ID,N)

 公共篮球(INT ID,字符串n) :基地()

 公共篮球():基地()

类似于

 公共篮球()//默认为
调用基类参数的构造函数

这一切都取决于你如何想你的类的表现得导出类实例化


I'm trying to understand base constructor implementation. Consider this situation If I have base class Sport

public class Sport 
{
   public int Id { get; set; }
   public string Name { get; set; }

   public Sport() 
   {
      Console.WriteLine("Sport object is just created");
   }

   public Sport(int id, string name) 
   {           
      Console.WriteLine("Sport object with two params created");
   }
}

Now I have basketball class which inherhit Sport class and I want to on basketball object initialization to use second constructor with two params.

public class Basketball : Sport
{    
    public Basketball : base ( ???? )
    {
       ?????
    }
}

First I was thinking to use private fields int _Id and string _Name and to use them in the constructor call

public Basketball : base ( int _Id, string _Name )
{
   Id = _Id;
   Name = _Name;
}

But that doesn't make sense of using inheritance, so please explain me on this example.

Updated Thanks everyone, I'm using code like this and it's ok.

public Basketball(int id, string name) : base (id, name)
{
   Id = id;
   Name = name;
}

Just to make sure, On this line public Basketball(int id, string name) : base (id, name) I'm declaring variables id, name, since my original are Capitalized vars, and using as params on base (id, name) to hit my base contructor.

Thank everyone., very helpful/

解决方案

You have to pass values or variables of the derives class to the base class constructor if the base class constructor does not have any default parameterless constructor.

You dont need to declare anything in the base constructor call

The main purpose of base keyword is to call the base class constructor.

In general,if you do not declare any constructor of your own,the compiler creates a default constructor for you.

But if you define your own constructor having parameter,then the compiler does not create the default parameterless constructor.

So in the case where you dont have a default constructor declared in the base class and want to call a base class constructor having parameters,you have to call that base class constructor and pass the required values through base keyword

Do it like this

public Basketball() : base (1,"user")

OR

public Basketball(int id,string n) : base (id,n)

OR

public Basketball(int id,string n) : base ()

OR

public Basketball() : base ()

is similar to

public Basketball()//calls base class parameterless constructor by default

It all depends on how you want your class to behave when the derived class is instantiated

这篇关于在C#中认识的基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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