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
    
    
  • 相关阅读:
    四十四 常用内建模块 struct
    四十三 常用内建模块 base64
    Django Haystack 全文检索与关键词高亮
    python实现简单tftp(基于udp)
    多线程socket UDP收发数据
    Python 线程复习
    python 进程复习
    python pdb 调试
    Linux 复习
    Django 博客
  • 原文地址:https://www.cnblogs.com/penggy/p/7475838.html
Copyright © 2011-2022 走看看