---
title: "第五惑：初始化和动态绑定之间的小小冲突"
author: "cantellow"
date: "2009-04-04T23:46:35Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-10-16T19:50:38Z"
id: 361658
url: "https://cantellow.iteye.com/blog/361658"
---

今天骑车去都江堰了，很累很累，没准备好今天该发什么Tips，所以就找了以前的知识点，不算是充数，希望大家多多包涵。

首先上代码：

```
package cantellow.text13;

public class Test13
{

	/**
	 * @param args
	 * @author cantellow
	 */
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		new RoundGlyph(5);
	}

}

abstract class Glyph
{
	abstract void draw();
	Glyph()
	{
		draw();
	}
}

class RoundGlyph extends Glyph
{

	private int radius = 1;
	RoundGlyph(int r)
	{
		radius = r;
	}
	void draw()
	{
		System.out.println(radius);
	}
}
```

 输出：0

问题：为什么输出不是1而是0？

==================================================================

参考理解：

因为在基类Glyph构造器中调用的 void draw()其实是RoundGlyph对象调用的，它调用的不是Glyph的draw()方法而是调用的RoundGlyph的draw()方法，而RoundGlyph的draw()方法它是把radius输出，而在这个时候，radius还没初始化。它只是在之前分配对象空间时被置为0，所以输出的是0。