package com.wh.Demo50; /** * @author 王恒 * @datetime 2017年4月7日 下午4:40:54 * @description * 分析:子类重写父类的方法时,调用子类的构造方法 * 这是一道逻辑题,比较绕,不知道程序怎么走的时候,用debug模式,追踪走一边,所有方法均打断点 */ public class Demo04_wolf { public static void main(String[] args) { Wolf w = new Wolf("灰太狼", 32.3); System.out.println(w); } } class Animal { private String desc; public Animal() { this.desc = getDesc(); } public String getDesc() { return "Animal"; } public String toString() { return desc; } } class Wolf extends Animal { private String name; private double weight; public Wolf(String name, double weight) { // super(); this.name = name; this.weight = weight; } public String getDesc() { return "Wolf[name=" + name + ", weight=" + weight + "]"; } }
运行结果:
Wolf[name=null, weight=0.0]
时间: 2024-10-12 16:04:50