JAVA Foundation-Chapter 4 Idea, Method

Today's content

Integrated development tool IDEA
method parameters and return value
method overload

teaching objectives

掌握idea开发工具的安装
能够使用idea创建Java工程
能够使用idea创建包和类
了解idea的Java工程目录
了解idea字体的设置
掌握idea常用开发快捷键
了解项目的导入和删除
掌握方法定义的参数和返回值含义
了解方法的调用过程
了解方法的三种调用形式
掌握方法定义的注意事项
掌握方法重载的概念
能够判断出方法的重置

Chapter One Development Tools IntelliJ IDEA

1. 1 Overview of development tools

IDEA is an integrated development tool (IDE) specifically for Java, written in Java language. Therefore, you need to have a JRE running environment and configure the environment variables.
It can greatly improve our development efficiency. Can automatically compile and check for errors. In the company, IDEA is used for development.

1. 2 IDEA software installation

This software integrates 32-bit and 64-bit, double-click ideaIU- 2017. 3. 2 .exe to enter the installation.

1. 欢迎界面

2. Select the installation path

3. Configure installation options

4. Start Menu

5. Installation is complete

IDEA development tools are installed

1. 3 IDEA first drive

1. Choose not to import any settings, click OK

2. 选择 Create New Project
3. 点击new 按钮,配置安装的JDK 9 版本

Select the JDK 9 directory and click OK

4. Do not use templates

  1. Name the project demo and store it in the d:\ideawork\demo directory. If the d drive does not have this directory, it will be created automatically.
首次新建项目时,默认的Project Location路径有问题,如 c:\\xxx,正确写法为c:\xxx。更改后不会再
出现此类问题。
  1. Open a Daily Post dialog box, tick off each startup display, click close
7. IDEA的工作界面,我们的项目已经创建好了,如果再新建项目,点击File->new->Project

1. 4 Create packages and classes

1. 展开创建的工程,在源代码目录src 上,鼠标右键,选择new->package ,键入包名com.itheima.demo ,
点击确定。

Right-click com.itheima.demo, select Show in Explorer, and you will find the directory structure of the created package.

Visible com.itheima.demo, which means that a multi-level folder has been created.

小贴士:所谓包,就是文件夹,用来对类文件进行管理。
2. 在创建好的包上,鼠标右键,选择 new->class 创建类,键入类名。
3. 在代码编辑区,键入主方法,并输出HelloWorld 。
4. 运行程序,在代码编辑区鼠标右键,选择Run HelloWorld 即可,或在菜单中选择Run->Run HelloWorld 。

1.5 Font settings

The default font of the IDEA tool is very small, and the output fonts of the code editor and console need to be adjusted.

点击菜单栏上的File->Settings->Editor->Font修改字体。

1. 6 IDEA project directory

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

1. 7 IDEA commonly used shortcut keys

Shortcut function

Alt+Enter 导入包,自动修正代码
Ctrl+Y 删除光标所在行
Ctrl+D 复制光标所在行的内容,插入光标位置下面
Ctrl+Alt+L 格式化代码
Ctrl+/ 单行注释
Ctrl+Shift+/ 选中代码注释,多行注释,再按取消注释
Alt+Ins 自动生成代码,toString,get,set等方法
Alt+Shift+上下箭头 移动当前代码行

1. 8 IDEA modify shortcut keys

In the IDEA tool, the shortcut key of Ctrl+space can help us complete the code, but this shortcut
key conflicts with the shortcut key for switching the input method in Windows, and the shortcut key in IDEA needs to be modified.

File->Settings->keymap->Main menu->code->Completion->Basic

Double-click Basic->remove->Ctrl+Space

Double-click Basic->Add Keyboard->Type Alt+/->Click OK

1. 9 IDEA import and close the project

Close the existing project in IDEA, File->Close Project

File->Close Project这时IDEA回到了刚启动界面,点击项目上的X,IDEA中就没有这个项目了

On the startup interface of IDEA, click OPEN and select the project directory

Tips:

课后若想通过IDEA同时开启多个项目,点击OPEN打开项目时,点击New Window按钮

Chapter 2 Methods

2. 1 Review-Method definition and call

In the previous lessons, I used nested loops to output rectangles, and the console printed out the rectangles. Therefore, the method was defined as void and there was no return value.
It is called directly in the main method main.

print方法被main方法调用后直接输出结果,而main方法并不需要print方法的执行结果,所以被定义为
void。

2. 2 Detailed explanation of the format of the definition method^

修饰符: public static 固定写法
返回值类型: 表示方法运行的结果的数据类型,方法执行后将结果返回到调用者
参数列表:方法在运算过程中的未知数据,调用者调用方法时传递
return:将方法执行后的结果带给调用者,方法执行到return ,整体方法运行结束
小贴士:return 结果; 这里的"结果"在开发中,我们正确的叫法成为方法的返回值

2.3 Two clear definition methods

public class Method_Demo 1  {
public static void main(String[] args) {
print();
}
private static void print() {
for (int i =  0 ; i <  5 ; i++) {
for (int j =  0 ; j <  8 ; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Modifier return value type method name (parameter list) {

//Code omitted...

return 结果;
}

2.3 Two clear definition methods

Requirements: Define a method to calculate the sum of two integers.

明确返回值类型:方法计算的是整数的求和,结果也必然是个整数,返回值类型定义为int类型。
明确参数列表:计算哪两个整数的和,并不清楚,但可以确定是整数,参数列表可以定义两个int类型的
变量,由调用者调用方法时传递

Program execution, the main method main calls getSum method, the actual data transfer 5 and 6, two variables a and b is the actual parameter received, and
will return the result of calculation, the main method of the received main variables sum method is The return value.

2. 4 Flow chart of calling method

2. 5 definition method exercise

Exercise one

public class Method_Demo 2  {
public static void main(String[] args) {
// 调用方法getSum,传递两个整数,这里传递的实际数据又称为实际参数
// 并接收方法计算后的结果,返回值
int sum = getSum( 5 ,  6 );
System.out.println(sum);
}
/*
定义计算两个整数和的方法
返回值类型,计算结果是int
参数:不确定数据求和,定义int参数.参数又称为形式参数
*/
public static int getSum(int a, int b) {
return a + b;
}
}

Compare two integers for the same

Analysis: To define the method to realize the function, there need to be two clear, namely the return value and the parameter list.

Clear return value: compare integers, the result of the comparison is only two possibilities, the same or different, so the result is of Boolean type, and the result of the comparison is the same

同为true。
明确参数列表:比较的两个整数不确定,所以默认定义两个int类型的参数。

Exercise two

Calculate the sum of 1 + 2 + 3… + 100

Analysis: To define the method to realize the function, there need to be two clear ones, namely the return value and the parameter.

明确返回值: 1 ~ 100 的求和,计算后必然还是整数,返回值类型是int
明确参数:需求中已知到计算的数据,没有未知的数据,不定义参数
public class Method_Demo 3  {
public static void main(String[] args) {
//调用方法compare,传递两个整数
//并接收方法计算后的结果,布尔值
boolean bool = compare( 3 ,  8 );
System.out.println(bool);
}
/*
定义比较两个整数是否相同的方法
返回值类型,比较的结果布尔类型
参数:不确定参与比较的两个整数
*/
public static boolean compare(int a, int b) {
if (a == b) {
return true;
} else {
return false;
}
}
}
public class Method_Demo 4  {
public static void main(String[] args) {
//调用方法getSum
//并接收方法计算后的结果,整数
int sum = getSum();
System.out.println(sum);
}
/*
定义计算 1 ~ 100 的求和方法
返回值类型,计算结果整数int
参数:没有不确定数据
*/
public static int getSum() {

Exercise three

Achieve indefinite printing

Analysis: To define the method to realize the function, there need to be two clear ones, namely the return value and the parameter.

明确返回值:方法中打印出HelloWorld即可,没有计算结果,返回值类型void。^
明确参数:打印几次不清楚,参数定义一个整型参数

2. 6 Notes on the definition method

Define the location, outside the method in the class.

返回值类型,必须要和return语句返回的类型相同,否则编译失败 。
不能在return 后面写代码,return 意味着方法结束,所有后面的代码永远不会执行,属于无效代码。

//Define variables to save the sum

int sum =  0 ;
//从 1 开始循环,到 100 结束
for (int i =  1 ; i <=  100 ; i++) {
sum = sum + i;
}
return sum;
}
}
public class Method_Demo 5  {
public static void main(String[] args) {
//调用方法printHelloWorld,传递整数
printHelloWorld( 9 );
}
/*
定义打印HelloWorld方法
返回值类型,计算没有结果 void
参数:不确定打印几次
*/
public static void printHelloWorld(int n) {
for (int i =  0 ; i < n; i++) {
System.out.println("HelloWorld");
}
}
}
// 返回值类型要求是int
public static int getSum() {
return  5 ;// 正确,int类型
return  1. 2 ;// 错误,类型不匹配
return true;// 错误,类型不匹配
}

2.7 Three forms of calling method

Direct call: directly write method name call

Assignment call: call the method, define variables in front of the method, and receive the return value of the method

Output statement call:

在输出语句中调用方法,System.out.println(方法名()) 。
不能用输出语句调用void 类型的方法。因为方法执行后没有结果,也就打印不出任何内容。

2. 8 method overloading

public static int getSum(int a,int b) {
return a + b;
System.out.println("Hello");// 错误,return已经结束,这里不会执行,无效代码
}
public static void main(String[] args) {
print();
}
public static void print() {
System.out.println("方法被调用");
}
public static void main(String[] args) {
int sum = getSum( 5 , 6 );
System.out.println(sum);
}
public static int getSum(int a,int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(getSum( 5 , 6 ));
}
public static int getSum(int a,int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(printHello());// 错误,不能输出语句调用void类型方法
}
public static void printHello() {
System.out.println("Hello");
}

Method overloading: It means that in the same class, more than one method with the same name is allowed, as long as their parameter lists are different, which is different from modifiers and return

The return type is irrelevant.

Parameter list: different numbers, different data types, and different orders.

Overloaded method call: JVM calls different methods through the parameter list of the method.

2.9 Method overload practice

Exercise one

Compare whether two data are equal. The parameter types are two byte types, two short types, two int types, and two long types, and
are tested in the main method.

public class Method_Demo 6  {
public static void main(String[] args) {
//定义不同数据类型的变量
byte a =  10 ;
byte b =  20 ;
short c =  10 ;
short d =  20 ;
int e =  10 ;
int f =  10 ;
long g =  10 ;
long h =  20 ;
// 调用
System.out.println(compare(a, b));
System.out.println(compare(c, d));
System.out.println(compare(e, f));
System.out.println(compare(g, h));
}
// 两个byte类型的
public static boolean compare(byte a, byte b) {
System.out.println("byte");
return a == b;
}
// 两个short类型的
public static boolean compare(short a, short b) {
System.out.println("short");
return a == b;
}
// 两个int类型的
public static boolean compare(int a, int b) {
System.out.println("int");
return a == b;
}
// 两个long类型的
public static boolean compare(long a, long b) {
System.out.println("long");
return a == b;

Exercise two

Determine which methods are overloaded.

Exercise three

To simulate the effect of the println method in the output statement, what type of data is passed will output what type of data. Only one method name
println is allowed to be defined .

}

}

public static void open(){}
public static void open(int a){}
static void open(int a,int b){}
public static void open(double a,int b){}
public static void open(int a,double b){}
public void open(int i,double d){}
public static void OPEN(){}
public static void open(int i,int j){}
public class Method_Demo 7  {
public static void println(byte a) {
System.out.println(a);
}
public static void println(short a) {
System.out.println(a);
}
public static void println(int a) {
System.out.println(a);
}
public static void println(long a) {
System.out.println(a);
}
public static void println(float a) {
System.out.println(a);
}
public static void println(double a) {
System.out.println(a);
}
public static void println(char a) {
System.out.println(a);
}
public static void println(boolean a) {
System.out.println(a);

}

public static void println(String a) {
System.out.println(a);
}
}

Guess you like

Origin blog.csdn.net/weixin_43419256/article/details/108198297