我的类里有三个构造:
public Employee(String n,double s,int year,int month,int day)
{
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
hireDay=calendar.getTime();
}
public Employee()
{}
第三个类要调用第一个类同时增加一个参数int x 请问这个类怎么写??谢谢!
public Employee(String n,double s,int year,int month,int day)
{
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
hireDay=calendar.getTime();
}
public Employee()
{}
第三个类要调用第一个类同时增加一个参数int x 请问这个类怎么写??谢谢!
解决方案 »
- RDP求助
- jvm的3点不明?
- 请教毕业设计:基于委托的访问控制
- com.sun.tools.javac.Main 编译莫名的警告:编码 utf-8 的不可映射字符(文件是utf-8的)
- 请教关于org.apache.poi的问题!HELP!!!
- 散分----马上找工作,想找JAVA方面的相关职业,最近比较迷茫,希望各位指点
- 求助队列数据结构
- java 执行Linuxshell 需要输入 如何处理?
- 郁闷啊~~~~~到底错哪了???
- 在异乡打工,有没有经历挫折或精神上遭受打击的朋友?一起坦言成败!
- struts-menu是啥啊?
- 我读了一个30几M的文件,就出现了java.lang.OutOfMemoryError: Java heap space的错误
public Employee(String n,double s,int year,int month,int day)
{
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
hireDay=calendar.getTime();
}
public Employee()
{}
第三个构造器要调用第一个构造器同时增加一个参数int x 请问这个构造器怎么写??谢谢!
{
this(n,s,year,month,day);
处理x
}
{
this(n,s,year,month,day);
对x的处理...
}
this(....)这句必须放在该构造器的第一行
Normally, when you say this, it is in the sense of “this object” or “the current object,” and by itself it produces the reference to the current object. In a constructor, the this keyword takes on a different meaning when you give it an argument list. It makes an explicit call to the constructor that matches that argument list. Thus you have a straightforward way to call other constructors: Feedback
//: c04:Flower.java
// Calling constructors with "this."
import com.bruceeckel.simpletest.*;public class Flower {
static Test monitor = new Test();
int petalCount = 0;
String s = new String("null");
Flower(int petals) {
petalCount = petals;
System.out.println(
"Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println(
"Constructor w/ String arg only, s=" + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
//! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println("default constructor (no args)");
}
void print() {
//! this(11); // Not inside non-constructor!
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
monitor.expect(new String[] {
"Constructor w/ int arg only, petalCount= 47",
"String & int args",
"default constructor (no args)",
"petalCount = 47 s = hi"
});
}
} ///:~The constructor Flower(String s, int petals) shows that, while you can call one constructor using this, you cannot call two. In addition, the constructor call must be the first thing you do, or you’ll get a compiler error message.