Custom class property and method attributes

조회 수: 4 (최근 30일)
Thomas Satterly
Thomas Satterly 2019년 9월 10일
댓글: Thomas Satterly 2019년 11월 4일
I'd like to be able to define my own attributes in classes, but I'm not finding any documentation as to how that might be done. My main goal is to be able to create a property attribute, "UseAsReference", that marks object properties that need to be saved and loaded via custom methods because they are references to other Matlab handle objects. I'm hoping to accomplish this with property attributes and a superclass rather than defining an interface so that I don't have to worry about maintaining a constant list of reference properties and custom save/load functions for each implementing member. The pages for property and method attributes note that inheriting from certian frameworks (e.g., the UnitTest framework) will open up more attributes that a class can use. Is it possible to replicate this functionality?
  댓글 수: 2
Adam
Adam 2019년 11월 4일
I'm not entirely sure what it is you are wanting, and didn't notice the question at the time, though as you seem to have found an answer now I guess it doesn't matter. I'd have thought dependent properties could have been utilised to achieve this though.
I did implement my own class called a JustInTimeProperty to store certain data in my classes too as I have cases where I want the property to only be calculated when first asked for, but then stored thereafter, rather than a pure dependent property which would always recalculate or a regular property which would have to be calculated upfront even if it is never actually asked for.
I can't remember the exact implementation i had for these and it isn't the same scenario you want anyway, but their usage did make use of dependent properties, paired with private properties.
Thomas Satterly
Thomas Satterly 2019년 11월 4일
I did a poor job wording what I wanted. Basically, I wanted a class property that, when saved, would save some key attribute of the object referenced by the property rather than the property itself. Then, upon loading, the attribute would br translated back into the originally saved object (which would have been saved and loaded separately). I have an application with highly interconnected objects that store multiple references to each other, and there were issues of duplicate object creation when saving and loading with the default methods because the save and load processes are not aware of the entire context that each object lives in.

댓글을 달려면 로그인하십시오.

채택된 답변

Thomas Satterly
Thomas Satterly 2019년 11월 4일
So Matlab doesn't support this type of thing, which can't really be too surprising. The best way I've found to get around this is to just redefine saveobj() and loadobj() methods, while having all "special" data stored in a dedicated struct that is purpose built for properties that can't be saved normally. For example, if is a property that reference another object, but you just want to save a "reference" (like through a UID), make the property transient, and saveobj() and loadobj() are then responsible for saving the UID to the dedicated save struct and attempting to find the old reference through the saved UID when loading.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by