super 和this 都只是引用
不可以代表对象,这句话切记。
老夫留一段代码,你调试几遍即可理解,
- class A
- {
- String name;
- A(String name)
- {
- this.name=name;
- }
- }
- class B extends A
- {
- B(String name)
- {
- super(name);
- }
- public void demo()
- {
- System.out.println(this.toString());
- System.out.println(super.toString());
- System.out.println(new A("nihao").toString());
- }
- }
- class Test
- {
- public static void main(String[] args)
- {
- B b = new B("haha");
- b.demo();
- System.out.println(b);
- A a=new A("nihao");
- System.out.println(a);
- }
- }
复制代码 |