tij学习笔记(关于构造函数的一些问题,this关键字)
2008-02-12 21:48
337 查看
一,用构造函数创建实例的问题。
问题:
Integer i1 = new Integer(40);
Integer i2 = new Integer(40);
i1==i2,i1.equals(i2)这两个表达式的值是多少?
i1==i2=false;i1.equals(i2)=true;
因为前者表示创建对象(对象的引用),等号后面表示赋值给对象(对象的内容);所以i1和i2值是相等的,但是是两个对象,所以i1!=i2。
二,this关键字。
1,this表示当前对象的引用,如在方法中
return this;表示return的是该class的实例,其方法的类型应该和该实例一样。
public class LeafTest ...{
int i = 0;
LeafTest increment()...{
i++;
return this;
}
void print()...{
System.out.println("i="+i);
}
/** *//**
* @param args
*/
public static void main(String[] args) ...{
// TODO Auto-generated method stub
LeafTest l = new LeafTest();
l.increment().increment().increment().print();
}
}/**Output i = 3;
*///~
2,可以在构造器中引用构造器,但是要注意几点
a:用this构造实例必须在最始处。
b:只有构造函数中允许调用其他构造函数。
package w4;
public class ThisTest2 ...{
int pCount = 0;
String s = "init value";
ThisTest2(int p)...{
pCount = p;
System.out.println("p="+p);
}
ThisTest2(String ss)...{
s = ss;
System.out.println("s="+s);
}
ThisTest2(String s,int p)...{
this(p);
this.s = s;
System.out.println("String$int");
}
ThisTest2()...{
this("hi",47);
System.out.println("aaa");
}
void pr()...{
// this(11);不能在非构造函数中使用
System.out.println("p="+pCount+" s="+s);
}
/** *//**
* @param args
*/
public static void main(String[] args) ...{
// TODO Auto-generated method stub
ThisTest2 t = new ThisTest2();
t.pr();
}
}/** *//**Output:
*p=47
*String$int
*aaa
*p=47 s=hi
*///~
问题:
Integer i1 = new Integer(40);
Integer i2 = new Integer(40);
i1==i2,i1.equals(i2)这两个表达式的值是多少?
i1==i2=false;i1.equals(i2)=true;
因为前者表示创建对象(对象的引用),等号后面表示赋值给对象(对象的内容);所以i1和i2值是相等的,但是是两个对象,所以i1!=i2。
二,this关键字。
1,this表示当前对象的引用,如在方法中
return this;表示return的是该class的实例,其方法的类型应该和该实例一样。
public class LeafTest ...{
int i = 0;
LeafTest increment()...{
i++;
return this;
}
void print()...{
System.out.println("i="+i);
}
/** *//**
* @param args
*/
public static void main(String[] args) ...{
// TODO Auto-generated method stub
LeafTest l = new LeafTest();
l.increment().increment().increment().print();
}
}/**Output i = 3;
*///~
2,可以在构造器中引用构造器,但是要注意几点
a:用this构造实例必须在最始处。
b:只有构造函数中允许调用其他构造函数。
package w4;
public class ThisTest2 ...{
int pCount = 0;
String s = "init value";
ThisTest2(int p)...{
pCount = p;
System.out.println("p="+p);
}
ThisTest2(String ss)...{
s = ss;
System.out.println("s="+s);
}
ThisTest2(String s,int p)...{
this(p);
this.s = s;
System.out.println("String$int");
}
ThisTest2()...{
this("hi",47);
System.out.println("aaa");
}
void pr()...{
// this(11);不能在非构造函数中使用
System.out.println("p="+pCount+" s="+s);
}
/** *//**
* @param args
*/
public static void main(String[] args) ...{
// TODO Auto-generated method stub
ThisTest2 t = new ThisTest2();
t.pr();
}
}/** *//**Output:
*p=47
*String$int
*aaa
*p=47 s=hi
*///~
相关文章推荐
- 关于构造函数继承的一些问题
- 关于构造函数析构函数以及成员变量内存分配顺序的一些问题
- 关于yum的一些安装问题
- 关于SAP的RFC一些问题列表
- 关于Android stdio使用的一些小问题的解决方法
- 关于servlet中web.xml的一些问题
- JavaScript关于使用空函数来作为原型对象xx.prototype之间的桥梁的一些小问题
- ABAP--关于ABAP Dictionary的一些简单问题
- 关于python处理作Excel文件的一些问题,包括时间处理 (转)
- 关于servlet中如何弹出消息框并跳转到某一页面---和其中的一些问题
- 关于IBM H23使用esxi5.1的一些问题
- 关于基类和派生类的构造函数和析构函数的执行顺序问题
- 关于LC(01)背包的一些小问题。
- 关于linux中多线程编程的一些初级问题
- 关于派生类构造函数与基类构造函数的调用顺序问题
- 关于Linux下apache的一些配置问题
- 关于自定义Camera的一些问题
- 一些关于中文乱码问题的一些解决方案和经验和大家分享!
- 一些常见的关于Linux系统的问题
- 【达达】关于指纹做识别的一些问题