例如,我创建了一个student
,所有属性都为空
classdef student < handle
properties
name
test
end
methods
function obj = student(name)
if nargin==1
obj.name = name;
obj.test = test();
end
end
end
end
>> A=student
A =
student with properties:
name: []
test: []
>> isempty(A)
ans =
logical
0
但CCD_ 2的输出为假。那么,什么函数可以检查对象的所有属性是否为空呢?如何在数据库中找到这类对象?
您可以重载类的isempty
方法:
classdef student < handle
properties
name
test
end
methods
function obj = student(name)
if nargin==1
obj.name = name;
obj.test = test();
end
end
function res = isempty(obj)
res = isempty(obj.name) && isempty(obj.test);
end
end
end
现在:
>> A = student;
>> isempty(A)
ans =
logical
1
要在数据库中查找空对象,请遍历数据库中的元素并检查它们是否为空。
除了循环检查对象的属性是否全部为空之外,没有更多的选择:
function isE = check_emptyness(A)
props = properties(A);
isE = true;
for iprop = 1:length(props)
if ~isempty(A.(props{iprop}))
isE = false;
break
end
end
end
student_arr=[student('A') student()];
check_emptyness(student_arr(1))
ans=
逻辑
0
check_emptyness(student_arr(2))
ans=
逻辑
1