public class King {
/**
* 非静态内部类简单实例代码
* @author 王者黑桃
*/
private double weight;
//外部类的两个重载的构造器
public King(){}
public King(double weight){
this.weight=weight;
}
//定义一个非静态内部类
private class KingBlack{
private double length;
private String color;
//非静态内部类的两个重载的构造器
public KingBlack(){}
public KingBlack(double length,String color){
this.color=color;
this.length=length;
}
public void setLength(double length){
this.length=length;
}
public double getLength(){
return this.length;
}
public void setColor(String color){
this.color=color;
}
public String getColor(){
return this.color;
}
//非静态内部类的实例方法
public void info(){
System.out.println("当前颜色:"+color+",长度:"+length);
//直接访问外部类的private属性:weight
System.out.println("当前重量为:"+weight);
}
}
//定义实例方法,创建内部类对象,调用内部类方法
public void test(){
KingBlack kb=new KingBlack(170, "黑色");
kb.info();
}
public static void main(String[] args) {
King king=new King(65);
king.test();
}
}
|