0%

static+this+super

面向对象中的关键字:static、this、super

#this 指本类中的引用。
可以利用this调取到本类中的属性及方法。
不能在static方法中使用。

#super 指父类中的引用。
可以利用super调取的父类中的属性及方法。
不能在static方法中使用。

#static 静态的。
在类被加载的时候自动加载。
被static修饰的方法 可被类名直接调用, 不需要new对象使用。

   静态方法访问方法外的成员时,此成员只能是静态成员 -全局变量
   静态方法中不可以写this,super关键字
   静态方法中不能直接调用非静态方法(可通过new对象,对象调用)                               
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class TestObject extends C {
static int c = 10;
int d = 15;

public static void main(String[] args) {
TestObject testObject = new TestObject();

testObject.staticTest();
testObject.thisTest();
testObject.superTest();

}

public void staticTest() {
int b = c;
System.out.println("staticTest结果" + b);
}

// 测试this
public void thisTest() {
int d = 5;
int b = this.d;
System.out.println("b=this.d的值为" + b);
}

// 测试super
public void superTest() {
String eString = super.taiyuan;
System.out.println("super测试结果:" + eString);
}
}
public class C {
public String taiyuan = "taiyuan";
}