继承和创造孩子 [英] Inheritance and child creation

查看:57
本文介绍了继承和创造孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我怎样才能实现childs构造函数只能从它的父级调用?

我必须声明该类是私有的A?


在此先感谢


公共MustInherit A级

受保护的子新()

结束子


公共共享功能CreateInstance()为A

返回新B

结束功能


结束班级


公共等级B:继承A


''应该只是可以从A中的CreateInstance方法调用。
Public Sub New()

Mybase.New()

End Sub

结束类

Hi,

How can I achieve that a childs constructor is only callable from it''s parent?
Must I declare the class Private within A?

Thanks in Advance

Public MustInherit Class A

Protected Sub New()
End Sub

Public Shared Function CreateInstance() As A
Return New B
End Function

End Class

Public Class B : Inherits A

''Should only be callable from the CreateInstance method in A
Public Sub New()
Mybase.New()
End Sub
End Class

推荐答案

John,
我怎样才能实现childs构造函数只能从它中调用
父母?

我通常把A& B进入类库(DLL),然后制作B.新朋友,因为

我通常有一个家庭。派生类。在那个C,D,E,F和G

也继承自A,而A.CreateInstance能够根据参数创建其中任何一个,
。类库代表这个家庭。


当然,类库中的其他类将能够创建B

对象。

公共类B:继承A

''只能从A中的CreateInstance方法调用。朋友Sub New()
Mybase.New()
结束Sub
End Class
我必须在A中声明该类是私有的吗?
将是另一个选项,这将确保没有人可以创建B

对象!我刚刚注意到System.IO.Stream.Null属性是

,是根据嵌套类实现的。


希望这有助于

Jay


" John" <菊*********** @ yahoo.se>在消息中写道

news:5f ************************** @ posting.google.c om ...

如何实现childs构造函数只能从它的
父级调用?我必须在A中宣布该课程为私人吗?

在此先感谢

公共MustInherit A级

受保护的子新()
结束Sub

公共共享功能CreateInstance()作为A
返回新B
结束功能

结束课

公共课B:继承A

''只能从A中的CreateInstance方法调用公共子新建()
Mybase.New()
End Sub
End Class
How can I achieve that a childs constructor is only callable from it''s parent?
I normally put A & B into a Class Library (DLL), then make B.New Friend, as
I normally have a "family" of derived classes. In that C, D, E, F, and G
also inherit from A, and A.CreateInstance is able to create any one of them,
based on a parameter. The class library represents this family.

Of course other classes within the class library will be able to create B
objects.
Public Class B : Inherits A

''Should only be callable from the CreateInstance method in A
Friend Sub New()
Mybase.New()
End Sub
End Class Must I declare the class Private within A? Would be the other option, which would ensure that no one could create B
objects! I just noticed that the System.IO.Stream.Null property is
implemented in terms of a nested class.

Hope this helps
Jay

"John" <ju***********@yahoo.se> wrote in message
news:5f**************************@posting.google.c om... Hi,

How can I achieve that a childs constructor is only callable from it''s parent? Must I declare the class Private within A?

Thanks in Advance

Public MustInherit Class A

Protected Sub New()
End Sub

Public Shared Function CreateInstance() As A
Return New B
End Function

End Class

Public Class B : Inherits A

''Should only be callable from the CreateInstance method in A
Public Sub New()
Mybase.New()
End Sub
End Class



" Jay B. Harlow [MVP-Outlook]" < JA ************ @ msn.com>在消息新闻中写道:< Oz ************** @ TK2MSFTNGP10.phx.gbl> ...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:<Oz**************@TK2MSFTNGP10.phx.gbl>...
John,
我怎样才能实现childs构造函数只能从它的
How can I achieve that a childs constructor is only callable from it''s


父级调用?
我通常把A& B进入类库(DLL),然后制作B.新朋友,因为我通常有一个家庭。派生类。在C,D,E,F和G
也从A继承,并且A.CreateInstance能够基于参数创建其中任何一个,
。类库代表了这个系列。

当然,类库中的其他类将能够创建B
对象。


parent?
I normally put A & B into a Class Library (DLL), then make B.New Friend, as
I normally have a "family" of derived classes. In that C, D, E, F, and G
also inherit from A, and A.CreateInstance is able to create any one of them,
based on a parameter. The class library represents this family.

Of course other classes within the class library will be able to create B
objects.

公共类B :继承A

''只能从A中的CreateInstance方法调用。朋友Sub New()
Mybase.New()
End Sub
结束类
Public Class B : Inherits A

''Should only be callable from the CreateInstance method in A
Friend Sub New()
Mybase.New()
End Sub
End Class


我必须在A中声明类私有吗?
Must I declare the class Private within A?


这将是另一种选择,这将确保没有人可以创建B
对象!我只是注意到System.IO.Stream.Null属性是用嵌套类实现的。

希望这有帮助
Jay


Would be the other option, which would ensure that no one could create B
objects! I just noticed that the System.IO.Stream.Null property is
implemented in terms of a nested class.

Hope this helps
Jay




感谢Jay的回复。制作b。新朋友可以工作,但在我的情况下,为了避免误解,为了避免误解,如果没有其他类在类库中除了父级A之外没有其他类别更好,可以打电话给b.new,c.new

等。如果我宣布AI中所有A的孩子都会达到这个但是

那么问题就是班级的大小。有没有办法在A中声明

B为私有但是在另一个文件中提供实现?


最好的问候

John



Thanks for the reply Jay. Making b.New friend works but in my case, in
order to avoid misunderstandings, it would be better if no other class
in the class library, besides the parent A, could call b.new, c.new
etc. If I declare all children of A within A I would achieve this but
the problem then is the size of the class. Is there any way to declare
B as private within A but supply the implementation in another file?

Best Regards
John


" John" <菊*********** @ yahoo.se> schrieb
"John" <ju***********@yahoo.se> schrieb

感谢Jay的回复。制作b。新朋友工作,但在我的情况下,为了避免误解,如果除了父A之外,类库中没有其他类可以调用b.new,<如果我在AI中声明A的所有孩子都会实现这个


怎么样?无论是将派生类中的构造函数设为私有,还是使用
friend,protected或public都不会实现此目的。

但问题是类的大小。有没有办法在A中声明B为私有但是在另一个文件中提供实现?

Thanks for the reply Jay. Making b.New friend works but in my case,
in order to avoid misunderstandings, it would be better if no other
class in the class library, besides the parent A, could call b.new,
c.new etc. If I declare all children of A within A I would achieve
this
How? Neither making the constructor in the derived class private, nor
friend, protected, or public would achieve this.
but the problem then is the size of the class. Is there any way
to declare B as private within A but supply the implementation in
another file?




唯一的方法(IMO)是

- 在A

中声明一个公共接口 - 在A(私有类...)中声明派生类是私有的。

构造函数被声明为public。

- 在派生类中实现公共接口

- 在A返回对象中实现公共函数public

界面(..作为IMyInterface ......返回新B)。


只评论周杰伦的建议:它可能不适用于所有

情况,这意味着它不是这种设计模式的通用解决方案。

让我们有A,B和C类。要求:

- 只有A可以创建B对象

- 只有B可以创建C对象

一方面A和B必须在同一个组件,另一边B和C

必须在同一个组件中。 IMO还有其他标准来定义哪个

类放在同一个库中。

-

Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote .html



The only way (IMO) is
- declare a public Interface within A
- declare the derived classes private within A ("Private Class..."). The
constructor is declared public.
- implement the public Interface in the derived classes
- Have the public function within A return objects implementing the public
Interface ("..As IMyInterface......Return New B").

Only a comment to Jay''s suggestion: It might not be appliable in all
situations which means it is not a general solution for this design pattern.
Let''s have classes A, B and C. Requirements:
- Only A can create B objects
- Only B can create C objects
On one side A and B must be in the same assembly, on the other side B and C
must be in the same assembly. IMO there are other criteria defining which
classes to place in the same library.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


这篇关于继承和创造孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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