Matlab 版 (精华区)
发信人: ramjet (史努比狗), 信区: Matlab
标 题: invoking methods on java objects
发信站: 哈工大紫丁香 (2002年03月03日11:39:30 星期天), 站内信件
Invoking Methods on Java Objects
This section explains how to invoke an object's methods in MATLAB. It also
covers how to obtain information
related to the methods that you're using and how MATLAB handles certain types
of nonstandard situations.
This section addresses the following topics:
Using Java and MATLAB Calling Syntax
Invoking Static Methods on Java Classes
Obtaining Information About Methods
Java Methods That Affect MATLAB Commands
How MATLAB Handles Undefined Methods
How MATLAB Handles Java Exceptions
-----------------------------------------------------------------
Using Java and MATLAB Calling Syntax
To call methods on Java objects, you can use the Java syntax
object.method(arg1,...,argn)
In the following example, frame is the java.awt.Frame object created above
, and getTitle and setTitle are
methods of that object.
frame.setTitle('Sample Frame')
title = frame.getTitle
title =
Sample Frame
Alternatively, you can call Java object (nonstatic) methods with the MATLAB
syntax
method(object, arg1,...,argn)
With MATLAB syntax, the java.awt.Frame example above becomes
setTitle(frame, 'Sample Frame')
title = getTitle(frame)
title =
Sample Frame
All of the programming examples in this chapter contain invocations of Java
object methods. For example, the
code for Reading a URL contains a call, using MATLAB syntax, to the openStream
method on a java.net.URL
object, url.
is = openStream(url)
In another example, the code for Example - Creating and Using a Phone Book
contains a call, using Java syntax,
to the load method on a java.utils.Properties object, pb_htable.
pb_htable.load(FIS);
Using the javaMethod Function on Nonstatic Methods
Under certain circumstances, you may need to use the javaMethod function
to call a Java method. The following
syntax invokes the method, method_name, on Java object J with the argument
list that matches x1,...,xn. This
returns the value X.
X = javaMethod('method_name',J,x1,...,xn);
For example, to call the startsWith method on a java.lang.String object passing
one argument, use
gAddress = java.lang.String('Four score and seven years ago');
str = java.lang.String('Four score');
javaMethod('startsWith', gAddress, str)
ans =
1
Using the javaMethod function enables you to:
Use methods having names longer than 31 characters
Specify the method you want to invoke at run-time, for example, as input
from an application user
The only way to invoke a method whose name is longer than 31 characters is
to use javaMethod. The Java and
MATLAB calling syntax does not accept method names of this length.
With javaMethod, you can also specify the method to be invoked at run-time
. In this situation, your code calls
javaMethod with a string variable in place of the method name argument. When
you use javaMethod to invoke a
static method, you can also use a string variable in place of the class name
argument.
--
在这个时代
如果你要为社会做一点贡献的话
那么就想办法
让你和你爱的人
好好的活着
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.237.3]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:6.177毫秒