带参运用有点问题

来源:3-4 自由编程

孬帮手慕小菜

2020-06-10 18:27:24

package com.andreas.fanshe;

import java.lang.reflect.Constructor;

import org.junit.Test;

public class ConstructorTest {
	@Test
	public void ct1() throws Exception {
		Class g = Class.forName("com.andreas.fanshe.Goods");
		Constructor c = g.getConstructor();
		Goods goods = (Goods) c.newInstance();
		goods.display();
	}
	@Test
	public void ct2() throws Exception {
		Class g = Class.forName("com.andreas.fanshe.Goods");
		Constructor c = g.getConstructor(int.class,String.class,float.class,String.class);
		Goods goods = (Goods) c.newInstance(11,"玩具",28.3,"8岁以上儿童");
		System.out.println(goods);
	}
	
}
package com.andreas.fanshe;

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


java.lang.IllegalArgumentException: argument type mismatch

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at com.andreas.fanshe.ConstructorTest.ct2(ConstructorTest.java:19)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)


写回答

1回答

好帮手慕小脸

2020-06-10

同学你好!在java中,使用float类型的话,需加上f或者F,具体修改如下所示:

http://img.mukewang.com/climg/5ee0bab509cfa98e11890272.jpg

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

0

0 学习 · 8016 问题

查看课程