**
Java 内部类(innerclasses)
**
package cn.pb.innerclasses;
public class Animal {
public static void main(String[] args) {
Animal animal = new Animal();
Animal.Breathe breathe = animal.new Breathe();
{
//非静态内部类
animal.setSpecies("狗");
animal.animal();
breathe.setSpecies("用鼻子呼吸");
breathe.breathe();
breathe.setAnimalNumber(150);
System.out.println(animal.number);
}
{
//静态内部类
Animal.Listen listen = new Animal.Listen();
listen.listen();
}
}
private String Species = null;
public int number = 0;
public static int age = 0;
public void setSpecies(String species) {
Species = species;
}
public void animal() {
System.out.println(Species);
}
/**
* 非静态内部类
* @author wl163
*
*/
class Breathe{
private String Species = null;
public int number = 0;
public void breathe() {
System.out.println(Species);
}
public void setSpecies(String species) {
Species = species;
}
public void setAnimalNumber(int num) {
Animal.this.number = num;
animal();
}
}
/**
* 静态内部类
*/
static class Listen{
public void listen() {
System.out.println("可以听");
}
public void setAnimalNumber(int num) {
Animal.age = num;
Animal.this.number = num;
animal();//错误:静态内部类不能调用非静态外部类的属性方法
}
}
}