Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-14, 20:13   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 Matlab代理类问题。 (关于:依赖属性)

考虑以下Matlab(2009a)类:

classdef BigDeal < handle properties hugeness = magic(2000); end methods function self = BigDeal() end end end classdef BigProxy < handle properties(Dependent) hugeness end properties(Access = private) bigDeal end methods function self = BigProxy(bigDeal) self.bigDeal = bigDeal; end function value = get.hugeness(self) value = self.bigDeal.hugeness; end end end 现在考虑对它们的以下用法:

设定:

>> b = BigDeal b = BigDeal handle Properties: hugeness: [10x10 double] Methods, Events, Superclasses 单层:

>> pb = BigProxy(b) pb = BigProxy handle Properties: hugeness: [10x10 double] Methods, Events, Superclasses 双层:

>> ppb = BigProxy(pb) ppb = BigProxy handle with no properties. Methods, Events, Superclasses 问题:为什么我的双层代理在单层可以看到的时候看不到hugeness的空间?可以计算相关属性-但这是否由于某种原因仅深入了一层?

更新:请参阅以下我的答案以找到解决方法。



回答:

这里的问题有两个:
  1. 所述BigProxy对象构造被设计成接受具有(非依赖型)的输入对象hugeness属性(如BigDeal对象),则其BigProxy对象可以访问然后来计算其自己的依赖于值hugeness属性。
  2. 创建ppb ,您正在将BigProxy对象传递给BigProxy构造函数,您显然无法从另一个从属属性计算一个从属属性。例如,这是我尝试访问ppb.hugeness时引发的错误:

    ??? In class 'BigProxy', the get method for Dependent property 'hugeness' attempts to access the stored property value. Dependent properties don't store a value and can't be accessed from their get method. 换句话说,外BigProxy对象尝试来计算其依赖于值hugeness通过访问所存储的值属性hugeness用于内BigProxy对象,但不存在存储的值,因为它是一个依赖特性
我认为针对这种情况的解决方法是让BigProxy构造函数检查其输入参数的类型,以确保它是BigDeal对象,否则抛出错误。



更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 23:26


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.