检查作业

来源:2-6 自由编程

qq_精慕门6417434

2021-04-02 23:57:13

相关代码:

public class goods {
private int id;
private String name;
private float price;
private String desc;


public goods(int id, String name, float price, String desc) {
this.id = id;
this.name = name;
this.price = price;
this.desc = desc;
}

static {

System.out.println("这是静态代码块");
}

public goods(){
System.out.println("这是构造方法");
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public goods display(String desc) {
this.desc = desc;
System.out.println("修改后的desc为"+this.desc);
return this;

}

@Override
public String toString() {
return "goods{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
}

相关代码:

​import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class Testor {

public static void main(String[] args) {
Scanner si = new Scanner(System.in);
try {
Class a = Class.forName("goods");
Constructor consturctor = a.getConstructor(new Class[]{
int.class,String.class,float.class,String.class
});
goods good = (goods)consturctor.newInstance(new Object[]{
1,"厉害",3200f,"fasdfasfsdfsd发士大夫士大夫"
});
Method method = a.getMethod("display",new Class[]{
String.class
});
goods good1 = (goods)method.invoke(good,"我是靓仔");
System.out.println(good1);

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}


写回答

1回答

好帮手慕小尤

2021-04-03

同学你好,已完成练习,棒棒哒!

祝学习愉快!

0

0 学习 · 16556 问题

查看课程

相似问题

作业打卡

回答 1

请老师检查

回答 1

请老师检查

回答 1

老师请检查

回答 1

老师请检查

回答 1