zoukankan      html  css  js  c++  java
  • this and super

    this 和 super 的区别:this, 先从本类找属性和方法,本类找不到再从父类找。super, 从父类找。
    this 和 super 都可以调用构造方法,所以this() 和 super() 不可以同时出现。
    public class Person {
    }
    public class Student extends Person {
        String name;
        public Student(String name) {
            super();
    //        this();
        }
        public Student() {}
    }


    class FatherClass {
        public int value;
        public void f() {
            value = 100;
            System.out.println("FatherClass,value = " + value);
        }
    }
    
    class ChildClass extends FatherClass {
        public int value;
        public void f() {
            super.f();
            value = 200;
            System.out.println("ChildClass,value = " + value);
            System.out.println(value);
            System.out.println(super.value);
        }
    }
    
    public class Demo {
        public static void main(String[] args) {
            ChildClass cc = new ChildClass();
            cc.f();
        }
    }
    View Code

    画图分析:

     

     问题:

    class Person {
        public String name;
        public String location;
        Person (String name) {
            this.name = name;
            location = "beijing";
        }
        Person (String name, String location) {
            this.name = name;
            this.location = location;
        }
        public String info() {
            return "name:" + name + ",location:" + location;
        }
    }
    
    class Student extends Person {
        private String school;
        Student(String name, String school) {
            // 这个地方应该调用父类无参的构造方法,但父类没有无参的构造方法,但实际上并没有调用,下面的"xxx"可以打印出来。?
            this(name, "benjing",school);
        }
        public Student(String name, String location, String school) {
            super(name, location);
            this.school = school;
        }
    }
    
    public class Demo {
        public static void main(String[] args) {
            new Student("c", "s1");
            System.out.println("xxx");
        }
    }
  • 相关阅读:
    JS正则手机靓号处理AB ABAB AABB
    url参数中有+、空格、=、%、&、#等特殊符号的问题解决
    LigerUI 使用教程表格篇
    JS获取地址栏参数
    手机端meta
    最大连续子序列 hdu 1231
    I NEED A OFFER! hdu1203(背包)
    Bookshelf 2 poj3628(01背包/DFS)
    Charm Bracelet poj3624(01背包)
    985的方格难题(dp)
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/7062072.html
Copyright © 2011-2022 走看看