What is an import list?

1 次查看(过去 30 天)
Stephen
Stephen 2016-7-14
Why would I want to add packages or classes to an import list?

回答(1 个)

Sean de Wolski
Sean de Wolski 2016-7-14
编辑:Sean de Wolski 2016-7-14
So you can use the shortname
pack1.pack2.file(pi)
or
import pack1.pack2.*
file(pi)
I tend to do the import if I need more than one file in a package or need to reference one file multiple times.
  4 个评论
Steven Lord
Steven Lord 2016-7-15
I don't think you ever need to import a package or Java name, but it can be convenient to do so.
As to why you don't need to import to use MathWorks functions, most MathWorks functions are not in a package. Many MathWorks function predate the introduction of packages, in fact. There are some exceptions; for instance, in the unit testing framework there are packages like matlab.unittest.constraints or matlab.unittest.fixtures.
While you could call matlab.unittest.constraint.IsEqualTo every time you needed to use it, that's a lot of typing. Using import matlab.unittest.constraint.IsEqualTo and then simply calling IsEqualTo will save typing if you need to call IsEqualTo multiple times. [Of course, this is probably not a great example; usually I would call the verifyEqual method on the test object instead of calling the constraint directly. But that was just an example, it doesn't need to necessarily be realistic.]
Sean de Wolski
Sean de Wolski 2016-7-19
Another example is the mlreportgen.dom.* package. I will often import it so that I can use components all based on shortname (Text, Image, Document rather than mlreportgen.dom.Text, mlreportgen.dom.Image,...). This is a prime example of why things are in the package, image and text are already existing MATLAB functions but by putting them in the package the report generator ones can coexist with the base MATLAB ones.
As for your question Stephen, importing just means that you can use the shortname. That is all. It's not being imported anywhere, you can think of it as a flag if you want: useshortname.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by