在不使用常量的情况下怎么调用带参的构造方法? 例如两个构造方法1:public Test(){}2:public Test(int a){}怎么在不使用常量的情况下,使用1的构造方法调用2的构造方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你new Test() 1new Test(2) 2话说1什么都没有何用 是在1方法体里写调用2的代码,例如写this(10);但是不能用常量,括号里的10怎么改 public Test(){this((int)Math.random());}没用常数吧,不过这样毫无意义啊 肯定是要调用this(int)的,但是你这个int到底怎么来啊,你意思不是常量就定义一个变量int i=10;this(i);不太明白意思 没太搞明白LZ的需求,照我的理解大概是这样吧:public class Test { /** * @param args */ public static void main(String[] args) { Test3 t1=new Test3(); Test3.initData=5; Test3 t2=new Test3(); }}class Test3{ public static int initData=0; public Test3(){ this(initData); } public Test3(int data){ System.out.println(data); }} public Test(){ this(new Integer(3)); System.out.println("OK"); } public Test(int a){ } this可以调用重载的构造器,如:this(10);,就是调用一个整型参数的构造器…需注意的是这句代码必须写在构造器的第一句…否则编译时报错… 如何把视频等多媒体信息存放在oracle中 在线求高手解答。 关于单选按钮的问题 java paint java 中如果用可增加数组实现 ArrayList 请问问题出在那里? 为什么throws不起作用啊? 一个很白痴的问题,为什么我import org.htmlparser时候出错? 我做练习写的这个程序编译是顺利通过,可是运行是却出现报错信息,大家能帮我看看么?! 为什么装不了Jbuilder 4企业版呀?? 请教一个问题,一个具有静态属性的成员方法能不能被子类覆盖? myeclipse与Tomcat配置!!!
new Test(2) 2
话说1什么都没有何用
{
this((int)Math.random());
}
没用常数吧,不过这样毫无意义啊
int i=10;
this(i);
不太明白意思
* @param args
*/
public static void main(String[] args) {
Test3 t1=new Test3();
Test3.initData=5;
Test3 t2=new Test3();
}
}class Test3{
public static int initData=0;
public Test3(){
this(initData);
}
public Test3(int data){
System.out.println(data);
}
}
this(new Integer(3));
System.out.println("OK");
}
public Test(int a){
}