定义一个内部类

来源:互联网 发布:seo专员招聘 编辑:程序博客网 时间:2024/04/28 11:52
/** * Description: * <br/>Copyright (C), 2005-2008, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author  Yeeku.H.Lee kongyeeku@163.com * @version  1.0 */public class Cow{private double weight;//外部类的两个重载的构造器public Cow(){}public Cow(double weight){this.weight = weight;}//定义一个内部类private class CowLeg{//内部类的两个属性private double length;private String color;//内部类的两个重载的构造器public CowLeg(){}public CowLeg(double length , String color){this.length = length;this.color = color;}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属性:weightSystem.out.println("本牛腿所在奶牛重:" + weight);}}public void test(){CowLeg cl = new CowLeg(1.12 , "黑白相间");cl.info();}public static void main(String[] args){Cow cow = new Cow(378.9);cow.test();}}/*当前牛腿颜色是:黑白相间, 高:1.12本牛腿所在奶牛重:378.9请按任意键继续. . .*/