我来自VB,是C的新手。我有2个课程:
public class Location {
public decimal lat {get;set;}
public decimal lon {get;set;}
}
public class credentials {
public string username {get;set;}
public string passwordhash {get;set;}
}
现在,我想以某种方式在其他一些类中使用这两个类,新类如下所示:
public class something1 {
public string property1 {get;set;}
public string property2 {get;set;}
// next 2 lines should be avoided -> class "Location"
public decimal lat {get;set;}
public decimal lon {get;set;}
// next 2 lines should be avoided -> class "credentials"
public string username {get;set;}
public string passwordhash {get;set;}
}
我怎么能意识到这一点?我不熟悉所有的面向对象的东西。感谢帮助。
简单:
public class something1 {
public string property1 {get;set;}
public string property2 {get;set;}
public Location property3 {get;set;}
public credentials property4 {get;set}
}
注意:
string
另一类完全相同。using MyOtherNamespace
在文件中的类定义上方添加一个。要访问这些属性,您就像访问的实例上的任何其他属性一样something1
,例如
something1 mySomething1 = new Something1();
mySomething1.Location = new Location();
string cred = mySomething1.credentials.ToString(); // oops you may get a null ref because nothing has been assigned to mySomething1.credentials yet so it will be `null`;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句