[英]Create new instance of private class in another private class
Is it possible to create an instance of a private class in another private class? 是否可以在另一个私有类中创建一个私有类的实例? (Not counting within the main() program.) And also, is it possible for a method in a private class to return a private type object?
(不计入main()程序中。)而且,私有类中的方法是否可能返回私有类型对象?
This question came because I was following Scott Allen from PluralSight on C# Fundamentals With C#5. 之所以问这个问题,是因为我在C#5的C#基础上关注PluralSight的Scott Allen。 And on lesson 2 about classes and objects, he has a code example like this:
在关于类和对象的第二课中,他有一个如下代码示例:
public GradeStatistics ComputeStatistics()
{
GradeStatistics stats = new GradeStatistics();
...
...
}
with GradeStatistics defined in a separate class file like: 在单独的类文件中定义的GradeStatistics如下:
class GradeStatisticss
{
}
Inlined comment: I am not talking about nested classes. 内联评论:我不是在谈论嵌套类。 What I meant is, you have two classes (separate files) and I am wondering if one class can create an instance of another class (knowing they are both private).
我的意思是,您有两个类(单独的文件),我想知道一个类是否可以创建另一个类的实例(知道它们都是私有的)。
Edited with examples:
private class Example1
{
}
private class Example2
{
public Example1 DoSomeComputation()
{
return new Example1();
}
}
private class Example3
{
Example1 ex1 = new Example1();
}
Is Example3 able to create ex1? Can Example2 return a new instance of Example1?
Is it possible to create an instance of a private class in another private class?
是否可以在另一个私有类中创建一个私有类的实例?
Only if the private class for which you want to create an instance is declared inside the private class that wants to create the instance. 仅在要创建实例的私有类中声明了要为其创建实例的私有类时。 If they are not nested, it's not possible.
如果未嵌套,则不可能。
Is it possible for a method in a private class to return a private type object?
私有类中的方法是否可以返回私有类型对象?
Yes, it can. 是的,它可以。
Here's some code showing everything together: 这是一些将所有内容一起显示的代码:
public class Tester {
private class ThePrivateCreator {
private class TheOtherPrivateClass {
}
public Object createObject() {
return new TheOtherPrivateClass();
}
}
public void canWeDoThis() {
ThePrivateCreator c = new ThePrivateCreator();
Console.WriteLine(c.createObject());
}
}
class Program
{
public static void Main(string[] args) {
Tester t = new Tester();
t.canWeDoThis();
}
}
No. A private class cannot be accessed by another class in a different file. 不能。私有类不能被另一个文件中的另一个类访问。 The reason why is that the modifier private is meant to encapsulate the data or method inside of that class.
其原因是修饰符private旨在将数据或方法封装在该类内部。 You should use the public or internal modifier if you want to access a class from a different class that is not nested.
如果要从另一个未嵌套的类访问一个类,则应使用public或internal修饰符。 If it is nested, you can also use the protected modifier.
如果是嵌套的,则还可以使用protected修饰符。
Not sure exactly what you had in mind, but here's one possible example: 不确定您的初衷是什么,但这是一个可能的示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication26
{
class Program
{
static void Main(string[] args)
{
private1 p1 = new private1();
private2 p2 = p1.foo();
Console.WriteLine(p2.Value);
Console.ReadLine();
}
private class private1
{
public private2 foo()
{
private2 p2 = new private2("I was created inside a different private class!");
return p2;
}
}
private class private2
{
private string _value;
public string Value
{
get { return _value; }
}
public private2(string value)
{
this._value = value;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.