关于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; }} 对字符串中数字进行排序 gmail 无法第二次查看收信问题 虽然==与equals的问题很老了,我还是忍不住来发个帖提问 关于Package的问题 请教一个内部类继承问题 请问可不可以进行两个int数组间的行对行拷贝 文本的格式化问题 我是大一学软件开发的新生,想向各位前辈讨要几本好书! 请熟悉jmf的高手解答. 大家说这个东东怎么实现那? 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;
}
}