方法的回顾和加深(Review and deepening of methods)

方法回顾和加深

方法的定义

方法:是类或类的实例可以执行的操作或者说具有的行为。方法总是定义在类的内部,是隶属于类的。java的任何操作型语句,只能出现在类体中的方法中。

根据方法名,我们分为构造方法普通方法

构造方法:定义类时,类体中有一个名称和类名一模一样的方法–构造方法。除此之外的方法,都是普通方法。

普通方法的设计建议遵循以下的约定:

  • 以动词开头。例如getArea,setAbove,compute等等。
  • 尽可能减少方法的参数。
  • 充分利用方法重载的机制,减少方法名称种数。
    /*
    修饰符 返回值类型 方法名(...){
    //方法体
    return 返回值;
    }
     */

break:跳出switch,结束循环;return:结束方法,返回一个结果。

方法名:首字母小写加驼峰规则,见名知意

参数列表:(参数类型,参数名)…

异常抛出:疑问,后面讲解

public void readFile(String file) throws IOException{

    }

方法的调用

静态方法 static

静态方法

值传递和引用传递

值传递和引用传递

java只有值传递,可参考文章深入理解。

参数传递规则为:

1.对于简单数据类型(如boolean、整型、浮点型、char)的参数,是简单数据类型变量的值的拷贝;

2.对于引用类型(数组和类(class)类型)的参数,是数组或者对象的值的拷贝。

package com.oop.Test01;
//引用传递:对象,本质还是值传递
//对象,内存!
public class Mytest05 {
    public static void main(String[] args){
        Person person = new Person();
        System.out.println(person.name);//null
        change(person);
        System.out.println(person.name);
    }
    public static void change(Person person){
        //person是一个对象:指向的是 --->  Person person = new Person();这是一个具体的人,可以改变属性
        person.name = "朵儿";
    }
}
//person类,属性:name
class Person{
    String name;
}

this关键字

前面博客中有介绍。

博客园:https://www.cnblogs.com/whitebunny/p/16121496.html

csdn:https://blog.csdn.net/qq_51607772/article/details/124082885?spm=1001.2014.3001.5501

————————

Methods review and deepen

Definition of method

Method: it is the operation or behavior that a class or an instance of a class can perform. Methods are always defined inside the class and belong to the class. Any operational statement of Java can only appear in the method in the class body.

According to the method name, we can divide it into < strong > construction method < / strong > and < strong > General method < / strong >.

construction method: when defining a class, there is a method in the class body with the same name as the class – construction method. Other methods are ordinary methods

The design recommendations of common methods follow the following conventions:

  • 以动词开头。例如getArea,setAbove,compute等等。
  • Minimize the parameters of the method.
  • Make full use of the mechanism of method overloading to reduce the number of method names.
    /*
    修饰符 返回值类型 方法名(...){
    //方法体
    return 返回值;
    }
     */

Break: jump out of the switch and end the loop; Return: end the method and return a result.

Method name: initial lowercase plus hump rule, see the meaning of the name

Parameter list: (parameter type, parameter name)

Exception thrown: questions, explained later

public void readFile(String file) throws IOException{

    }

Method call

静态方法 static

静态方法

Value passing and reference passing

值传递和引用传递

< strong > java has only value passing < / strong >, which can be understood in depth by referring to the article.

Parameter transfer rules are:

1. For parameters of simple data type (such as boolean, integer, floating point and char), it is a copy of the value of the variable of simple data type;

2. For parameters of reference type (array and class type), it is a copy of the value of array or object.

package com.oop.Test01;
//引用传递:对象,本质还是值传递
//对象,内存!
public class Mytest05 {
    public static void main(String[] args){
        Person person = new Person();
        System.out.println(person.name);//null
        change(person);
        System.out.println(person.name);
    }
    public static void change(Person person){
        //person是一个对象:指向的是 --->  Person person = new Person();这是一个具体的人,可以改变属性
        person.name = "朵儿";
    }
}
//person类,属性:name
class Person{
    String name;
}

this关键字

It is introduced in the previous blog.

博客园:https://www.cnblogs.com/whitebunny/p/16121496.html

csdn:https://blog.csdn.net/qq_51607772/article/details/124082885?spm=1001.2014.3001.5501