NO.A.0006——day02——JAVA.方法入门

一、方法入门:
方法:就是将一个功能抽取出来;把代码单独定义在一个大括号内;形成一个单独的功能。
当我们需要这个功能的时候,就可以去调用,这样即实现了代码的重复性,也解决了代码冗余的现象。
代码库:Demo11Method.java
/*
定义一个方法的格式:
public static void 方法名称() {
    方法体
}

方法名称的命名规则和变量一样,使用小驼峰。
方法体:也就是大括号当中可以包含任意条语句。

注意事项:
1. 方法定义的先后顺序无所谓。
2. 方法的定义不能产生嵌套包含关系。
3. 方法定义好了之后,不会执行的。如果要想执行,一定要进行方法的【调用】。

如何调用方法,格式:
                方法名称();
*/
public class Demo11Method {
    
    public static void main(String[] args) {
        farmer(); // 调用农民的方法
        seller(); // 调用小商贩的方法
        cook(); // 调用厨子的方法
        me(); // 调用我自己的方法
    }
    
    // 厨子
    public static void cook() {
        System.out.println("洗菜");
        System.out.println("切菜");
        System.out.println("炒菜");
        System.out.println("装盘");
    }
    
    // 我
    public static void me() {
        System.out.println("吃");
    }
    
    // 小商贩
    public static void seller() {
        System.out.println("运输到农贸市场");
        System.out.println("抬高价格");
        System.out.println("吆喝");
        System.out.println("卖给厨子");
    }
    
    // 农民伯伯
    public static void farmer() {
        System.out.println("播种");
        System.out.println("浇水");
        System.out.println("施肥");
        System.out.println("除虫");
        System.out.println("收割");
        System.out.println("卖给小商贩");
    }
}
 
 
 
 
 






 

Walter Savage Landor I    strove with none,   for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                               ——W.S.Landor

 

<wiz_marker id="wiz-painter-root" style="">

 



posted on   yanqi_vip  阅读(61)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示