lesson:2 处理对象
1.creating objects
一般情况下,创建一个对象用以下方法
rectangle r = new rectangle();
但如果你正在开发一个development tools,在运行之前或许不知道要生成对象的类。
所以要像下面这样来创建对象:
string classname;
// . . . load classname from the user interface
object o = new (classname); // wrong!
但以上是错误的。
正确的方法是使用类的反射特性:
1)using no-argument constructors
例如:
class classdefinition = class.forname(classname);//指定类的运行期实例
object = classdefinition.newinstance();//调用无参构造函数来生成指定类的实例。
2)using constructors that have arguments
这个技术要用到如下步骤:
a,创建一个class对象
b,创建一个constructor对象,getconstructor(class[] params)方法,参数是一个与构造方法相适合的class 数组.
c,在constructor对象上调用newinstance方法来生成一个对象,参数 是一个object数组与这个构造方法相配备。
例如:
import java.lang.reflect.*;
import java.awt.*;
class sampleinstance {
public static void main(string[] args) {
rectangle rectangle;
class rectangledefinition;
class[] intargsclass = new class[] {int.class, int.class};
integer height = new integer(12);
integer width = new integer(34);
object[] intargs = new object[] {height, width};
constructor intargsconstructor;
try {
//1.
rectangledefinition = class.forname("java.awt.rectangle");
//2.
intargsconstructor =
rectangledefinition.getconstructor(intargsclass);//找到指定的构造方法
//3.
rectangle =
(rectangle) createobject(intargsconstructor, intargs);//构造方法描述对象,object[]
} catch (classnotfoundexception e) {
system.out.println(e);
} catch (nosuchmethodexception e) {
system.out.println(e);
}
}
public static object createobject(constructor constructor,
object[] arguments) {
system.out.println ("constructor: " + constructor.tostring());
object object = null;
try {
object = constructor.newinstance(arguments);
system.out.println ("object: " + object.tostring());
return object;
} catch (instantiationexception e) {
system.out.println(e);
} catch (illegalaccessexception e) {
system.out.println(e);
} catch (illegalargumentexception e) {
system.out.println(e);
} catch (invocationtargetexception e) {
system.out.println(e);
}
return object;
}
}
2。getting field values
if you are writing a development tool such as a debugger, you must be able to obtain field values. this is a three-step process:
如果要作一个开发工具像debugger之类的,你必须能发现filed values,以下是三个步骤:
a.创建一个class对象
b.通过getfield 创建一个field对象
c.调用field.getxxx(object)方法(xxx是int,float等,如果是对象就省略;object是指实
例).
例如:
import java.lang.reflect.*;
import java.awt.*;
class sampleget {
public static void main(string[] args) {
rectangle r = new rectangle(100, 325);
printheight(r);
}
static void printheight(rectangle r) {
field heightfield;
integer heightvalue;
class c = r.getclass();
try {
heightfield = c.getfield("height");
heightvalue = (integer) heightfield.get(r);
system.out.println("height: " + heightvalue.tostring());
} catch (nosuchfieldexception e) {
system.out.println(e);
} catch (securityexception e) {
system.out.println(e);
} catch (illegalaccessexception e) {
system.out.println(e);
}
}
}
3。setting field values
a.创建一个class对象
b.通过getfield 创建一个field对象
c.调用field.set(object,withparam)方法(xxx是int,float等,如果是对象就省略;object是指实例,withparam指和这个字段相区配的字段。
import java.lang.reflect.*;
import java.awt.*;
class sampleset {
public static void main(string[] args) {
rectangle r = new rectangle(100, 20);
system.out.println("original: " + r.tostring());
modifywidth(r, new integer(300));
system.out.println("modified: " + r.tostring());
}
static void modifywidth(rectangle r, integer widthparam ) {
field widthfield;
integer widthvalue;
class c = r.getclass();
try {
widthfield = c.getfield("width");
widthfield.set(r, widthparam);
} catch (nosuchfieldexception e) {
system.out.println(e);
} catch (illegalaccessexception e) {
system.out.println(e);
}
}
}
4。调用指定的方法
a.创建一个class对象
b.创建一个方法对象method,getmethod(string methodname,class[])方法
c.调方法对象,method.invoke(object,object[]),两个参数,第一个是指调用方法所属于的对象,第二个是传递的值对象列表。
the sample program that follows shows you how to invoke a method dynamically. the program retrieves the method object for the string.concat method and then uses invoke to concatenate two string objects.
//
import java.lang.reflect.*;
class sampleinvoke {
public static void main(string[] args) {
string firstword = "hello "; //指定类的实例
string secondword = "everybody.";//变元
string bothwords = append(firstword, secondword);
system.out.println(bothwords);
}
public static string append(string firstword, string secondword) {
string result = null;
class c = string.class;
class[] parametertypes = new class[] {string.class};
method concatmethod;
object[] arguments = new object[] {secondword};
try {
concatmethod = c.getmethod("concat", parametertypes);//获取到方法对象
result = (string) concatmethod.invoke(firstword, arguments);//调用
} catch (nosuchmethodexception e) {
system.out.println(e);
} catch (illegalaccessexception e) {
system.out.println(e);
} catch (invocationtargetexception e) {
system.out.println(e);
}
return result;
}
}