zoukankan      html  css  js  c++  java
  • 子类对象实例化全过程

    子类对象实例化全过程:

    package com.aff.sup;
    
    public class TestDog {
        public static void main(String[] args) {
            Dog d = new Dog();
            d.setAge(2);
            d.setName("天天");
            d.setHostName("芳芳");
            System.out.println("name:" + d.getName() + "age:" + d.getAge() + "hostName:" + d.getHostName());
            System.out.println(d.toString());
        }
    
    }
    
    // 生物
    class Creator {
        private int age;
    
        public Creator() {
            super();
            System.out.println("this is  Creator's constructor");
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
    }
    
    // 动物
    class Animal extends Creator {
        private String name;
    
        public Animal() {
            super();
            System.out.println("this is  Animal's constructor");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    //
    class Dog extends Animal {
        private String hostName;
    
        public Dog() {
            super();
            System.out.println("this is  Dog's constructor");
        }
    
        public String getHostName() {
            return hostName;
        }
    
        public void setHostName(String hostName) {
            this.hostName = hostName;
        }
    
    }

    输出结果:

    this is Creator's constructor
    this is Animal's constructor
    this is Dog's constructor
    name:天天age:2hostName:芳芳
    com.aff.sup.Dog@6d06d69c

    All that work will definitely pay off
  • 相关阅读:
    01 HTTP协议_servlet基础
    JS 08表单操作_表单域
    09 多态
    JS 07 Dom
    JS 06 bom 框窗_页面_定时任务
    JS 05 json
    08 包_继承
    JS 04 Date_Math_String_Object
    【Java每日一题】20161018
    【Java每日一题】20161017
  • 原文地址:https://www.cnblogs.com/afangfang/p/12516848.html
Copyright © 2011-2022 走看看