Flex DataGrid的dataField如何抓取当前遍历元素对象里的属性

Flex DataGrid的dataField如何抓取当前遍历元素对象里的属性,第1张

<mx:itemRenderer>

<mx:Component>

<mx:Text text="{datacardname}"/>

</mx:Component>

</mx:itemRenderer>

/如第二个回答所说,你应该多看jdk API文档的/

import javalangreflectField;

public class TestGetField {

public static void main(String[] args) {

try{

Student student = new Student(21,"banxi1988","boy");

Class sc = studentgetClass();

Field[] fieldArray = scgetFields();

Systemoutprintln("直接得到公开的属性");

Systemoutprintln("属性名\t属性类型\t\t属性值");

for(int i = 0; i<fieldArraylength;i++){

Field tmpField = fieldArray[i];

Systemoutprint(tmpFieldgetName()+"\t\t");

Systemoutprint(tmpFieldgetType()+"\t\t");

Systemoutprintln(tmpFieldget(student));

}

/通过反射构造新的对象 ,然后

Student refStudent =(Student)scnewInstance();

/

}catch(Exception e){

eprintStackTrace();

}

}

}

class Student{

private int age;

public String name;

private String gender;

public int getAge() {

return age;

}

public void setAge(int age) {

thisage = age;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

thisgender = gender;

}

public Student(int age, String name, String gender) {

super();

thisage = age;

thisname = name;

thisgender = gender;

}

}

用反射,代码你可以参考下面的:

public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{

Field[] field = modelgetClass()getDeclaredFields(); //获取实体类的所有属性,返回Field数组

for(int j=0 ; j<fieldlength ; j++){ //遍历所有属性

String name = field[j]getName(); //获取属性的名字

Systemoutprintln("attribute name:"+name);

String type = field[j]getGenericType()toString(); //获取属性的类型

if(typeequals("class javalangString")){ //如果type是类类型,则前面包含"class ",后面跟类名

Method m = modelgetClass()getMethod("get" + namesubstring(0,1)toUpperCase() + namesubstring(1) );

String value = (String) minvoke(model); //调用getter方法获取属性值

if(value != null){

Systemoutprintln("attribute value:"+value);

}

}

if(typeequals("class javalangInteger")){

Method m = modelgetClass()getMethod("get" + namesubstring(0,1)toUpperCase() + namesubstring(1) );

Integer value = (Integer) minvoke(model);

if(value != null){

Systemoutprintln("attribute value:"+value);

}

}

if(typeequals("class javalangShort")){

Method m = modelgetClass()getMethod("get" + namesubstring(0,1)toUpperCase() + namesubstring(1) );

Short value = (Short) minvoke(model);

if(value != null){

Systemoutprintln("attribute value:"+value); }

}

if(typeequals("class javalangDouble")){

Method m = modelgetClass()getMethod("get" + namesubstring(0,1)toUpperCase() + namesubstring(1) );

Double value = (Double) minvoke(model);

if(value != null){

Systemoutprintln("attribute value:"+value);

}

}

if(typeequals("class javalangBoolean")){

Method m = modelgetClass()getMethod("get"+name);

Boolean value = (Boolean) minvoke(model);

if(value != null){

Systemoutprintln("attribute value:"+value);

}

}

if(typeequals("class javautilDate")){

Method m = modelgetClass()getMethod("get"+name);

Date value = (Date) minvoke(model);

if(value != null){

Systemoutprintln("attribute value:"+valuetoLocaleString());

}

}

}

以上就是关于Flex DataGrid的dataField如何抓取当前遍历元素对象里的属性全部的内容,包括:Flex DataGrid的dataField如何抓取当前遍历元素对象里的属性、怎么获得java bean中的属性名称、java 遍历 类属性的属性值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9526154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存