java非静态内部类可以访问外部类的private实例变量

来源:互联网 发布:php将字符串转换成数组 编辑:程序博客网 时间:2024/05/22 14:51
import static java.lang.System.*;
public class Cow
{
private double weight;
public Cow()
{}
public Cow(double weight)//Cow构造器
{
this.weight=weight;
}
private class CowLeg//内部非静态类
{
private double legth; //内部类的实例field
private String color;
public CowLeg(double legth,String color)
{
this.legth=legth;
this.color=color;
}
public void setLegth(double legth)
{
this.legth=legth; 
}
public double getLegth()
{
return this.legth;
}
public String getColor()
{
return this.color;
}
public void info()
{
             out.println("Cow's leg's legth is "+legth+" color is "+color);
out.println("本牛的重量为"+weight);   //非静态内部类可以访问外部类的private实例变量
}
}
public void test()
{
CowLeg c1= new CowLeg(232.4,"red");
c1.info();
}
public static void main(String[] args)
{
Cow cow = new Cow(378.9);
cow.test();
}
}
原创粉丝点击