gpt4 book ai didi

Java方法描述了任何对象需要改进的所有字段

转载 作者:行者123 更新时间:2023-12-01 11:06:19 25 4
gpt4 key购买 nike

同事们,这是我的方法,它描述了任何对象的所有属性(字段):

public static void describeObjectFields(Object o) {

System.out.println("Object description: " + o.getClass().getCanonicalName() + ": ");

for (Field field: o.getClass().getDeclaredFields()) {

field.setAccessible(true);
String name = field.getName();

Object value;
try {
value = field.get(o);
/*
if (value.toString().contains("com.comp."))
{
LOG.info("There is an Object in the described class " + value.getClass().getCanonicalName() );
System.out.println(value.getClass().getFields().toString() );
for (Field field1 : value.getClass().getDeclaredFields()) {
field1.setAccessible(true);
String name1 = field1.getName();
Object value1 = null;
value1 = field1.get(value1);
System.out.println(name1 + " -> " + value1);
}

} else {
*/
System.out.println(name + " -> " + value);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();

} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

它可以很好地处理原始类型(字符串、数据、整数...)任何人都可以改进此方法以打印不是原始类型而是复杂类型(当对象字段之一也是对象时)。

更新

根据 @Jon Skeet 的建议,让程序看起来像这样

package App;

public class Kitchen {


double sqr;
int windows;


public Kitchen(float sqr, int windows) {
this.sqr = sqr;
this.windows = windows;
}


public double getSqr() {
return sqr;
}

public void setSqr(double sqr) {
this.sqr = sqr;
}

public int getWindows() {
return windows;
}

public void setWindows(int windows) {
this.windows = windows;
}
}


package App;
public class Flat {

Kitchen kitchen;

double sqr;
int windows;
int rooms;


public Flat(Kitchen kitchen, double sqr, int windows, int rooms) {
this.kitchen = kitchen;
this.sqr = sqr;
this.windows = windows;
this.rooms = rooms;
}

public Kitchen getKitchen() {
return kitchen;
}

public void setKitchen(Kitchen kitchen) {
this.kitchen = kitchen;
}

public double getSqr() {
return sqr;
}

public void setSqr(float sqr) {
this.sqr = sqr;
}

public int getWindows() {
return windows;
}

public void setWindows(int windows) {
this.windows = windows;
}

public int getRooms() {
return rooms;
}

public void setRooms(int rooms) {
this.rooms = rooms;
}
}



package App;
import java.lang.reflect.Field;
public class App {


public static void main (String [] args)
{

Kitchen kit = new Kitchen(12, 1 );

Flat flat = new Flat(kit, 32.32, 5, 4);


App.describeObjectFields(flat);

}


public static void describeObjectFields(Object o) {

System.out.println("Object description: " + o.getClass().getCanonicalName() + ": ");

for (Field field: o.getClass().getDeclaredFields()) {

field.setAccessible(true);
String name = field.getName();

Object value;
try {
value = field.get(o);
/*
if (value.toString().contains("com.comp."))
{
LOG.info("There is an Object in the described class " + value.getClass().getCanonicalName() );
System.out.println(value.getClass().getFields().toString() );
for (Field field1 : value.getClass().getDeclaredFields()) {
field1.setAccessible(true);
String name1 = field1.getName();
Object value1 = null;
value1 = field1.get(value1);
System.out.println(name1 + " -> " + value1);
}

} else {
*/
System.out.println(name + " -> " + value);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();

} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

结果我收到

Object description: App.Flat: 
kitchen -> App.Kitchen@140e19d
sqr -> 32.32
windows -> 5
rooms -> 4

但是想要

Object description: App.Flat:    
Object description: App.Kitchen:
sqr -> 12
windows -> 1
sqr -> 32.32
windows -> 5
rooms -> 4

如何?

最佳答案

您应该为要打印的每种类型重写 toString 方法。

例如,如果您想以可读的方式打印 YourObject 的实例,您应该编写如下内容:

public class YourObject
{
private int yourField1;
private int yourField2;

public YourObject(int f1, int f2)
{
yourField1 = f1;
yourField2 = f2;
}

@Override
public String toString()
{
return "YourObject[yourField1=" + yourField1 + ", yourField2=" + yourField2 + "]");
}

}

鉴于此,您可以执行以下操作:

AnotherObject obj = new AnotherObject();
obj.setYourObject(new YourObject(1,2));

当你打电话时:

describeObjectFields(obj);

您将得到:

YourObject[yourField1=1, yourField2=2]

更新

就您而言,您应该在 Kitchen 类中执行类似的操作:

public class Kitchen {
// your instance variables, constructor, getters and setters, etc.

@Override
public String toString() {
return ("sqr: " + sqr + ", windows: " + windows);
}

}

关于Java方法描述了任何对象需要改进的所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com