package test;
public class Human
{
/**
* constructor
*/
public Human(int h)
{
this.height = h;
System.out.println("I'm born");
Human.population +=1;
}
/**
* accessor
*/
public int getHeight()
{
return this.height;
}
/**
* mutator
*/
public void growHeight(int h)
{
this.height = this.height + h;
}
public static int getPopulation()
{
return Human.population;
}
private int height;
private static int population; // 类内部使用
public static boolean is_mammal = true; // 类外部可以直接使用
}
主入口
import test.Human;
public class testAgain {
public static void main(String[] args)
{
Human aPerson = new Human(180);
System.out.println(aPerson.getHeight());
System.out.println(Human.is_mammal);
System.out.println(aPerson.getPopulation());
}
}