【新手学java】关于this的种种问题

苦苦的潜行者 2011-07-13 10:33:35
话说各种教程上娴熟的运用this..真tm神奇.
我是越看越不懂,越懂越懵懂.

直接上代码,求分析求解释...


class Person{
String name;
int age;
Person(){
name="noname";
age=1;
}
Person(String name){
this();
this.name=name;
}
Person(int age){
this();
this.age=age;
}
Person(String name,int age){
this.name=name;
this.age=age;
}
void print(){
System.out.println ("我的名字是"+name+",年龄是"+age);
}
}
class Test{
public static void main(String [ ] args){
Person p1=new Person();
p1.print();
Person p2=new Person("小明");
p2.print();
Person p3=new Person(10);
p3.print();
Person p4=new Person("小芳",11);
p4.print();
}
}


教程上说:
注意在使用this 关键字来重载构造函数时,this 必须是构造函数的第一个语句,且一个构造函数中只能出现一次。

我就试验了一下.
class Person{
String name="noname";
int age=1; //这里变成初始化
/*
Person(){
name="noname";
age=1;
}*/
Person(String name){
this(); //运行时这里就开始报错,说找不到符号,构造函数Person()
this.name=name;
}
...........


但是我如果改成
class Person{
String name="noname";
int age=1; //这里变成初始化

Person(){
//name="noname";
//age=1;
}
Person(String name){
this();
this.name=name;
}
...........


这样就不出错.

难道必须要有一个不带参数的构造函数Person(){}吗
不是说,系统默认就有一个不带参数,不做任何操作的构造函数么?

如果这么说的话,
还有一道例题,关于this的用法,这this又是一个什么用法呢(代码略显冗长,就是设置时间的)
// Time3.java类的定义
public class Time3 {
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59

// Time3 constructor initializes each instance variable
// to zero. Ensures that Time object starts in a
// consistent state.

public Time3()
{
this.setTime( 0, 0, 0 ); //这个this指向何方,如果上面教程的话说this指向第一个构造函数,但是这个就是this就是本身了public Time3(){}了
}
// Time3 constructor: hour supplied, minute and second
// defaulted to 0
public Time3( int h )
{
this.setTime( h, 0, 0 );
}
// Time3 constructor: hour and minute supplied, second
// defaulted to 0
public Time3( int h, int m )
{
this.setTime( h, m, 0 );
}
public Time3( int h, int m, int s )
{
this.setTime( h, m, s );
}
// Time3 constructor: another Time3 object supplied
public Time3( Time3 time )
{
this.setTime( time.getHour(), time.getMinute(),
time.getSecond() );
}
// Set Methods
// set a new Time value using universal time
public Time3 setTime( int hour, int minute, int second )
{
this.setHour( hour ); // set hour
this.setMinute( minute ); // set minute
this.setSecond( second ); // set second
return this; // enables chaining
}
// validate and set hour
public Time3 setHour( int hour )
{
this.hour = ( hour >= 0 && hour < 24 ? hour : 0 );
return this; // enables chaining
}
public Time3 setMinute( int minute )
{
this.minute = ( minute >= 0 && minute < 60 ) ? minute : 0;
return this; // enables chaining
}
// validate and set second
public Time3 setSecond( int second )
{
this.second = ( second >= 0 && second < 60 ) ? second : 0;
return this; // enables chaining
}
// get value of hour
public int getHour() { return this.hour; }

// get value of minute
public int getMinute() { return this.minute; }
// get value of second
public int getSecond() { return this.second; }
// convert to String in universal-time format
public String toUniversalString()
{
return hour + ":" + minute + ":" + second ;
}
// convert to String in standard-time format
public String toString()
{
return ((hour == 12 || hour == 0) ? 12 : hour % 12 ) +":" + minute+":" + second + ( hour < 12 ? " AM" : " PM" );
}
public static void main( String args[] )
{
Time3 t1, t2, t3, t4, t5, t6;
t1 = new Time3(); // 00:00:00
t2 = new Time3( 2 ); // 02:00:00
t3 = new Time3( 21, 34 ); // 21:34:00
t4 = new Time3( 12, 25, 42 ); // 12:25:42
t5 = new Time3( 27, 74, 99 ); // 00:00:00
t6 = new Time3( t4 ); // 12:25:42
System.out.println(t1.toUniversalString()+'\t'+ t1.toString());
System.out.println(t2.toUniversalString()+'\t'+ t2.toString());
System.out.println(t3.toUniversalString()+'\t'+ t3.toString());
System.out.println(t4.toUniversalString()+'\t'+ t4.toString());
System.out.println(t5.toUniversalString()+'\t'+ t5.toString());
System.out.println(t6.toUniversalString()+'\t'+ t6.toString());
}
}



不吝赐教!!!!!
...全文
169 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
苦苦的潜行者 2011-07-16
  • 打赏
  • 举报
回复
结贴!,其实应该在8楼就应该结贴,结果有事耽误了.
我这个人不会给灌水分的,对不起了.
pywepe 2011-07-14
  • 打赏
  • 举报
回复

this 就是 self

python用的是self
wugui414 2011-07-14
  • 打赏
  • 举报
回复
mark 赚积分
苦苦的潜行者 2011-07-13
  • 打赏
  • 举报
回复
恩,很好很不错,我一开始的时候没弄明白调用(非构造函数)方法和调用构造函数的区别.
误以为void Test(int i){this();} 和 void Test(int i){this.getTest();}是一种情况了

dreamliver8 2011-07-13
  • 打赏
  • 举报
回复
mark
iis81365341 2011-07-13
  • 打赏
  • 举报
回复
public class Test
{
private int id;

private String name;

public Test()
{
this.name = "";//表示本对象中的name成员变量
}

public Test(int i)
{
this();//表示无参构造函数Test()
this.id = i;//表示本对象中的id成员变量
}

public Test(int i, String name)
{
this(i);//表示有一个参构的造函数Test(int i)
}
}
xiaomaha 2011-07-13
  • 打赏
  • 举报
回复

public class Test{
private int a=100;
public int getA(){
return this.a;//这里的this就是指当类对象的.a就是调用当前类对象中的a属性
}
public getB(){
this.getA();//这个里.getA()就是调用当前类对象中的getA()方法·
}
}

啊,不知明白不?
xiaomaha 2011-07-13
  • 打赏
  • 举报
回复
this就是调用自己这个类本身的方法或属性,当然构造函数也是方法!

构造函数的问题ls几位已经说的很清楚了·请参考·
苦苦的潜行者 2011-07-13
  • 打赏
  • 举报
回复
听了你们的话我突然一下子懵懂了很多...
希望楼下的给更多关于this的知识点和注意点,谢谢了/
luochengor 2011-07-13
  • 打赏
  • 举报
回复
1、类本身自带一个无参的构造函数,但是如果你写了带参构造函数,那么这个无参的构造函数将不存在,如果需要使用无参构造函数,必须重新写上无参的构造函数。
2、关于Time3()的this
如:Time3 time = new Time3();
调用无参构造函数时,创建一个对象,里面的this指的是这个对象,之后将这个对象赋给time.
这是个人的理解,如有错误,希望大家指出,希望对你有所帮助。
sunyiz 2011-07-13
  • 打赏
  • 举报
回复
在一个类没有定义构造方法时,会有一个默认的无参构造方法
如果类已经定义了带参构造方法,将不会有默认无参构造方法
sunxin1001 2011-07-13
  • 打赏
  • 举报
回复
this(); 调用的是不带参数的构造函数 Person(){
//name="noname";
//age=1;
}

如果把它注视掉,当然会出错了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧