【java 学习笔记4】java学习软件IDEA&&方法&&方法重载

一、IDEA

1.下载方式

可自行去某宝搜索,或者按照网上教程下载

2.快捷键

1Alt + Enter : 导入包,自动修复代码(不包括书写错误)

2Ctrl + Y:删除光标所在行

3Ctrl + D : 复制光标所在行的内容,并插入到光标位置下面

4Ctrl + Alt +L :格式化代码(对齐,规范代码格式)

5Ctrl + Z : 退回到上一步操作

6Ctr + / :单行注释
7Ctr + Shift + / :多行注释
注意:重复操作即可取消注释

8Atl + Ins :自动生成代码,toString,get,set等方法

9Alt + Shift + 上下箭头:移动当前代码行

10Fiie --> setting: 可以进行字体(Fonts)、快捷键(keymap)等修改

11、导入ModulesFile --> Project Settings --> Modules -->点击左上方+-->选择Import Module -->选择Module文件所在位置

二、方法

1.方法定义

  • 方法是若干语句的功能集合
  • 方法包括:
    A、参数:就是进入方法的数据
    B、返回值:就是从方法中出来的数据
  • 定义方法的完整格式:
    修饰符 返回值类型 方法名称 (参数类型 参数名称,…){
    方法体
    return 返回值;
    }
	public static 方法名称 (参数类型 参数名称,...){
				方法体;
	}

修饰符:现阶段的固定写法:public static
return :两个作用,第一停止当前方法,第二将后面的返回值还给调用处

2.方法调用的三种格式

  • 单独调用:方法名称(参数);
  • 打印调用:System.out.println(方法名称(参数));
  • 赋值调用:变量类型 变量名称 = 方法名称(参数);

代码示例:

 * 求最大值
 *
 */
public class MethodDemo01 {
    public static void main(String[] args) {
        //单独调用
        operator3();

        //打印调用
        System.out.println(operator3());
        
        //赋值调用
        int a = operator3();
    }
    // 求三个数的最大值
    public static int operator3(){
        int a = 1;
        int b = 9;
        int c = 3;
        int max;
        max = a > b ? a : b;
        max = max > c ? max : c;
       return max;
    }
}

3.方法重载

  • 方法重载(Overload):多个方法的名称一样,但是参数列表不同
    例如:
	sum(int a)  
	sum(double a)
	sum(int a,double b)
	sum(double b,int a)
  • 方法重载的相关因素
    A、参数个数不同
    B、参数类型不同
    C、不同类型的参数顺序不同
  • 方法重载的不相关因素
    A、返回值类型
    B、参数名称
下面
两个方法并不是方法重载,如果同时出现会出现错误
public static int sum(int a){}
public static double sum(int b){}

代码示例:


/**
 * 
 * 方法的重载:存在一个以上的重名方法,但是参数列表不同,
 *
 * 求最大值
 */
public class MethodDemo02 {
    public static void main(String[] args) {
        int x = 1;
        int y = 9;
        int z = 3;
        System.out.println("int = "+operator3(x,y,z));
        System.out.println("double = "+operator3(1.0,2.0,3.0));
    }
    public static int operator3(int a,int b ,int c){
        int max = a > b ? a : b;
        max = max > c ? max : c;
        return max;
    }
    public static double operator3(double a, double b,double c){
        double max;
        max = a > b ? a : b;
        max = max > c ? max : c;
        return max;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值