【问题标题】:Returning an object of a second class through the constructor of first class通过第一类的构造函数返回第二类的对象
【发布时间】:2012-02-27 22:38:08
【问题描述】:

我有一个节点类和一个树类。我已经定义了节点类以包含节点声明所需的属性,并且树类用于从节点形成树结构。虽然树结构是由节点形成的,但我在返回节点对象时遇到了问题。我的代码结构是:

classdef Node
properties 
    node_center;
    node_size;
end
methods
   function this = Node(center,size)
       this.node_center = center;
       this.node_size = size;
   end
end
end % end of class Node
classdef Tree < handle
methods
   function n = Tree(points,objects_in_tree)
      n = Node(center_of_points,size);
      n = insert_child(n,center,sizez);
   end
end

现在我得到的错误是: 在构造类“树”的实例时,构造函数必须保留返回对象的类。 我知道它发生的原因,但想知道解决方法。谢谢。

【问题讨论】:

    标签: oop matlab


    【解决方案1】:

    构造函数的返回值必须是创建的对象 - 没有办法绕过它。您可以创建另一个函数来返回您希望从中获取的其他值(如节点)。树构造完成后,调用该对象的访问器函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多