JAVA 构造函数未定义
1个回答
展开全部
构造函数不需要抛出异常吧,我给你改了下,如果不是你要的答案,勿喷,我也是初学者……
public
class
person
{
public
int
age;
public
string
name;
public
string
sex;
public
person(){
age=0;
name="0";
sex="男";
}
public
person(int
age,string
name,string
sex){
this.age=age;
this.name=name;
this.sex=sex;
}
public
person(person
n){
this.set(n.age,
n.name,
n.sex);
}
public
void
set(int
age,string
name,string
sex){
if(age>150
||
age<1)
system.out.println(age+"\t年龄设置不合适,有效年龄为1~150。");
this.age=age;
system.arraycopy(name,
0,
this.name,
0,
5);
system.arraycopy(sex,
0,
this.sex,
0,
5);
}
public
static
void
main(string
args[]){
person
somebody
=
new
person(25,"swd","男");
system.out.println("年龄:"+somebody.age+"\n姓名:"+somebody.name+"\n性别:"+somebody.sex);
}
}
public
class
person
{
public
int
age;
public
string
name;
public
string
sex;
public
person(){
age=0;
name="0";
sex="男";
}
public
person(int
age,string
name,string
sex){
this.age=age;
this.name=name;
this.sex=sex;
}
public
person(person
n){
this.set(n.age,
n.name,
n.sex);
}
public
void
set(int
age,string
name,string
sex){
if(age>150
||
age<1)
system.out.println(age+"\t年龄设置不合适,有效年龄为1~150。");
this.age=age;
system.arraycopy(name,
0,
this.name,
0,
5);
system.arraycopy(sex,
0,
this.sex,
0,
5);
}
public
static
void
main(string
args[]){
person
somebody
=
new
person(25,"swd","男");
system.out.println("年龄:"+somebody.age+"\n姓名:"+somebody.name+"\n性别:"+somebody.sex);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询