打卡请老师检查

来源:2-6 自由编程

慕前端8369922

2020-08-17 07:18:17

package practice2;

public class Goods {
    private int no;
    private String name;
    private Float price;
    private String desc;

    public void display(String desc){
        System.out.println(toString());
        this.desc = desc;
        System.out.println("商品信息更改为: " + desc);
        System.out.println(toString());
    }

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

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    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(int no, String name, Float price, String desc) {
        this.no = no;
        this.name = name;
        this.price = price;
        this.desc = desc;
    }

    public Goods() {
    }
}
package practice2;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) {
        try {
            Class goodsClass = Class.forName("practice2.Goods");
            Constructor constructor = goodsClass.getConstructor(int.class, String.class, Float.class, String.class);
            Goods goods = (Goods) constructor.newInstance(1, "Apple", 3f, "this is an apple");
            System.out.println(goods);
            Method displayMethod = goodsClass.getMethod("display", String.class);
            displayMethod.invoke(goods,"this is an orange");
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}


写回答

1回答

好帮手慕小班

2020-08-17

同学已完成练习,棒棒哒!继续加油!

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

0

0 学习 · 16556 问题

查看课程