public class Test
{
public static void main(String[] args)
{
Woman aWoman = new Woman();
aWoman.growHeight(120);
System.out.println(aWoman.getHeight());
}
}
class Woman extends Human
{
/**
* new method
*/
public Human giveBirth()
{
System.out.println("Give birth");
return (new Human());
}
}
class Human
{
/**
* 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;
}
Human定义在Woman后面也能被Woman类查找到。。。和c/c++的先声明再使用完全不一样啊。。。 java新手 java的名字查找是怎么样的? 网上没找到这方面的资料。。。
编译完了之后每个类都是一个 .class 文件,哪来的先后。
Java 控制访问都是通过
public
,protected
,默认包权限,private
几个修饰符来限制的。在一个 Java 文件里定义的顶级类(非内部类)只有一个要求:只能有一个 public 类。