Class association has error

5 次查看(过去 30 天)
shawin
shawin 2016-11-23
编辑: Adam 2016-11-23
I have the code bellow:
Course.m
classdef Course < handle
properties
courseName
stdent
end
properties ( Access= private)
lastIDX=1
end
methods
function obj= Course(courseName, capacity)
obj.courseName= courseName
obj.stdent= cell(capacity,1)
end
function addStudent(obj,stdent)
if obj.lastIDX > numel(obj.stdent)
fprintf(2,'sorry class is full`an');
return
end
obj.stdent(obj.lastIDX)=stdent;
obj.lastIDX= obj.lastIDX +1;
end
function printClass(obj)
fprintf('course name = %s\n ', obj.courseName)
fprintf('Enroled =%d , capacity =%d\n', obj.lastIDX-1, length(obj.stdent));
for i =1:obj.lastIDX-1
fprintf('ID =%d, name = %s\n', i, obj.stdent{i}.name);
end
end
end
end
StudentTest1.m
classdef studentTest1 < handle
properties
name
end
methods
function obj=studentTest1(name)
if nargin > 0
obj.name=name;
end
end
function delete(obj)
fprintf('--Student Destructor: %s\n',obj.name);
end
end
end
I call the objects :
c = Course('AAA',3);
for i = 1 :4
name= fprintf('amro%d',i);
fprintf('Adding student :%s\n', name)
c.addStudent(name);
end
c.printClass()
clear c
but receiving error :
Error using Course
The specified superclass 'handle' contains a parse error, cannot be found on MATLAB's search path or is shadowed by another file with the same name.
Error in TestStudentCourse (line 6) c = Course('AI',3);
any help, please
  1 个评论
Adam
Adam 2016-11-23
编辑:Adam 2016-11-23
Please don't edit away the original question to turn it into a new one when fixing the first problem just leads to another one, it is very confusing and doesn't help any future people coming to the question and looking at the answer provided which no longer makes much sense, relative to the question.

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2016-11-23
Well, learn to read error messages. It's telling you that the superclass handel cannot be found. That's because you misspelled it, it should be handle.
classdef Course < handle
  5 个评论
Guillaume
Guillaume 2016-11-23
Learn to debug your program. The simplest way is to issue
dbstop if error.
then run the code. It will break into the debugger at the line where the error occurs. Look at what is in obj.stdent{i}. Most likely it's not a studentTest object. What is it?
Adam
Adam 2016-11-23
编辑:Adam 2016-11-23
I make very heavy use of
doc validateattributes
for the input arguments of all my public functions of classes to ensure that when something gets passed into the function I know exactly what it is (for ease of debugging purposes, but also for documentation) and if I pass the wrong thing in it will give an error with an error message that tells me exactly this rather than sliding down through the code to some potentially obscure error when it actually tries to use the object.
It obviously adds a small amount to your development time and the runtime, but neither is significant to me compared to the value it gives me for debugging and coming back to a class 6 months down the line when I can't remember if theat 2nd input argument was supposed to be a string or a scalar or some object or other of my own class, etc.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by