Java 面向对象 之 静态内部类

转载于 : http://www.verejava.com/?id=16992860269360

/**
    知识点: 内部类
        1. 内部类的定义
        2. 内部类的优缺点
        3. 外部类怎么实例化其他类的内部类
        4. 方法中定义内部类
            如果在方法中定义内部类,方法中的内部类要访问变量, 需要加final 关键字
        5. static 声明内部类

    题目:母亲怀了孕, 母亲的营养决定孩子的健康成长
    思路:
        1.  抽象出类 :  母亲(Mother), 孩子(Baby)
        2.  找出类的关系: 孩子在母亲里面 Baby in Mother
        3.  抽象出方法: 母亲吃东西 (eat)
*/
public class InnerClass3 {

    public static void main(String[] args) {
        //实例化母亲
        Mother mother = new Mother("lucy");
        //母亲吃苹果
        mother.eat("苹果");

        //孩子吸收营养
        //Mother.Baby baby=new Mother.Baby();
        //baby.eat();
        Mother.Baby.eat();

    }
}

class Mother {

    private String name;
    private static String food;//母亲吃的食物

    public Mother(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //母亲吃东西
    public void eat(String food) {
        this.food = food;
        System.out.println(this.name + " 吃了 " + this.food);

    }

    static class Baby {
        //孩子从母亲吃的东西中吸收营养
        public static void eat() {
            System.out.println("孩子从母亲吃的 " + food + " 中吸收营养");
        }
    }

}

http://www.verejava.com/?id=16992860269360

转载于:https://blog.51cto.com/11842410/2171925

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值