我来自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;}
}
我怎么能意识到这一点?我不熟悉所有OO的东西。 谢谢你的帮助。
答案 0 :(得分:6)
易:
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`;