什么是“最佳实践”

编程入门 行业动态 更新时间:2024-10-26 16:32:42
本文介绍了什么是“最佳实践” - 在构造函数中实例化或在集合类外部实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题:myList是否相同(工作原理相同)是否 在(普通)构造函数中实例化,如下所示,以及我的 标准练习,还是在外面? RL 公共类AClass { int j; // public List< NodemyList = new List< Node>(); //更安全 这里? public List< NodemyList; //定义;正常的声明 构造函数 public AClass() { j = 0; myList = new List< Node>(); } }

解决方案

也许你可以创建私有的mylist然后你可以设置并获得 来自的mylist类型数据您可以通过这种方式将其设置为只读或 writeonly。它可以更安全。 Hakan Fatih YILDIRIM MCP

8月12日上午12:14,raylopez99< raylope ... @ yahoowrote: [...]是相同的[...]?

不,在myList初始化之后,基本构造函数(在您的情况下,object())将被调用 你在声明中初始化它, ,反之亦然。如果你有复杂的 继承,这可能会导致并发症。这种情况下的最佳实践可能是在构造函数中初始化 ,因为编译器无论如何都会在那里移动初始化 ,除非你希望一个类可以访问变量 在派生类中初始化。

您好UL-Tomten。

不,如果在声明中初始化myList,则在myList初始化之后,将调用基本构造函数(在您的情况下,object()) , ,反之亦然。如果你有复杂的 继承,这可能会导致并发症。

你是否介意详细说明你的意思如果你有复杂的继承,这可能导致 并发症。 ? 我对你的评论有点困惑的原因是因为据我所知,基础课对这些课程一无所知继承 形成它们,所以如果他们对它们一无所知,怎么能修改它们的任何变量? 我认为某些事情可能出错的唯一方法是,如果基础 类具有被继承者覆盖的虚拟方法,从而具有 能力以这种方式改变变量值,但据我所知,你不能从构造函数中调用虚拟方法。 提前谢谢。

Question: the "myList" below is the same (works the same) whether instantiated inside the (normal) constructor, as below, and as my standard practice, or outside? RL public class AClass { int j; //public List<NodemyList = new List<Node>(); // less safe here? public List<NodemyList; //definition; declaration in normal constructor public AClass() { j =0; myList = new List<Node>(); } }

解决方案

Hi, Maybe you can create mylist with private and then you can set and get the mylist type data from property.You can set it read only or writeonly by this way.It can be safier. Hakan Fatih YILDIRIM MCP

On Aug 12, 12:14 am, raylopez99 <raylope...@yahoowrote:

[...] is the same [...]?

No, the base constructor (in your case, "object()") will be called after myList is initialized if you initialize it in the declaration, and vice versa. This may cause complications if you have complex inheritance. The best practice in this case is probably to initialize in the constructor, since the compiler will move the initialization there anyway, unless you want a class to have access to variables initialized in a derived class.

Hi UL-Tomten.

No, the base constructor (in your case, "object()") will be called after myList is initialized if you initialize it in the declaration, and vice versa. This may cause complications if you have complex inheritance.

Would you mind elaborating a little more on what you mean by "This may cause complications if you have complex inheritance."? The reason I am a little confused about you comment is because as far as I know, base classes don''t know anything about the classes that are inheriting form them, so if they don''t know anything about them, how can they modify any of their variables? The only way that I can think that something could go wrong is if the base class has virtual methods that are overridden by inheritors thus having the capability of changing variable values that way, but as far as I know, you can''t call virtual methods from constructors. Thanks in advance.

更多推荐

什么是“最佳实践”

本文发布于:2023-11-04 10:12:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!