2016-12-05 93 views
-1

我可以调用构造函数吗? 这是我的课c#调用构造函数通过赋值

public class Person 
{ 
public string sName; 
public string sAge; 

public Person (string sPerson) 
{ 
//in sPerson i have Name and Age separate from , 
sName=sPerson.split(',')[0]; 
sAge=sPerson.split(',')[1]; 
} 

在主代码,我需要我的稳定物价的对象,但我不能调用构造函数,有一个方法来调用构造函数也是这样吗?

Person myObject = newPerson(); 
myPerson = sPeson; 

}

+0

你的意思是'人myObject = myPerson;'? – RandomStranger

+0

'newPerson'是一个错字('新人')?没有无参数的构造函数。什么是'佩森'和'myPerson'?笏*确定我的目标是否意味着? –

回答

0

是的,你可以提供一个copy-constructor是另需Person

public Person (Person otherPerson) 
{ 
    sName = otherPerson.sName; 
    sAge = otherPerson.sAge; 
} 

你可以把它用这种方式:

Person myObject = new Person(sPerson);