帮助,“这个”构造函数中的初始化程序。 [英] Help with the, "this" initializer in a constructor.

查看:77
本文介绍了帮助,“这个”构造函数中的初始化程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



-----开始PGP签名消息-----

哈希:SHA1


大家好。


我正在自学C#,我觉得我做得很好。


我学会了怎么打电话一个类的正确构造函数,如果

类具有多个cosntructor,则确保每个
构造函数具有不同的签名。我已经设法学习并获得了它在旧脑细胞中停留的价值。


但是我有一点困难, ;这"初始化程序用于调用

a类OWN构造函数而不是它继承的基类cosntructor。


示例如下,[为了清楚起见'我的缘故会提到这些

类是书中的例子]


我说这两个类一个是基类,另一个是派生类。


A级

{

公共A()

{

Console.WriteLine (A);

}


} //结束A级


B级:A

{

public B():base()

{

Console.WriteLine( B);

}


} //结束班级B

现在我在B级知道它是,明确地称它为'基类'

基类的构造函数,A使用初始化器,base() 。


但是我不知道怎么做才能确保派生类,B

只调用它''通过使用this()自己的构造函数。启动器。


任何人都可以帮我这个吗?


正如我所说,我已经学会了如何通过<重载方法和构造函数br />
不同的签名方法,我只是对如何使用

,this()而感到困惑。构造函数中的初始化程序使它的类称为

而不是它的基类构造函数,如果它是
派生它将会执行它上课,即使没有明确的电话。


提前致谢:)


播放器




- -

***********

我听到了,我忘记了。

我明白了,我记得了。

我这样做,我理解。

- - 孔子

***** *****¥

-----开始PGP签名-----

版本:PGP 8.0


iQA / AwUBQYXnOS / z2sM4qf2WEQIzAQCgkP3vbB5lNJotB1 / 9WXSt3 + jQnb4Anjek

6jRR6opsxA7UIqLslbD53ugg

= JHP

----- END PGP SIGNATURE ---- -


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello all.

I am in the process of teaching myself C# and I think I am doing OK.

I have learnt how to how to call the right constructor of a class, if the
class has more than than one cosntructor, by making sure that each
constructor has a different signature. I have managed to learn that and get
it stuck up there in the old brain cells :)

But I am having a little difficulty with the, "this" initializer for calling
a classes OWN constructor rather than it''s inherited base class cosntructor.

Example as follows, [and for clarity''s sake I will mention that these
classes are book examples]

Say I have these two classes one a base class the other a derived one.

class A
{
public A()
{
Console.WriteLine("A");
}

} // end class A


class B : A
{
public B() : base()
{
Console.WriteLine("B");
}

} // end class B
Now I know in class B that it is, "explicitly" calling it''s base class
constructor from the base class, "A" using the initializer, "base()" .

BUT what I don''t know how to do is make sure that the derived class, "B"
calls ONLY it''s own constructor via the use of the, "this()" initilaizer.

Can anyone help me with this please?

As I have said, I have learnt how to overload methods and constructors via
the different signature approach, I am just a touch confused on how to use
the, "this()" initializer within a constructor to make it''s class call it
and not it''s base class constructor, which it will do anyway if it''s a
derived class, even without the explicit call.

Thanks in advance :)

Player



- --
***********
I hear, and I forget.
I see, and I remember.
I do, and I understand.
- -- Confucius
************
-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBQYXnOS/z2sM4qf2WEQIzAQCgkP3vbB5lNJotB1/9WXSt3+jQnb4Anjek
6jRR6opsxA7UIqLslbD53ugg
=JHPs
-----END PGP SIGNATURE-----

推荐答案



-----开始PGP签名消息-----

哈希:SHA1

快速查看...


B bee = new B();


给我输出结果..


A

B


当我想要的只是...


B


得到我的意思??


再次感谢...


播放器


-----开始PGP签名--- -

版本:PGP 8.0

iQA / AwUBQYXrLi / z2sM4qf2WEQI8rQCfb4mhPSr / A5F5AIpWSABplz / mmHkAoKTN

mJYor2zowLZYrtmEXJqjbY81

= elHL

----- END PGP SIGNATURE -----

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
See a quick...

B bee = new B();

Gives me as output..

A
B

When all I might want is..

B

Get what I mean??

Thanks again...

Player

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBQYXrLi/z2sM4qf2WEQI8rQCfb4mhPSr/A5F5AIpWSABplz/mmHkAoKTN
mJYor2zowLZYrtmEXJqjbY81
=elHL
-----END PGP SIGNATURE-----


Player写道:
-----开始PGP签名消息-----
哈希:SHA1

大家好。

我正在自学C#我认为我做得很好。

我已经学会了如何调用类的正确构造函数,
如果类有多个cosntructor,通过确保<每个构造函数都有不同的签名。我已经设法学习了那个并且把它放在那里的老脑细胞中:)

但是我对这个这个有点困难。初始化器用于调用类OWN构造函数而不是它继承的基类
类cosntructor。

示例如下,[为了清楚起见我会提到的这些
课程是书中的例子]

说我有这两个类一个基类而另一个是派生类。

A类
{<公共A()
{
Console.WriteLine(" A");
}

} //结束类A


B班:A
公共B():基地()
{
Console.WriteLine(" B");
}

} //结束班级B

现在我在B级知道它是明确地从基类中调用它的'基类
构造函数,A使用初始化器,base() 。

但是我不知道怎么做才能确保派生类,B和B。通过使用this()
启动器来调用它自己的构造函数。


如果是仅的意思是tp阻止调用基类构造函数,这是不可能的。它会让你有一个不完整的东西。对象。


任何人都可以帮我这个吗?

正如我所说的,我已经学会了如何通过以下方法来重载方法和构造器不同的签名方式,我只是对如何使用this()而感到困惑。构造函数中的初始化程序
使它的类调用它而不是它的基类构造函数,如果它是派生类,它将会做什么,即使没有明确的
打电话。
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello all.

I am in the process of teaching myself C# and I think I am doing OK.

I have learnt how to how to call the right constructor of a class,
if the class has more than than one cosntructor, by making sure that
each constructor has a different signature. I have managed to learn
that and get it stuck up there in the old brain cells :)

But I am having a little difficulty with the, "this" initializer for
calling a classes OWN constructor rather than it''s inherited base
class cosntructor.

Example as follows, [and for clarity''s sake I will mention that these
classes are book examples]

Say I have these two classes one a base class the other a derived one.

class A
{
public A()
{
Console.WriteLine("A");
}

} // end class A


class B : A
{
public B() : base()
{
Console.WriteLine("B");
}

} // end class B
Now I know in class B that it is, "explicitly" calling it''s base class
constructor from the base class, "A" using the initializer, "base()" .

BUT what I don''t know how to do is make sure that the derived class,
"B" calls ONLY it''s own constructor via the use of the, "this()"
initilaizer.
If be mean by "ONLY" tp prevent calling a base class constructor, that''s
impossible. It would leave you with an "incomplete" object.

Can anyone help me with this please?

As I have said, I have learnt how to overload methods and
constructors via the different signature approach, I am just a touch
confused on how to use the, "this()" initializer within a constructor
to make it''s class call it and not it''s base class constructor, which
it will do anyway if it''s a derived class, even without the explicit
call.




正如我所说,这不起作用,我想知道为什么你会想要这样做?
。 this()初始化程序具有完全不同的含义,BTW。用来调用同一类的另一个构造函数是




公共类Foo {

私有长id;


public Foo():this(-1){}


public Foo(long id){

this .id = id;

}

}


-

Joerg Jooss www.joergjooss.de
ne ** @ joergjooss.de



As I said, that cannot work, and I wonder why you would ever want to do
that. The this() initializer has a complete different meaning, BTW. It is
used to call another constructor of the same class:

public class Foo {
private long id;

public Foo() : this(-1) {}

public Foo(long id) {
this.id = id;
}
}

--
Joerg Jooss
www.joergjooss.de
ne**@joergjooss.de




-----开始PGP签名留言 - ----

哈希:SHA1


对不起,我不知道你刚刚说了什么。


创建BI am elft类型的对象后,有两个输出...

A

B


当所有我想要创建一个B类型的对象,输出为...

B


为什么我必须输出...

A

以及

B ????

如果我有这两个我以前的原帖中的课程,以及我想要的所有内容都是一个类型的对象..

B

那么我该怎么办这样做我所调用的唯一构造函数是B'的

构造函数??


当然得到输出..

A

B

不想一半的时间,这意味着你的价值是

输出的两倍......


我想做的就是自己调用B'的构造函数,而不是调用

A'的构造函数。

衍生课程是不可能的吗?


播放器

-----开始PGP签名-----

版本:PGP 8.0

iQA / AwUBQYXwsS / z2sM4qf2WEQJuFACeMVYLBnRQbzcMZqpvsVjR5GvfX6kAnjRF

ODAEOY3kUvgxUG2LFJ3ZnJez

= zC3c

----- END PGP SIGNATURE -----

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sorry I don''t have a clue what you just said.

After creating an object of type B I am elft with two outputs...
A
B

When all I wanted was to create an object of type B with the output being...
B

Why do I have to have the output...
A
as well as
B ????
If I have those two classes in my previous original post, and all I want to
do is make an object of type..
B
then how do I do it so the only constructor I am calling is B''s
constructor??

Surely getting the output..
A
B
is not wanted half the time, that means you have double the value in the
output..

All I want to do is call B''s constructor on it own, and not have it calling
A''s constructor as well.
Is that impossible with a derived class??

Player
-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBQYXwsS/z2sM4qf2WEQJuFACeMVYLBnRQbzcMZqpvsVjR5GvfX6kAnjRF
ODAEOY3kUvgxUG2LFJ3ZnJez
=zC3c
-----END PGP SIGNATURE-----


这篇关于帮助,“这个”构造函数中的初始化程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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