Matlab OO 프로그래밍에서 크래시가 시작되어 다른 오브젝트의 관련 필드에 값을 설정하여 값을 설정하는 오브젝트에 대해 set 메소드를 작성하려고합니다.오브젝트 메소드 내에서의 자체 참조
classdef Person
properties
age;
sex;
priority; % net priority based on all adjustment values
adjustment; % personal adjustment value for each interest
family;
end
methods
function obj = set.sex(obj, value)
if value == 'm' || value == 'f'
obj.sex = value;
else
error('Sex must be m or f')
end
end
function obj = set.family(obj,value)
if class(value) == 'Family'
obj.family = value;
else
error('Family must be of type Family')
end
end
end
end
classdef Family
properties
husband;
wife;
children;
elders;
adjustment; % interest adjustment values
end
methods
function this = set.husband(this,person)
if class(person) == 'Person'
this.husband = person;
person.family = this;
else
error('Husband must be of type Person')
end
end
function this = set.wife(this,person)
if class(person) == 'Person'
this.wife = person;
person.family = this;
else
error('Wife must be of type Person')
end
end
end
end
그래서 내가 지금해야 할 것입니다 :
p = Person
f = Family
f.husband = p
그리고 가족들을 : 내가 좋아하는 것이 무엇
p = Person
f = Family
f.husband = p
p.family = f
자동 서로에 자신을 설정하는 가족과 사람입니다 .husband 함수는 p의 패밀리 값을 f로 설정합니다. 코드가 작동하지 않는 이유는 무엇입니까? 내가 말할 수있는 한, 나는 의견에서 제안되는 것을하고있다.
편집 : "이"와 "사람"이 올바른 유형의 개체라는 것을 주위를 어지럽히고 난 후. 궁극적으로 Matlab은 참조가 아니라 값으로 전달됩니다. 할 수있을 때 아무에게도 그 방법을 알지 못한다면 나는 스스로에게 대답 할 것이다.
이 Family' 클래스'에 setHusband'같은 setter 메소드 (p)를'사용을, 내부 설정 현재'Family' 객체의'husband' 속성과''p' 객체의'family' 속성을 설정하는 것입니다. – Bee
나는 똑같은 것을 시도했다. 코드를 보라. 불행히도 작동하지 않습니다. 위의 코드는 가족 값을 설정하지만 사람 값은 설정하지 않습니다. 왜 그런지에 대한 어떤 생각? – csleys
알겠습니다. 'Person'과'Family' 클래스를'handle' 객체로 만들면 참조 문제로 패스를 풀 수 있을까요? 그냥 클래스의 정의에'을 추가하십시오 : http://www.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html –
Bee