Java 面向对象 之 方法中内部类

Wesley13
• 阅读 405

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

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

    题目:母亲怀了孕, 母亲的营养决定孩子的健康成长
    思路:
        1.    抽象出类 :  母亲(Mother), 孩子(Baby)
        2.  找出类的关系: 孩子在母亲里面 Baby in Mother
        3.  抽象出方法: 母亲吃东西 (eat)
*/
public class InnerClass2
{
    public static void main(String[] args)
    {
        //实例化母亲
        Mother mother=new Mother("lucy");
        //母亲吃苹果
        mother.eat("苹果");

    }
}
class Mother
{
    private String name;
    private 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(final String food)
    {
        this.food=food;
        System.out.println(this.name+" 吃了 "+this.food);
        class Baby
        {
            /**
                孩子从母亲吃的东西中吸收营养
            */
            public void eat()
            {
                System.out.println("孩子从母亲吃的 "+food+" 中吸收营养");
            }
        }
        //母亲吃东西的同时 孩子也吃东西
        new Baby().eat();
    }

    
}

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

点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
Appscan的下载安装
1、下载Appscan:http://download2.boulder.ibm.com...2AppScan\_Setup.exe(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fdownload2.boulder.ibm.com%2Fsar%2FCMA%2FRAA%2F00jq2
Wesley13 Wesley13
2年前
4. Nginx模块
Nginx官方模块1.ngx\_http\_stub\_status\_modulehttp://nginx.org/en/docs/http/ngx\_http\_stub\_status\_module.html。(https://www.oschina.net/action/GoToLink?urlhttp%3A%2
Wesley13 Wesley13
2年前
P2P技术揭秘.P2P网络技术原理与典型系统开发
Modular.Java(2009.06)\.Craig.Walls.文字版.pdf:http://www.t00y.com/file/59501950(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.t00y.com%2Ffile%2F59501950)\More.E
Wesley13 Wesley13
2年前
Uber基于RNN的极端事件预测,解决交通问题
时间 2017061212:00:15  亿欧网(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.tuicool.com%2Fsites%2FAJj2Un)_原文_  http://www.iyiou.com/p/47628(https://www.oschina.n
Wesley13 Wesley13
2年前
Java 面向对象 之 Object 类
<ahref"http://www.verejava.com/?id16992832835152"http://www.verejava.com/?id16992832835152(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.verejava.com%2F%3Fid%
Stella981 Stella981
2年前
Cubieboard 3(cubietruck) 安装 java 环境
Cubieboard3 刷机过程 请看 http://name5566.com/4398.html(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fname5566.com%2F4398.html)安装java环境下载 armjdkh
Easter79 Easter79
2年前
Swift项目兼容Objective
!456.jpg(http://static.oschina.net/uploads/img/201509/13172704_1KcG.jpg"1433497731426906.jpg")本文是投稿文章,作者:一叶(博客(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2F00red
Wesley13 Wesley13
2年前
Java中静态代码块、构造代码块、构造函数、普通代码块
转载来源http://www.cnblogs.com/ysocean/(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.cnblogs.com%2Fysocean%2F)1、静态代码块①、格式  在java类中(方法中不能存在静态代码块)使用s
Wesley13 Wesley13
2年前
Java 正则表达式 之 分割
<ahref"http://www.verejava.com/?id16994950342031"http://www.verejava.com/?id16994950342031(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.verejava.com%2F%3Fid%
Wesley13 Wesley13
2年前
Java多线程同步器
Java多线程同步器博客分类:java转自:http://www.talentdigger.cn/home/space.php?uid2189&dothread&id4129(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.talentdigger.cn%2Fho