zoukankan      html  css  js  c++  java
  • Java多态--构造方法的内部方法多态

    Java多态–构造方法的内部方法多态

    package com.pgy.base;
    
    /**
     * 构造器内部的多态方法的行为
     * 构造方法调用内部的方法时,会执行所创建对象的内部方法
     * @author admin
     * @version $Id: PolymorphismDemo.java, v 0.1 2015年9月1日 上午8:30:31 admin Exp $
     */
    class Person {
        void run() {
            System.out.println("Person run");
        }
    
        Person() {
            System.out.println("Person run before student");
            run();//当子类继承父类的构造方法的时候,此处调用的run,是创建对象student的run方法,而不是父类的run方法,因为父类对象没有被创建
            System.out.println("Person run after student");
        }
    }
    
    class Student extends Person {
        private int age = 1;
    
        //当父类调用此方法的时候,age没有被初始化,内存分配给age初始化的二进制0
        void run() {
            System.out.println("Student run method, age = " + age);
        }
    
        Student(int age) {
            System.out.println("Student run construct,age = " + age);
        }
    }
    
    public class PolymorphismDemo {
    
        public static void main(String[] args) {
            new Student(10);
        }
    }
    
    /*console*/
    //Person run before student
    //Student run method, age = 0
    //Person run after student
    //Student run construct,age = 10
    
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    2021 发工资咯:)
    HDU-2021
    HDU-2020
    HDU-2019
    HDU-2018
    HDU-2017
    HDU-2016
    HDU-2015
    HDU-2014
    HDU-2013
  • 原文地址:https://www.cnblogs.com/penggy/p/4786484.html
Copyright © 2011-2022 走看看