MATLAB Object-Oriented Programming

(Joyce) #1

Import Classes


In this section...
“Syntax for Importing Classes” on page 6-30
“Import Package Functions” on page 6-30
“Package Function and Class Method Name Conflict” on page 6-31
“Clearing Import List” on page 6-31

Syntax for Importing Classes


Import classes into a function to simplify access to class members. For example, suppose
that there is a package that contains several classes and you need to use only one of these
classes in your function, or even just a static method from that class. Use the import
command to simplify code. Once you have imported the class, you do not need to
reference the package name:

function myFunc
import pkg.cls1
obj = cls1(arg,...); % call cls1 constructor
obj.Prop = cls1.staticMethod(arg,...); % call cls1 static method
end

Import all classes in a package using the syntax pkg.*:

function myFunc
import pkg.*
obj1 = cls1(arg,...); % call pkg.cls1 constructor
obj2 = cls2(arg,...); % call pkg.cls2 constructor
a = pkgFunction(); % call package function named pkgFunction
end

Import Package Functions


Use import to import package functions:

function myFunc
import pkg.pkfcn
pkfcn(arg,...); % call imported package function
end

6 Defining and Organizing Classes

Free download pdf