用与对象类型相同的getter/setter命名另一个对象的属性的对象 [英] Naming an object that is an attribute of another object with the same getter/setter as Object Type

查看:82
本文介绍了用与对象类型相同的getter/setter命名另一个对象的属性的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个基本问题.将对象用作对象的属性时,所以Object2具有属性Object1.

This is a fundamental question. When using objects as an attribute for an object, So Object2 has an attribute Object1.

您可以使用对象名称作为获取器和设置器或第二个对象的类型和名称.

Can you use the Object name as the type and name for the getter and setter or the second object.

Class Object1{}

Class Object2
{
    Object1 Object1 {get; set;}
    // OR
    Object1 Object1_{get; set;}
}

因为我已经尝试过使用相同的名称来表示简单的程序,并且它可以工作(尽管我不敢冒险使用它来编写复杂的代码).我知道可能会有歧义,但这对任何项目都有效吗?

As I've tried it using the same name for simple programs and it works (though I've not dare to venture into complex code with it). I understand that there could be ambiguity, but will this actually work for any project?

推荐答案

是的,您可以这样做.实际上,这是C#中非常常见的模式

Yes, you can do it. In fact this is a very common pattern in C#

class Foo
{
   Bar Bar {get; set; }
}

这篇关于用与对象类型相同的getter/setter命名另一个对象的属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆