内部类练习题

package com.Summer_0429.cn;
/**
 * @author Summer
 * 定义一个牛Cow类,牛有:
 * 身高
 * 体重
 * 牛腿内部类CowLeg
 *         颜色
 *         长度
 *         显示牛的信息
 * 显示整牛信息()
 * 
 * 要求:创建一头牛,显示整头牛的信息
 * 运行结果参考:
 * 牛腿颜色:黑白,牛腿长:50
 * 整牛高:100,整牛重:500
 *
 */
class Cow{
    double height;
    double weight;
    public Cow(double weight,double height){
        this.weight = weight;
        this.height = height;
    }
    
    class CowLeg{
        String color;
        double length;
        public CowLeg(String color,double length) {
            this.color = color;
            this.length = length;
        }
        void show(){
            System.out.println("牛腿的颜色是"+color+"牛腿的长度是"+length);
        }
    }
    
    public void display(){
        CowLeg leg = new CowLeg("黑白", 50);
        leg.show();
        System.out.println("整牛高"+height+"整牛重"+weight);    
    }
}

public class Test02 {

    public static void main(String[] args) {
        Cow a = new Cow(500, 100);
        a.display();
        //顶级类调用内部类方法
        Cow.CowLeg leg = a.new CowLeg("黑色", 100);
        leg.show();

    }

}

 

posted @ 2019-04-29 21:03  Geek张东坡  阅读(480)  评论(0编辑  收藏  举报