如何在同一个程序集中允许从另一个类方法实现类的实例化??? [英] How to allow instanciation of a class from another class method inside the same assembly???

查看:68
本文介绍了如何在同一个程序集中允许从另一个类方法实现类的实例化???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我希望能够允许从另一个类方法(类)中实现类(类Class_A)的实例化Class_B)。我想编写

必要的代码来强制执行这种行为,即使在定义了

class Class_A和Class_B的同一个程序集中。

我已经编写了代码来执行此操作但我想了解这是否是获得此类结果的最正确方法。


这是我的简化代码:


//抽象类

公共抽象类Class_C

{

public abstract void Method_1();

}


公共类Class_B

{

//返回类Class_A实例的方法

public Class_A Method_2()

{

返回新的Class_A();

}


//嵌套私有类 - 实现抽象类Class_C

私有类Class_A:Class_C

{

public override void Method_1

{

//实施代码

}

}

}

上述实现允许编写以下客户端。代码:


Class_B b = new Class_B();

Class_C c = b.Method_2(); //创建类Class_A的实例

c.Method_1(); //调用类Class_A的Method_1()


这段代码效果很好,我唯一不喜欢的是Class_A

完全默默无闻类Class_B之外的代码。

而不是抽象类我可以使用接口但是,因为我不需要多次实现该接口,所以我不需要实现该接口以为使用

抽象类来保持对类Class_A的实例的引用更为正确。


任何人都可以审查这种方法并告诉我是否有更好的解决方案?

Bob Rock

解决方案

< blockquote>嗨Bob,


好​​吧,你的代码没有编译:P(或者至少不适合我)。关于返回类型的东西比方法更难访问。但是,将Method_1的返回类型更改为Class_C可以解决问题,但我怀疑你已经这样做了。


还有另一种受控实例化的方法,只涉及一个类。 />

公共班Class_A

{

私人Class_A()

{

}


public void Method_1()

{

}


public static Class_A Method_2()

{

返回新的Class_A();

}


}


快乐的编码!

Morten Wennevik [C#MVP]


>嗨鲍勃,


好吧,你的代码没有编译:P(或者至少不适合我)。
关于返回类型比方法更难访问的东西。

但是,将Method_1的返回类型更改为Class_C就可以了,

但是我怀疑你已经这样做了。
还有另一种控制实例化方法,只涉及
一类。
公共类Class_A
{
私有Class_A()
{
}

public void Method_1()
{
}

public static Class_A Method_2()
{
返回新的Class_A();
}


快乐的编码!
Morten Wennevik [C#MVP]


Morten,


对不起,我不得不清理我的代码来发帖并犯了一个错误。

Method_1确实会返回Class_C的一个实例。


Morten感谢您的代码,但这不是我要求的。

我想上课方法(类Class_B方法)是唯一一个能够返回一个ANOTHER clas实例的
s(Class_A类)。

Bob Rock


正如Morton向您展示的那样,您可以防止课程被实例化

来自其类型之外,使其''类的构造私有。那个

基本上就是你想要的。请记住,您的C类需要公开

才能将该类型分配给引用指针。我知道你想要一个

级是唯一一个能够返回另一种类型的对象但是从我看到的有两个问题的



1.要强制继承一个类型,你会把它变成抽象的,但是通过制作它抽象的你不能实现其类型的对象(你需要它)

do)

2.通过隐藏构造函数从类型外部阻止继承

类,这会阻止你返回

的一个对象,使用基础对象从它的类外面使用它的类型


Morton方式让一个类型成为它自己的对象工厂是最好的通过这种方式来获取你所需要的东西(据我所知)。请参阅代码。


使用系统;


命名空间ConsoleApplication1

{

///< summary>

/// Class1的摘要说明。

///< / summary>

公共类ClassC

{

public void SayHello()

{

Console.WriteLine(" Hello"); < br $>
}


私有ClassC()

{

//构造函数只能在其自己的内部显示类型

}


公共静态ClassC ReturnC()

{

返回新的ClassC() ;

}

}


公共类AppStart

{

[STAThread]

static void Main(string [] args)

{

// ClassC c = new ClassC(); //会失败

//c.SayHello();

ClassC c = ClassC.ReturnC();

c.SayHello() ;

}

}

}


-


Br,

Mark Broadbent

mcdba,mcse + i

=============

" Bob Rock" <无*************************** @ hotmail.com>在消息中写道

news:uz ************** @ TK2MSFTNGP09.phx.gbl ...

嗨鲍勃,

嗯,你的代码不编译:P(或者至少不适合我)。关于返回类型的东西比方法更难访问。
然而,将Method_1的返回类型更改为Class_C将执行



技巧,但我怀疑你已经这样做了。


还有另一种受控实例化的方法,只有


涉及一个类。


公共类Class_A
{
私有Class_A()
{
}
公共空方法1()
{
}

public static Class_A Method_2()
{
返回新的Class_A();
}

}

快乐的编码!< Morten Wennevik [C#MVP]



Morten,

很抱歉,我不得不清理我的代码来制作帖子并制作一个错误。
Method_1确实返回了一个Class_C的实例。

Morten感谢你的代码,但这不是我要求的。
我想要一堂课方法(类Class_B方法)是onl是一个能够返回另一个类的实例(类Class_A)。

Bob Rock



Hello,

I''d like to be able to allow instanciation of a class (class Class_A) only
from another class method (class Class_B). And I''d like to write the
necessary code to enforce this behavior even inside the same assembly where
class Class_A and Class_B are defined.
I''ve written the code that does this BUT I''d like to understand if this is
the most correct way to obtain such a result.

This is my simplified code:

// abstract class
public abstract class Class_C
{
public abstract void Method_1();
}

public class Class_B
{
// method that returns instances of class Class_A
public Class_A Method_2()
{
return new Class_A();
}

// nested private class - implements the abstract class Class_C
private class Class_A : Class_C
{
public override void Method_1
{
// implementation code
}
}
}

The above implentation allows the writing of the following "client" code:

Class_B b = new Class_B();
Class_C c = b.Method_2(); // creating an instance of class Class_A
c.Method_1(); // calling Method_1() of class Class_A

This code works well, the only thing I don''t like is that Class_A is
completely unknown by code outside class Class_B.
Instead of an abstract class I could have used an interface but, since I
won''t need to implement that interface more than once, I thought using an
abstract class to hold a reference to an instance of class Class_A to be
more correct.

Can anyone review this approach and tell me if there is any better solution?
Bob Rock


解决方案

Hi Bob,

Well, your code does not compile :P (or at least did not for me). Something about the return type being less accessible than the method. However, changing the return type for Method_1 to Class_C will do the trick, but I suspect you did so already.

There is another approach of controlled instantiation which only involves one class.

public class Class_A
{
private Class_A()
{
}

public void Method_1()
{
}

public static Class_A Method_2()
{
return new Class_A();
}

}

Happy coding!
Morten Wennevik [C# MVP]


> Hi Bob,


Well, your code does not compile :P (or at least did not for me). Something about the return type being less accessible than the method.
However, changing the return type for Method_1 to Class_C will do the trick,
but I suspect you did so already.
There is another approach of controlled instantiation which only involves one class.
public class Class_A
{
private Class_A()
{
}

public void Method_1()
{
}

public static Class_A Method_2()
{
return new Class_A();
}

}
Happy coding!
Morten Wennevik [C# MVP]


Morten,

I''m sorry, I had to clean up my code to make the post and made a mistake.
Method_1 indeed returns an instance of Class_C.

Morten thank you for your code but it is not what I was asking for.
I''d like a class method (class Class_B method) to be the only one able to
return instances of a ANOTHER class (class Class_A).
Bob Rock


As Morton has shown you, you can prevent a class from being instanciated
from outside its type by making it''s constructer private to its'' class. That
is essentially what you want. Remember that your class C needs to be public
in order to assign that type to the reference pointer. I know you want one
class to be the ONLY ONE to be able to return objects of another type but
from what I see there are 2 problems to this.

1. To force a type to be inherited you would make it abstract, but by making
it abstract you cannot instanciate objects of its type (which you need to
do)
2. By hiding the constructor from outside the type you are preventing the
class from being inherited, which prevents you from returning an object of
its type from outside its class using the base object

Morton way to have a type become it''s own object factory is the best way to
do what you what (as far as I can figure out). Please see code.

using System;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class ClassC
{
public void SayHello()
{
Console.WriteLine("Hello");
}

private ClassC()
{
//constructor is only visible within its own type
}

public static ClassC ReturnC()
{
return new ClassC();
}
}

public class AppStart
{
[STAThread]
static void Main(string[] args)
{
//ClassC c = new ClassC(); //would fail
//c.SayHello();
ClassC c = ClassC.ReturnC();
c.SayHello();
}
}
}

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Bob Rock" <no***************************@hotmail.com> wrote in message
news:uz**************@TK2MSFTNGP09.phx.gbl...

Hi Bob,

Well, your code does not compile :P (or at least did not for me). Something about the return type being less accessible than the method.
However, changing the return type for Method_1 to Class_C will do the


trick, but I suspect you did so already.


There is another approach of controlled instantiation which only

involves one class.


public class Class_A
{
private Class_A()
{
}

public void Method_1()
{
}

public static Class_A Method_2()
{
return new Class_A();
}

}
Happy coding!
Morten Wennevik [C# MVP]


Morten,

I''m sorry, I had to clean up my code to make the post and made a mistake.
Method_1 indeed returns an instance of Class_C.

Morten thank you for your code but it is not what I was asking for.
I''d like a class method (class Class_B method) to be the only one able to
return instances of a ANOTHER class (class Class_A).
Bob Rock



这篇关于如何在同一个程序集中允许从另一个类方法实现类的实例化???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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