当前位置: 技术问答>java相关
有关参数传递错误的问题向各位请教。
来源: 互联网 发布时间:2015-04-08
本文导语: 想写一段代码来看两个进程同时访问一个对象的一个方法,来判断在进程同时运行时该方法是使用不同的存储区域来保存局部变量还是一个存储区域保存局部变量,但在编译的过程中遇到了参数传递错误的问题,说是...
想写一段代码来看两个进程同时访问一个对象的一个方法,来判断在进程同时运行时该方法是使用不同的存储区域来保存局部变量还是一个存储区域保存局部变量,但在编译的过程中遇到了参数传递错误的问题,说是参数数目不对。
源代码:
import java.awt.*;
import java.awt.event.*;
class A extends Thread
{
private TextField tf = new TextField(10);
private B b;
public void A(Container c, B b){
c.add(tf);
this.b = b;
start();
}
public void run(){
b.functionB(this);
}
public void setText(int i){
tf.setText("i = " + i);
}
};
class B extends Thread
{
public void functionB(A a){
int i = 0;
while(true){
a.setText(i);
i++;
try{
sleep(1000);
}
catch(InterruptedException e){}
}
}
};
public class MultiMethodCall
{
public void MultiMethodCall(Container c){
B b = new B();
A a1 = new A(c, b);
A a2 = new A(c, b);
}
/* public void MultiMethodCall_1(Container c){
B b = new B();
A a1 = new A(c, b);
A a2 = new A(c, b);
}*/
public static void main(String[] args)
{
Frame aFrame = new Frame("MutilCall");
MultiMethodCall mmc = new MultiMethodCall(aFrame);
//mmc.MultiMethodCall_1(aFrame);
aFrame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
aFrame.setSize(200, 200);
aFrame.setVisible(true);
}
}
错误信息:
MultiMethodCall.java:40: Wrong number of arguments in constructor.
A a1 = new A(c, b);
^
MultiMethodCall.java:41: Wrong number of arguments in constructor.
A a2 = new A(c, b);
象这种形式创建新的对象是否属于不正常的创建呢?
源代码:
import java.awt.*;
import java.awt.event.*;
class A extends Thread
{
private TextField tf = new TextField(10);
private B b;
public void A(Container c, B b){
c.add(tf);
this.b = b;
start();
}
public void run(){
b.functionB(this);
}
public void setText(int i){
tf.setText("i = " + i);
}
};
class B extends Thread
{
public void functionB(A a){
int i = 0;
while(true){
a.setText(i);
i++;
try{
sleep(1000);
}
catch(InterruptedException e){}
}
}
};
public class MultiMethodCall
{
public void MultiMethodCall(Container c){
B b = new B();
A a1 = new A(c, b);
A a2 = new A(c, b);
}
/* public void MultiMethodCall_1(Container c){
B b = new B();
A a1 = new A(c, b);
A a2 = new A(c, b);
}*/
public static void main(String[] args)
{
Frame aFrame = new Frame("MutilCall");
MultiMethodCall mmc = new MultiMethodCall(aFrame);
//mmc.MultiMethodCall_1(aFrame);
aFrame.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
aFrame.setSize(200, 200);
aFrame.setVisible(true);
}
}
错误信息:
MultiMethodCall.java:40: Wrong number of arguments in constructor.
A a1 = new A(c, b);
^
MultiMethodCall.java:41: Wrong number of arguments in constructor.
A a2 = new A(c, b);
象这种形式创建新的对象是否属于不正常的创建呢?
|
你的程序中A的构造方法中去掉void,
public void A(Container c, B b){
改为:
pubilc A(Container c,B b){}
public void A(Container c, B b){
改为:
pubilc A(Container c,B b){}