Self Java, day04_ Idea, method

IntelliJ IDEA development tools

Development Tools Overview IDEA is a specific Java integrated development tools (IDE), written in Java. So, we need to have JRE runtime environment and configure the environment variables. It can greatly improve our development efficiency. It can automatically compile, check for errors. In the company, it is the use of IDEA for development.

IDEA software installation

Enter the official website to download the version we need, this software integrates a 32-bit and 64-bit, double-click ideaIU-2017.3.2.exe into the installation.

  • 1. Welcome Screen
  • 2. Select the installation path
  • 3. Configure Installation Options

 

  • 4. Start Menu
  • 5. installation

IDEA project structure

As shown below

IDEA first drive

  • 1. choose not to import any settings, click OK
  • 2. Select Create New Project
  • 3. Click the new button to configure the installed version JDK9

 

 

  • 4. Do not use a template
  • The demo for the project a name, and stored in d: \ ideawork \ directory under demo, if d drive this directory is not automatically created. The first time you create a new project, the default Project Location path problems, such as c: \\ xxx, correct wording is c: \ xxx. Such problems will not occur again after the change.

  • 6. Open a day quote dialog box, check off each time you start the display, click close
  • 7. IDEA interface work, our project has been created, if the new project, click on File-> new-> Project 

Creating packages and classes

  • 1. Expand the project created on the source code directory src, right click and select new-> package, type the package name com.itheima.demo, click OK.
  • 2. On the created package, right click and select new-> class create a class, type the name of the class.
  • 3. In the code editing area, the main type method, and outputs the HelloWorld. 
  • 4. Run the program in the right-code editing area, select Run HelloWorld to, or select Run-> Run HelloWorld menu.

Font settings

DEA工具的默认字体非常小,代码编辑器和控制台的输出字体都需要进行调整。
  • 点击菜单栏上的 File->Settings->Editor->Font 修改字体。

 

IDEA的项目目录

我们创建的项目,在d:\ideawork目录的demo下
  • .idea 目录和 demo.iml 和我们开发无关,是IDEA工具自己使用的
  • out 目录是存储编译后的.class文件
  • src 目录是存储我们编写的.java源文件 

IDEA常用快捷键

IDEA修改快捷键 

在IDEA工具中, Ctrl+空格 的快捷键,可以帮助我们补全代码,但是这个快捷键和Windows中的输入法切换快捷键冲突,需要修改IDEA中的快捷键。
  • File->Settings->keymap->Main menu->code->Completion->Basic 

 

IDEA导入和关闭项目 

 

  • 在IDEA的启动界面上,点击 OPEN ,选择项目目录即可 
  • 若想通过IDEA同时开启多个项目,点击OPEN打开项目时,点击New Window按钮 

方法

概述 

我们在学习运算符的时候,都为每个运算符单独的创建一个新的类和main方法,我们会发现这样编写代码非常的繁琐,而且重复的代码过多。能否避免这些重复的代码呢,就需要使用方法来实现。方法:就是将一个功能抽取出来,把代码单独定义在一个大括号内,形成一个单独的功能。当我们需要这个功能的时候,就可以去调用。这样即实现了代码的复用性,也解决了代码冗余的现象。 

方法的定义

格式:

格式详解:

  • 修饰符:控制方法的访问权限及状态的关键字,现在一般使用 public static 固定写法
  • 返回值类型: 表示方法运行的结果的数据类型,方法执行后将结果返回到调用者
  • 方法名称:方法的名字,规则和变量一样,小驼峰
  • 参数列表:方法在运算过程中的未知数据,调用者调用方法时传递
  • 方法体:方法需要做的事情,若干行代码
  • return:将方法执行后的结果带给调用者,方法执行到 return ,整体方法运行结束
  • 结果:也就是方法执行后最终产生的数据结果

注意:return后面的“返回值”,必须和方法名称前面的“返回值类型”,保持对应。

参数列表详解:

  • 参数类型:进入方法的数据是什么类型
  • 参数名称:进入方法的数据对应的变量名称,参数如果有多个,使用逗号进行分隔
  • 形式参数 :定义方法的时候写的变量
  • 实际参数 :调用方法的时候,传递的实实在在的参数

方法的三种调用格式

  • 单独调用:方法名称(参数);
  • 赋值调用:调用方法,在方法前面定义变量,接收方法返回值 
  • 输出语句调用:在输出语句中调用方法, System.out.println(方法名()) 。 

注意事项:

  • 对于有返回值的方法,可以使用单独调用、打印调用或者赋值调用。
  • 但是对于无返回值的方法,只能使用单独调用,不能使用打印调用或者赋值调用。

需求

定义一个两个int数字相加的方法。

代码实现

package demo02;


/*
方法其实就是若干语句的功能集合。

参数:就是进入方法的数据。
返回值:就是从方法中出来的数据。

定义一个两个int数字相加的方法,2个明确:
明确:返回值类型:int
明确:参数列表:int a, int b

*/
public class Demo02MethodDefine {

    public static void main(String[] args) {
        // 单独调用
        sum(10, 20);

        // 打印调用
        System.out.println(sum(10, 20));

        // 赋值调用
        int number = sum(15, 25);
        number += 100;
        System.out.println("变量的值:" + number);
    }

    public static int sum(int a, int b) {
        int result = a + b;
        return result;
    }

}

代码执行后的结果

定义方法的两个明确

明确返回值类型:
  • 有返回值:当一个方法运行完成之后,得到的功能结果,需要返回给方法的调用处的时候,就必须定义返回值类型。例如:定义一个方法,用来【求出】两个数字之和。(你帮我算,算完之后把结果告诉我。)
  • 无返回值:当一个方法运行完成之后,得到的功能结果,无需返回给方法的调用处的时候,就无需定义返回值类型。例如:定义一个方法,用来【打印】两个数字之和。(你来计算,算完之后你自己负责显示结果,不用告诉我。)
明确参数列表:
  • 有参数:当一个方法需要一些外界的数据条件,才能完成方法功能的时候,就可以定义为有参数的方法(定义方法的时候小括号当中有参数列表)。例如两个数字相加,必须知道两个数字是各自多少,才能相加。
  • 无参数:当一个方法不需要任何外界数据条件,就能完成方法功能的时候,就可以定义为无参数的方法(定义方法的时候小括号当中留空)。例如定义一个方法,打印固定10次HelloWorld。

调用方法的流程图解 

需求

定义一个方法,用来求出1-100之间所有数字的和值。

代码实现

public class Demo02MethodSum {

    public static void main(String[] args) {
        System.out.println("结果是:" + getSum());//
    }

    /*
    三要素
    返回值:有返回值,计算结果是一个int数字
    方法名称:getSum
    参数列表:数据范围已经确定,是固定的,所以不需要告诉我任何条件,不需要参数
     */
    public static int getSum() {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        return sum;
    }

}

需求

定义一个方法,用来打印指定次数的HelloWorld。

代码实现

public class Demo03MethodPrint {

    public static void main(String[] args) {
        printCount(5);
    }

    /*
    三要素
    返回值类型:只是进行一大堆打印操作而已,没有计算,也没有结果要告诉调用处
    方法名称:printCount
    参数列表:到底要打印多少次?必须告诉我,否则我不知道多少次,没法打印。次数:int
     */
    public static void printCount(int num) {
        for (int i = 0; i < num; i++) {
            System.out.println("Hello, World!" + (i + 1));
        }
    }

}

代码执行后的结果

定义方法的注意事项

  • 方法应该定义在类当中,但是不能在方法当中再定义方法。不能嵌套。
  • 方法定义的前后顺序无所谓。
  • 方法定义之后不会执行,如果希望执行,一定要调用:单独调用、打印调用、赋值调用。
  • 如果方法有返回值,那么必须写上“return 返回值;”,不能没有。
  • return后面的返回值数据,必须和方法的返回值类型,对应起来。
  • 对于一个void没有返回值的方法,不能写return后面的返回值,只能写return自己。
  • 对于void方法当中最后一行的return可以省略不写。
  • 一个方法当中可以有多个return语句,但是必须保证同时只有一个会被执行到,两个return不能连写。

 代码演示方法的注意事项

public class Demo04MethodNotice {

    public static int method1() {
        //返回值类型必须和return 后面的结果类型一致
        return 10;
    }

    public static void method2() {
//        return 10; // 错误的写法!方法没有返回值,return后面就不能写返回值。
        return; // 没有返回值,只是结束方法的执行而已。
    }

    public static void method3() {
        System.out.println("AAA");
        System.out.println("BBB");
//        return; // 最后一行的return可以省略不写。
    }

    public static int getMax(int a, int b) {

        //一个方法当中可以有多个return语句,但是必须保证同时只有一个会被执行到,两个return不能连写。
        if (a > b) {
            return a;
        } else {
            return b;
        }
    }

}

方法重载

方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关。

方法重载与下列因素相关:

  • 参数个数不同
  • 参数类型不同
  • 参数的多类型顺序不同

方法重载与下列因素无关:

  • 与参数的名称无关
  • 与方法的返回值类型无关

重载方法调用:JVM通过方法的参数列表,调用不同的方法

代码演示方法重载的基本使用

package demo04;


/*
题目要求:
比较两个数据是否相等。
参数类型分别为两个byte类型,两个short类型,两个int类型,两个long类型,
并在main方法中进行测试。
 */
public class Demo02MethodOverloadSame {

    public static void main(String[] args) {
        byte a = 10;
        byte b = 20;
        System.out.println(isSame(a, b));

        System.out.println(isSame((short) 20, (short) 20));

        System.out.println(isSame(11, 12));

        System.out.println(isSame(10L, 10L));
    }

    public static boolean isSame(byte a, byte b) {
        boolean same;
        if (a == b) {
            same = true;
        } else {
            same = false;
        }
        return same;
    }

    public static boolean isSame(short a, short b) {
        boolean same = a == b ? true : false;
        return same;
    }

    public static boolean isSame(int a, int b) {
        return a == b;
    }

    public static boolean isSame(long a, long b) {
        if (a == b) {
            return true;
        } else {
            return false;
        }
    }

}

代码执行后的结果

 

 

 

 

Guess you like

Origin www.cnblogs.com/wurengen/p/12127631.html