关于Java类 在构造Java类是有一条:对于不带参数的构造器在其第一行用this关键字来调用本类的其他构造器!这有什么具体用处?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this 关键字是用来表示当前对象的··构造器的意思应该也是一个对象·· 这样在写代码构造对象的时候,就省去了传参数的繁琐,之间new + 类名就搞掂了 这样在写代码构造对象的时候,就省去了传参数的繁琐,直接new + 类名就搞掂了 this便是当前的变量。你的问题不是很明白。 设置默认值例如class num{num(){this(1);//说明默认构造方法参数m默认值是1}num(int m){}} 因为一个类进行new之前,类实例(包括实例变量,方法)是不存在的,注意实例变量与静态的区别,构造函数的作用就是初始化,new其实就是构造类对象,初始化函数的过程。this关键字为你提供了多种初始化类实例的途径。仔细研究下面的这个类,public class Exercise{ String name; int age; String sex; int height; Exercise(String name,int age,String sex,int height) { this.name=name; this.age=age; this.sex=sex; this.height=height; } Exercise(String name,int age,String sex) { this(name,age,sex,getHeight()); } Exercise(String name,int age) { this(name,age,getSex()); } Exercise(String name) { this(name,getAge()); } Exercise() { this(getName()); } public static void main(String[] args) { Exercise ex=new Exercise("Mike",40);//多种参数都行。 System.out.println(ex); } public String toString() { return "name="+this.name+" age="+this.age+ " sex="+this.sex+" height="+this.height; } static String getName() { String name="Peter"; return name; } static int getAge() { int age=25; return age; } static String getSex() { String sex="man"; return sex; } static int getHeight() { int height=168; return height; }} 请叫大牛们如何学习好JAVA的基础,谢谢你们 一道java题目 求高手帮我看一下这段代码哪里写错了。。 函数的指针调用方式 关于StringBuffer比较的相关问题 请教一个正则表达式的问题 isNaN方法怎么用 关于 (a&(a-1))==0判断是否是2 的幂的疑问 今天早晨刚得的最后十分,请教个简单问题关于JFrame 今天看视频的第一个项目 各种各样的Exception啊 求解 Swing高手关注!使用SwingUtilities.invokeLater 如何给一个泛型的ArrayList排序
例如
class num{
num(){
this(1);//说明默认构造方法参数m默认值是1
}
num(int m){
}
}
public class Exercise
{
String name;
int age;
String sex;
int height;
Exercise(String name,int age,String sex,int height)
{
this.name=name;
this.age=age;
this.sex=sex;
this.height=height;
}
Exercise(String name,int age,String sex)
{
this(name,age,sex,getHeight());
}
Exercise(String name,int age)
{
this(name,age,getSex());
}
Exercise(String name)
{
this(name,getAge());
}
Exercise()
{
this(getName());
}
public static void main(String[] args)
{
Exercise ex=new Exercise("Mike",40);//多种参数都行。
System.out.println(ex);
}
public String toString()
{
return "name="+this.name+" age="+this.age+
" sex="+this.sex+" height="+this.height;
}
static String getName()
{
String name="Peter";
return name;
}
static int getAge()
{
int age=25;
return age;
}
static String getSex()
{
String sex="man";
return sex;
}
static int getHeight()
{
int height=168;
return height;
}
}