属性可以返回默认值(类似于某些内置 .NET 属性)吗?

发布于 2024-12-06 12:29:28 字数 376 浏览 0 评论 0原文

有没有办法让一个类可以返回默认类型而不指定属性?

如果我有一个具有一些属性的类,其中一个属性返回字符串值,其他属性返回其他类型,我可以执行以下操作吗?

Dim StudentGrade as new StudentGradeClass

调用并获取默认属性,

Dim CurrentGrade as string=StudentGrade

而不是这个

Dim CurrentGrade as string=StudentGrade.Current

我问这个问题的原因是当我的类具有其他类作为属性并且只想获取默认返回值时。

Is there a way to have a class that can return a default type without specifying the property?

If I had a class that had a few properties and one property returned a string value and others returned additional types, could I do the following?

Dim StudentGrade as new StudentGradeClass

Call and get default property,

Dim CurrentGrade as string=StudentGrade

instead of this

Dim CurrentGrade as string=StudentGrade.Current

The reason I am asking this is when I have classes that have other classes as properties and would like to just get a default return value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命比纸薄 2024-12-13 12:29:28

隐式转换运算符(VB 中的加宽转换运算符)基本上可以满足您的需求,但这实际上是可读性的障碍。我建议你不要这样做,除非你有一个非常非常好的理由。

参考资料:

An implicit conversion operator (Widening conversion operator in VB) will get you basically what you want, but this is really a hindrance to readability. I would advise you not to do it unless you have a really, really, really good reason.

References:

夜巴黎 2024-12-13 12:29:28

.NET 框架不支持您所说的“默认属性”。您可能会通过使用 隐式运算符来伪造它,但我建议这样做可能是一个坏主意。

The .NET framework has no support for what you refer to as a "default property." You could potentially fake it through the use of implicit operators, but I would suggest that it is probably a bad idea to do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文