本文介绍了访问其他类中的私有构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想访问其他类中的私有构造函数。可以通过链接构造函数来实现。我尝试使用代码,但不允许这样做。
在下面的代码中我在类中声明变量k1 = 1然后在私有构造函数k1 = 2中更改它。当我创建一个参数化和公共的其他构造函数的对象时。我打电话给它。但私有构造函数不会被调用。
请帮忙。
我尝试了什么:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
命名空间 ConsoleApplication2
{
class Program
{
int k1 = 1 ;
private 计划()
{
k1 = 2 ;
}
public 程序( int k )
{
k = k1;
}
public void ftr()
{
Console.WriteLine(k1);
Console.ReadKey();
}
静态 void Main( string [] args)
{
gtr g = new gtr();
g.main();
}
}
class gtr
{
程序p = new 程序( 1 );
public void main()
{
p.ftr();
}
}
}
解决方案
I want to access private constructor in other class. Can it is possible through chaining of constructor. I tried following code for it but it is not allowed.
in following code i declared variable k1=1 in class then change it in private constructor k1=2. when i create a object of other constructor which is parameterized and public also. I called it. but private constructor is not called.
please help.
What I have tried:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { int k1 = 1; private Program() { k1 = 2; } public Program(int k) { k = k1; } public void ftr() { Console.WriteLine(k1); Console.ReadKey(); } static void Main(string[] args) { gtr g = new gtr(); g.main(); } } class gtr { Program p=new Program(1); public void main() { p.ftr(); } } }
解决方案
这篇关于访问其他类中的私有构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!