快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

java中,this用途总结。。。

注:参考了JIT(java in thinking),示例都是那里面的。
1、在一个类中,this可以表示该类的当前实例;例如:
public class Leaf {
private int i = 0;
Leaf increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
public static void main(String[] args) {
Leaf x = new Leaf();
x.increment().increment().increment().print();
}
} ///:~
2、若为一个类写了多个构造器,那么常常都需要在一个构造器里调用另一个构造器,以避免写重复的代码。这时可以使用this,例如:
//: Flower.java
// Calling constructors with "this"

public class Flower {
private int petalCount = 0;
private String s = new String("null");
Flower(int petals) {
petalCount = petals;
System.out.println(
"Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println(
"Constructor w/ String arg only, s=" + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
//! this(s); // Can\\\'t call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
void print() {
//! this(11); // Not inside non-constructor!
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
} ///:~

而需要注重的是:在一个类A的内部类B中使用this它表示的并非是A.B的当前实例,而是A的当前实例;







返回类别: 教程
上一教程: 《java深度历险》读书笔记(一)
下一教程: Draw2D--1. 设计思想和相关模式

您可以阅读与"java中,this用途总结。。。"相关的教程:
· JAVAMAIL操作的总结(3)
· JAVAMAIL操作的总结
· java初学者必读-经验总结
· Java1.5语言新特性简朴总结
· java 中protected modifier 使用总结
    微笑服务 优质保证 索取样品