zoukankan      html  css  js  c++  java
  • 实例化函数的具体步骤

    1、找父类的无参构造函数

    1.1、初始化成员变量

    1.2、实现构造函数中的方法体

    2、回到元类,并依次实现1.1和1.2

    3、注:this()、super(),位于构造函数第一位,语义是调用相应的构造函数

    this、super可任意在方法体中的位置调用,语义是调用相应方法和属性

    package com.jacob.javase.extend;
    
    public class Parent {
        int age;
        String username="xieji";
    
        public void getUsername(){
             System.out.println("parent username");
        }
    
        public Parent() {
            System.out.println("Parent wucan constructor");
        }
        public Parent(int i) {
    //      this();
            System.out.println("Parent youcan constructor");
        }
    
    }
    
    package com.jacob.javase.extend;
    
    public class Child extends Parent{
        String username="jacob";
    
        public void getUsername(){
             System.out.println("child username");
        }
    
        public Child() {
            super(1);
            System.out.println("child wucan constructor");
            System.out.println(super.username);
            super.getUsername();
        }
    
        public Child(int i) {
            System.out.println("child ycan constructor");
        } 
    }
    
    package com.jacob.javase.extend;
    
    public class Test {
     public static void main(String[] args) {
           new Child().getUsername();
           System.out.println(new Child().username);
    }
    }
    
  • 相关阅读:
    算法图解
    Cairo graphics tutorial
    远程对象调用
    异步和多线程的关系
    jQuery调用api
    GTK# tutorial
    DLT
    protobuf入门笔记
    PDO讲解
    数据库练习——分页查询
  • 原文地址:https://www.cnblogs.com/xieji233/p/6155620.html
Copyright © 2011-2022 走看看