Java this/static/super/abstract

1:this 

 

 

 

code
public class Leaf {
int i=0;
Leaf(
int i) { this.i = i ;}
Leaf increament() {
i
++;
return this;
void print() { System.out.println("i = " + i); }

public static void main(String[] agrs) {
Leaf leaf
= new Leaf(100);
leaf.increament().increament().print();
}

}
// print result :102;

 

2: static

code
public class Cat {
private static int sId = 0;
private String name;
int id;
Cat(String name) {
this.name = name;
id
= sId++;
}
void info() {
System.out.println(name
+" " + id);
}
public static void main(String[] args) {
Cat.sId
= 100;//use SId without "new"...
Cat mimi = new Cat("mimi");
Cat keke
= new Cat("keke");
mimi.info();
keke.info();
}
}

//print : mimi 100
// keke 101

3:super

 

code
class Father {
public int value;
public void f() {
value
= 100;
System.out.println(
"father.vaue is "+value);
}
}
class Child extends Father {
public int value;
public void f() {
super.f();
value
= 200;
System.out.println(
"child.value is "+value);
System.out.println(
"no super.value is"+value);
System.out.println(
"super.value is"+super.value);
}
}
public class TestSuper {
public static void main(String[] args) {
Child cc
=new Child();
cc.f();
}
}
// father.value is 100
// child.value is 200
// no super.value is 200
// super.value is 100

在 继承构造函数中,先使用Super(**,**)调用父类的构造函数;再使用this(**,**)调用自身

没写super的话默认为无参构造函数

 4:abstarct

 抽象方法必须被继承重写,抽象类不能被实例化

 5:final

 

code
public class TestFinal {
public static void main(String[] args) {
T t
= new T();
//t.i = 2; //final变量无法赋值;
System.out.println(t.i);
}
}

/*final*/ class T { //如果类是final,无法做基类
final int i = 2;
public void m(final Dog j) { //final 无法改变一个对象
//
}
public final void n() {} //final无法被重载
}
class Dog {
}
class TT extends T {
// public void n(){}
}

 

posted @ 2010-07-30 17:38  jorgen  阅读(194)  评论(0编辑  收藏  举报
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj