Java编译器生成的内部类的字节码文件的名字和通常的类不同
将字节码文件复制给其他开发人员时,不要忘记内部类的字节码文件
内部类对应的字节码文件的名字格式是 外嵌类名$内部类名
,RedCowFarm$RedCow.class
/**
* 某种类型的农场饲养了一种特殊种类的牛
* 但不希望其他农场饲养这种牛,将这种特殊种类的牛的类作为内部类
*/
public class RedCowFarm {
String farmName; // 农场名称
RedCow redCow; // 饲养的红牛
public RedCowFarm() {
}
public RedCowFarm(String farmName) {
this.farmName = farmName;
redCow = new RedCow(158, 110, 5000);
}
/**
* 展示农场信息
*/
public void showFarmMess() {
redCow.speak();
// 【外访内,必须通过内部类对象】
}
class RedCow {
String cowName = "红牛";
int height, weight, price;
public RedCow(int height, int weight, int price) {
this.height = height;
this.weight = weight;
this.price = price;
}
/**
* 展示牛的信息
*/
void speak() {
System.out.println("品种:" + cowName + ",身高:" + height + ",体重:"
+ weight + ",价格:" + price + ",生活在" + farmName);
// 【内访外,随意直接访问】
}
}
}