Class 程序集的实例化 ("new") 问题和 类
Class instantiation ("new") problems with assemblies and classes
我正在从事一个项目,该项目要求我仅通过 class 自己的程序集实例化 ("new") 一个 class,而不是其他程序集。这可能吗?
我尝试使用抽象,但这意味着我无法在任何地方实例化它。
示例:
以下到classes在同一个project/assembly.
public class Banana
{
public Banana()
{
...
}
}
public class Food.
{
public static Banana InstantiateBanana()
{
Banana banana = new Banana();//<=====How can you instantiate it inside the same assembly? I don't want other assemblies to instantiate it.
...
return banana;
}
}
我想通了!
public class Banana
{
private Banana()
{
...
}
public static Banana InstantiateBanana()
{
Banana banana = new Banana();//<=====How can you instantiate it inside this class? I don't want other assemblies to instantiate it.
...
return banana;
}
}
这应该有效。仅在其自己的 class 中实例化 Banana class,但不在其他 class 中实例化。我只需要一个私有构造函数和一个静态方法来实例化。
我正在从事一个项目,该项目要求我仅通过 class 自己的程序集实例化 ("new") 一个 class,而不是其他程序集。这可能吗?
我尝试使用抽象,但这意味着我无法在任何地方实例化它。
示例: 以下到classes在同一个project/assembly.
public class Banana
{
public Banana()
{
...
}
}
public class Food.
{
public static Banana InstantiateBanana()
{
Banana banana = new Banana();//<=====How can you instantiate it inside the same assembly? I don't want other assemblies to instantiate it.
...
return banana;
}
}
我想通了!
public class Banana
{
private Banana()
{
...
}
public static Banana InstantiateBanana()
{
Banana banana = new Banana();//<=====How can you instantiate it inside this class? I don't want other assemblies to instantiate it.
...
return banana;
}
}
这应该有效。仅在其自己的 class 中实例化 Banana class,但不在其他 class 中实例化。我只需要一个私有构造函数和一个静态方法来实例化。