您好。有人可以解释我的属性.Now of DateTime有没有其他属性?例如:DateTime.Now.Year 我想做同样的事情:通过另一个访问该属性。 谢谢! 我的尝试: 在Google中搜索,现在在CodeProject中搜索
解决方案属性现在返回对数据的引用宾语。这可以作为对象访问。 你只需要在属性getter中返回该类型的对象。 C#中的getter和setter 的一些解释和更复杂的示例。
这只是因为该属性返回一个对象,所以你可以然后访问它返回的对象的属性。 公共类数据 { public int ID {get;组; } public string Name {get;组; } } 公共类MyClass {公共数据数据{get;组; } public MyClass() { this.Data = new Data(); } }
MyClass c = new MyClass(); c.Data.ID = 1;
在上面,Data属性是对象的属性,如果你使它静态然后它是该类型的属性,并模拟DateTime.Now更好
公共类MyClass { public static Data Data { get { return new Data(); } } }MyClass.Data.ID = 1;
喜欢这个 公共类MyDateTime { public static MyDateTime My_Now {get ; } public int My_Year {get; } }
int year = MyDateTime.My_Now.My_Year;
通过这些链接 C#中面向对象编程概念的介绍 [ ^ ] 属性 - {get;组; } - Stack Overflow [ ^ ] 使用属性(C#编程指南)| Microsoft Docs [ ^ ] static(C#Reference)| Microsoft Docs [ ^ ]
Hi. Can someone explain me how the property .Now of DateTime has another properties in it ? For example: DateTime.Now.Year I want to make the same thing: Accessing the property through the another one. Thanks! What I have tried: Searching in Google and now asking here in CodeProject
解决方案 The property Now is returning the reference to an data object. That can be accessed as objects to. You only must return an object of that type in the property getter. Some explanation and more complex examples to getters and setters in C#.It's simply because the property returns an object so you can then access the properties of the object it returns. public class Data { public int ID { get; set; } public string Name { get; set; } } public class MyClass { public Data Data { get; set; } public MyClass() { this.Data = new Data(); } }MyClass c = new MyClass(); c.Data.ID = 1;
In the above the Data property is a property of the object, if you make it static then it is a property of the type and that emulates DateTime.Now better
public class MyClass { public static Data Data { get { return new Data(); } } }MyClass.Data.ID = 1;like this public class MyDateTime { public static MyDateTime My_Now { get; } public int My_Year { get; } } int year = MyDateTime.My_Now.My_Year;
go through these links Introduction to Object Oriented Programming Concepts in C#[^] properties - { get; set; } - Stack Overflow[^] Using Properties (C# Programming Guide) | Microsoft Docs[^] static (C# Reference) | Microsoft Docs[^]
更多推荐
[C#]财产的属性?
发布评论