傲视天地400级后期:java里面问题!求助

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/28 06:08:47
带参数的构造方法
比如class a {
a i(int m)
{
return ......
{
}
这样的方法
具体在括号里的参数有什么用处啊?
能举个详细的例子吗?
我只知道比如
class a{
int a,b;
a i()
{
b=a*3
return b;
}
a(int i){
this.i=a;}
}
这样的
但是带了参数的a i(int m){}
这样的方法我就不会了。。。。。
能帮我详细解释下吗?
是我程序敲错了
我的意思是
比如有方法i如下
class text{
int a,b;
int i()
{
a=b*3
}
//构造函数如下
test(int b){
this.a=b
}
}
public static void main(String args[])
{
test JU=new test(3)
System.out.println(JU.i());
}
这样我是懂的
但是当方法变成
int i(int j)
{
a=b*3
}
时我就不懂了
参数int j是做什么用的啊?

首先说明构造函数是不能有返回类型的修饰符的,所以你上面的java程序有问题。构造函数中有带参数和不带参数的分别。如下
class test{
int a,b;
public test()
{
a=0;
b=0;
}
public test(int a,int b){
this.a=a;
this.b=b
}
}
其中,
public test(int a,int b){
this.a=a;
this.b=b
} 就是带参数的构造函数,可以通过如下方式创建该对象
test aa=new test();//此时调用构造函数test()
test aa=new test(1,1);//此时调用构造函数test(int a,int b)