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();
}
}
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();
}
}
相关文章推荐
- Java匿名内部类可以访问外部类的成员;若访问所在方法的成员,那个成员必须具有final属性
- 深入理解Java中为什么内部类可以访问外部类的成员
- 在我的上一篇博客 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- Java中为什么内部类可以访问外部类的成员
- # 深入理解Java中为什么内部类可以访问外部类的成员#
- 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- 非静态内部类可以直接访问外部类的成员,而外部类不可以直接访问非静态内部类的成员
- Java基础-内部类-为什么成员内部类可以无条件访问外部类
- 非静态内部类为什么可以访问外部类的静态成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员
- 深入理解Java中为什么内部类可以访问外部类的成员