请老师检查

来源:3-4 自由编程

Ctrlzhao

2019-12-19 14:05:45

package com.imooc.reflect.test;

import java.lang.reflect.Constructor;

import org.junit.Test;

public class GoodsConstructorTest {
	@Test
	/**
	 * 获得无参构造方法 
	 */
	public void demo1() throws Exception {
		Class class1 = Class.forName("com.imooc.reflect.test.Goods");
		Constructor c = class1.getConstructor();
		Goods goods = (Goods)c.newInstance();
		goods.display();
	}
	
	@Test
	/**
	 * 带参构造方法
	 */
	public void demo2() throws Exception {
		Class class1 = Class.forName("com.imooc.reflect.test.Goods");
		Constructor c = class1.getConstructor(int.class,String.class,float.class,String.class);
		Goods goods = (Goods)c.newInstance(001,"小米10",3699f,"8+256");
		System.out.println(goods);
	}
}
package com.imooc.reflect.test;

public class Goods {
	private int id;
	private String name;
	private float price;
	private String desc;
	
	public Goods() {
		super();
	}
	public Goods(int id, String name, float price, String desc) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
		this.desc = desc;
	}
	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;
	}
	@Override
	public String toString() {
		return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", desc=" + desc + "]";
	} 
	public void display() {
		System.out.println("商品信息");
	}
}


写回答

1回答

好帮手慕酷酷

2019-12-19

同学你好,代码完成的不错!继续努力!

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

0

0 学习 · 8016 问题

查看课程