类中集合的 getter/setter 等价物

     2023-02-25     183

关键词:

【中文标题】类中集合的 getter/setter 等价物【英文标题】:Equivalent of getters/setters for collections within a class 【发布时间】:2012-01-24 09:37:52 【问题描述】:

我有一堂课如下:

public class Document

    public List<DocumentSection> sections = new List<DocumentSection>();
    ...

各种问题涵盖属性需要在类内部可写但从外部只读的情况(http://***.com/questions/4662180/c-sharp-public-变量-as-writeable-inside-the-clas-but-readonly-outside-the-cl)

我想对这个集合做同样的事情 - 允许从类内添加到它,但只允许用户在它外面时迭代它。这优雅可行吗?

谢谢

【问题讨论】:

【参考方案1】:

将集合公开为IEnumerable,以便用户只能遍历它。

public class Document 
   private List<DocumentSection> sections;

   public IEnumerable<DocumentSection> Sections 
    
       get  return sections; 
   

【讨论】:

【参考方案2】:

是的,您必须隐藏 List 并且只公开 Add 方法和 IEnumerable&lt;DocumentSection&gt; 类型的属性:

public class Document

    private List<DocumentSection> sections = new List<DocumentSection>();

    public void AddSection(DocumentSection section) 
        sections.Add(section);
    

    public IEnumerable<DocumentSection> Sections 
        get  return sections; 
    

【讨论】:

感谢一月,非常感谢。【参考方案3】:

您可以将列表公开为IEnumerable&lt;DocumentSection&gt;,并且仅在内部使用List。像这样:

public class Document 
  public IEnumerable<DocumentSection> Sections  get  return list;  
  private List<DocumentSection> list;

【讨论】:

【参考方案4】:

如果你真的想只允许迭代,你可以保持 IList 私有,但创建一个解析为 GetEnumerator() 的公共函数

【讨论】:

【参考方案5】:
public class Document 
   private readonly List<DocumentSection> sections = new List<DocumentSection>();

   public IEnumerable<DocumentSection> Sections 
    
       get 
        
           lock (this.sections)
           
               return sections.ToList(); 
           
       
   

【讨论】:

使用 getter/setter 在类之间传递值

】使用getter/setter在类之间传递值【英文标题】:Passingvaluesbetweenclassesusinggetters/setters【发布时间】:2016-11-2219:00:32【问题描述】:我在C++/JUCE项目中有3个类:KeyboardGUI、Chord和Audio。KeyboardGUI收到一个名为rootNote的int,需要将其提... 查看详情

为啥 getter/setter 在 vue typescript 类组件中无法正常工作

】为啥getter/setter在vuetypescript类组件中无法正常工作【英文标题】:whyaregetter/setternotworkingproperlyinvuetypescriptclasscomponent为什么getter/setter在vuetypescript类组件中无法正常工作【发布时间】:2021-06-3007:02:50【问题描述】:我是vue.js的... 查看详情

Boost python getter/setter 同名

】Boostpythongetter/setter同名【英文标题】:Boostpythongetter/setterwiththesamename【发布时间】:2017-05-2820:22:45【问题描述】:我正在用boost-python包装C++类,我想知道有没有比我现在做的更好的方法。问题在于这些类具有同名的getter/setter... 查看详情

java示例代码_实施';生成getter/setter';对于emacs中的Java类

java示例代码_实施';生成getter/setter';对于emacs中的Java类 查看详情

JSON 使用 getter/setter 对 ES6 类属性进行字符串化

】JSON使用getter/setter对ES6类属性进行字符串化【英文标题】:JSONstringifyES6classpropertywithgetter/setter【发布时间】:2017-06-2517:43:37【问题描述】:我有一个JavaScriptES6类,它的属性设置为set并使用get函数访问。它也是一个构造函数参... 查看详情

scalascala类

1、scala类 1.class关键字 2.var属性默认生成getter/setter方法 3.val属性默认生成getter方法 4.自定义getter/setter方法,property和proeprty_方法来表示getter和setter方法 5.主构造函数和辅构造函数的定义使用 6.使用@BeanProperty... 查看详情

dart中类的getter和setter(代码片段)

 Dart类Getters和SetterGetters和Setter(也称为访问器和更改器)允许程序分别初始化和检索类字段的值。使用get关键字定义getter或访问器。Setter或存取器是使用set关键字定义的。默认的getter/setter与每个类相关联。但是,可以通过显式... 查看详情

找不到getter/setter——没有安装lombok插件

项目pull后的问题:importlombok.Getter;importlombok.Setter;@Setter@GetterpublicclassConcreteEntityextendsEntity//具体属性一个Entity类使用lombok插件,找不到getter/setter1.lombok包已经导入2.排查发现是IDE没有安装lombok插件 查看详情

推荐用属性代替getter/setter(代码片段)

Java实体类有条不成文的规矩,Bean是一个getter/setter:classApple privateStringcolor; publicvoidsetColor(Stringcolor) this.color=color; publicStringgetColor() returncolor; 然后有人觉得getter/setter书写太烦了 查看详情

带有 Getter/Setter 的私有列表与公共列表

】带有Getter/Setter的私有列表与公共列表【英文标题】:PrivateListwithGetter/SettervsPublicList【发布时间】:2014-07-2121:57:04【问题描述】:在一个类中创建一个全局List并具有getter和setter方法会更好,还是将其公开会更好?Java的标准是... 查看详情

SQLAlchemy:声明性 Mixin 类中的 getter/setter

】SQLAlchemy:声明性Mixin类中的getter/setter【英文标题】:SQLAlchemy:getter/setterindeclarativeMixinclass【发布时间】:2011-04-2816:32:23【问题描述】:我正在尝试为我打算在我的数据库架构中使用的mixin类定义简单的getter/setter方法:fromsqlalche... 查看详情

是否有任何 Maven 插件可用于从 Jhipster 中删除生成类的 getter/setter 并添加 lombok [关闭]

】是否有任何Maven插件可用于从Jhipster中删除生成类的getter/setter并添加lombok[关闭]【英文标题】:IsthereanyMavenpluginsavailabletotoremovegetter/settersofgeneratedclassesfromJhipsterandaddlombok[closed]【发布时间】:2021-11-1107:56:50【问题描述】:jhipster... 查看详情

基于类的 vue 组件属性定义:构造函数 vs.getter/setter vs.mounted 生命周期

】基于类的vue组件属性定义:构造函数vs.getter/settervs.mounted生命周期【英文标题】:Classbasedvuecomponentpropertydefinition:constructorvs.getter/settervs.mountedlifecycle【发布时间】:2019-07-2917:25:06【问题描述】:我只是想知道哪种方式定义属性... 查看详情

Java 9中集合的重载便利工厂方法有啥意义

】Java9中集合的重载便利工厂方法有啥意义【英文标题】:WhatisthepointofoverloadedConvenienceFactoryMethodsforCollectionsinJava9Java9中集合的重载便利工厂方法有什么意义【发布时间】:2017-06-1414:36:11【问题描述】:Java9附带conveniencefactorymethod... 查看详情

kotlin类与对象①(成员属性|kotlin自动为成员字段生成getter和setter方法|手动设置成员的getter和setter方法|计算属性)(代码片段)

文章目录一、Kotlin自动为成员字段生成getter和setter方法二、手动设置成员的getter和setter方法三、计算属性一、Kotlin自动为成员字段生成getter和setter方法定义Kotlin类,在类中定义成员属性,会自动生成getter和setter方法;在Kotlin中定义如... 查看详情

javascript如何删除mongodb中集合的所有文档?(代码片段)

查看详情

scala_类和对象(代码片段)

...面向JVM的类时候,会生成一个私有字段name和对应的公有getter、setter方法,getter,setter方法分别叫做name,name_7privatevarsum=089//val修飾的只生成getter方法10valname=""1112//将私有字段的getter和seeter改成公有方法13privatevarprivateHeigth=0.014//重新定... 查看详情

为 Symfony 2 中集合的每个项目指定不同的验证组?

】为Symfony2中集合的每个项目指定不同的验证组?【英文标题】:SpecifydifferentvalidationgroupsforeachitemofacollectioninSymfony2?【发布时间】:2014-02-1203:23:04【问题描述】:[Documentationaboutcollection]当嵌入表单(集合类型)可以根据当前项目... 查看详情