- public class Test extends Base {
- private String name = "dervied";
- public Test() {
- tellName();
- printName();
- }
-
- public void tellName() {
- System.out.println("Dervied tell name: " + name);
- }
-
- public void printName() {
- System.out.println("Dervied print name: " + name);
- }
- public static void main(String[] args){
-
- new Test();
- }
- }
- class Base {
-
- private String name = "base";
- public Base() {
- tellName();
- printName();
- }
-
- public void tellName() {
- System.out.println("Base tell name: " + name);
- }
-
- public void printName() {
- System.out.println("Base print name: " + name);
- }
- }
复制代码
为什么运行后是
Dervied tell name: null
Dervied print name: null
Dervied tell name: dervied
Dervied print name: dervied
|
|