您的位置:首页 > 编程语言 > Java开发

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

2013-01-19 19:33 281 查看
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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐