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();
}
}
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非静态内部类可以访问外部类的private实例变量
- 震惊!外部类可以访问内部类private变量
- 外部类可以自由访问内部类的private方法
- 非静态内部类为什么可以访问外部类的静态成员
- JAVA 在外部类外部访问内部类,静态内部类和非静态内部类的区别
- 为什么内部类可以访问外部类的变量
- 外部类是否可以访问内部类private元素
- 为什么不管是静态内部类的普通方法还是静态方法都不能访问外部类的实例变量
- Java内部类为什么可以访问外部类的成员
- Java非静态内部类和静态内部类,访问父类的非静态和静态成员变量及成员函数
- 内部类访问外部类的实例
- 关于java内部类访问类的静态成员变量
- 静态内部类访问外部类静态变量的原因分析
- java静态内部类和非静态内部类,及他们在外部的定义
- java 结合内部类与外部类的静态(static)与非静态的用法
- java 外部类静态方法访问内部类
- Java中匿名内部类 只能访问外部类变量
- 内部类可以访问外部类的属性
- make工具介绍
- 教你制作自己喜欢的Win7主题
- 从程序员到项目经理(10):程序员加油站 –要执着但不要固执
- leetcode 46: Two Sum
- jQuery Ajax(load,post,get,ajax)用法与详解
- java非静态内部类可以访问外部类的private实例变量
- Windows内存管理
- SwingWorker
- SQL Server 2000和SQLServer 2005同时安装在一台电脑上出现的问题
- Android开发--详解SharedPreference/PreferenceActivity
- 利用本地包含漏洞执行任意代码
- Servlet线程安全
- Android getDecorView用途——屏幕截图
- 有关asset pipeline与stylesheets文件位置