[url=http://www.1sucai.cn/article/54548.htm]构造方法与方法重载[/url]
比如下面的程序中,Human类有一个构造方法:
class Human
{
/**
* constructor
*/
public Human(int h)
{
this.height = h;
}
/**
* accessor
*/
public int getHeight()
{
return this.height;
}
/**
* mutator
*/
public void growHeight(int h)
{
this.height = this.height + h;
}
/**
* breath
*/
public void breath()
{
System.out.println("hu...hu...");
}
private int height;
}
衍生类Woman类的定义及其构造方法:
class Woman extends Human
{
/**
* constructor
*/
public Woman(int h)
{
super(h); // base class constructor
System.out.println("Hello, Pandora!");
}
/**
* new method
*/
public Human giveBirth()
{
System.out.println("Give birth");
return (new Human(20));
}
/**
* override Human.breath()
*/
public void breath()
{
super.breath();
System.out.println("su...");
}
}
[b]总结[/b]
extends
method overriding
protected
super.member, super()