java methods (Methods)

  1. Methods (Methods)
    is designed to use certain functions of the program to help solve problems. In other programming languages, we call Java's "methods" subroutines / functions.
    1.1 Definition of methods
    Java is an object-oriented language. Its methods are the same as variables. Each method has its own class object. The class integrates multiple methods to form a class object with specific functions. Each method can also be regarded as a block. When using the method, pay attention to its class object properties, parameters and return value rules, namely

1. In the class, create a method (Declare) to create (Methods).
2. If the declared method does not execute return data, it must be preceded by void; if it is to execute return data, it must not be preceded by void. There must be a return in the content. In short, void and return cannot coexist.
3. When using methods, you must first generate a new object with the new class and then run the method in the new object.
4. When using the method, pay attention to the parameters between the calling program and the called program, which must be consistent, that is (a) The parameter types must be consistent. If this type of rule is violated, the program will not run.

class Study {
	int credit = 0;
	void addcredit(int i) {
		credit +=i;
	}
	int totalcredit() {
		return credit;
	}
}

public class Student {

	public static void main(String[] args) {
	Test a = new Test();
	Test b = new Test();
	a.addcredit(12);
	b.addcredit(9);
	b.addcredit(6);
	System.out.println("a.addcredit:"+a.totalcredit());//12
	System.out.println("b.addcredit:"+b.totalcredit());//15
	}

}

1.2 Instance Methods (Intance Methods)
This method can only be used when the class object to which the method belongs exists, so that the method that exists with the existence of the object is called an "instance method".

public class Number {
	int n=0;
	void addN(int i) {
		n=i;
	}
	int getN() {
		return n;
	}
}

public static void main(String[] args) {
		Number N;		//删除后报错
		N = new Number();//删除报错
		N.addN(10);
		System.out.println("使用方法 N.getN:"+N.getN());//10
	}
  1. Class methods (Class Methods)
    when the method is declared, the "static" prefix can keep the declared method always exists, without having to go through the process of generating new objects, can be directly run, called "class methods".
public class Number {
	static int n=0;
	static void addN(int i) {
		n=i;
	}
	static int getN() {
		return n;
	}
}

public static void main(String[] args) {

		Number.addN(10);
		System.out.println(" N.getN:"+Number.getN());//10
		
		Number.n = 20;
		System.out.println("Number.n:"+Number.n);//20
	}

Compared with instance methods, class methods can no longer be new objects, and you can directly use variables in static.

  1. Constructor (Constructor)
    class constructor features are:

1. The name of the constructor must be the same as the name of the class to which it belongs.
2. The constructor is a method, unlike other general methods, there is no return data (no return), and there is no leading void.
3. The constructor generates a new object in the class to initialize and run.
4. In the class, if there is no constructor, the system will automatically give a default hidden blank constructor (Default Constructor).

public class Mynumber {
	int n;
	Mynumber(){
		n = 3;
		System.out.println("Here is Constructor and n="+n);
		}
	int getnumber() {
		return n;
}
}

public static void main(String[] args) {

		Mynumber a = new Mynumber();
		System.out.println("Here is main and a.getnumber()="+a.getnumber());//3
	}

4.
There are multiple methods in the Method Overload class with the same name and the same function but different parameters; the data types and number of parameters are different. When the class is called to run, the method with the same parameter type and number is automatically accepted to run. Such an object is called "method overloading".

public class adder {
	int add(int a,int b) {
		return a+b;
	}
	double add(double a, double b) {
		return a+b;
	}
	int add(int x, int y, int z) {
		return x+y+z;
	}
}

public static void main(String[] args) {
		int j;
		double k;
		int m;
		adder ad = new adder();
		j=ad.add(5, 7);
		k=ad.add(1.2, 1.32);
		m=ad.add(12, 12,52);
		System.out.println("j="+j);//12
		System.out.println("k="+k);//2.52
		System.out.println("m="+m);//76
	}
  1. Constructor overloading
public class adder {
	int c;
	double k;
	adder(int a,int b) {
		c=a+b;
		System.out.println("int c="+c);
	}
	adder(double a, double b) {
		k=a+b;
		System.out.println("double k="+k);
	}
}

public static void main(String[] args) {
		adder add_int = new adder(2,3);//5
		adder add_double = new adder(13.2,2.3);//15.5
	}
  1. Public / private modifiers (Modifications)
    declare variables (Variables) or methods (Methods ) in the class can be prefixed with public and private modifiers to limit their use environment and timing.

1. When variables (Methods) and Methods (Methods) are prefixed with public, they are public variables or public methods, and other types of objects can be run and called.
2. When variables and methods are prefixed with private, they are private variables or private methods. Only methods in their own class objects are allowed to be used, and other class objects must not be called.
3. When there is no pre-modifier, it is the default setting (Default), allowing calls to be run in the same package (package), and cannot be called in different packages. In addition, the modifier protected has the same function.

6.1 No Modifications (Default Modifications)

class Number {
	int number;
}

public class Lab {

	public static void main(String[] args) {
	Number i = new Number();
	i.number=10;
	System.out.printf("i.n=%d",i.number);//i.number=10
	}
}

Note: When variables and methods do not have any pre-modifiers, it is the default setting (Default), which is only allowed to be run and called in the same package (package), and cannot be used in different packages.
6.2 Pre-modifier public

public class Number {
		public int N;
}


import text_two.Number;
public class Lab {

	public static void main(String[] args) {
	Number i = new Number();
	i.N=10;
	System.out.printf("i.N=%d",i.N);//i.N=10
	}
}

Packages must be imported in different packages: format: import <package name>. <Class name>; and the class of the imported package must be pre-public. If it is under the same package, only the variable public keyword is required.
6.3 When variables and methods are prefixed with private, only the methods in their own class objects are allowed to be used, and other class objects must not be called.

class Study {
	private int credit = 0;
	public void addcredit(int i) {
		credit +=i;
	}
	public int totalcredit() {
		return credit;
	}
}

public class Lab {

	public static void main(String[] args) {
	Study joe = new Study();
	Study george = new Study();
	joe.addcredit(12);
	george.addcredit(9);
	joe.addcredit(6);
	george.addcredit(3);
	System.out.println("joe studied:"+joe.totalcredit()+"credites");//joe studied:18credites
	System.out.println("george studied:"+george.totalcredit()+"credites");//george studied:12credites
	}
}

  1. this keyword
    When a constructor calls another overloaded constructor with this (), this () must be placed on the first line of the constructor.
    7.1 Class
    In a complex class, in order to simplify the program design and increase the readability of the program, this is used in the method, which means the keyword of the class object.
class Mynumber {
	private int n;
	public void setnumber(int i) {
		this.n=i;
	}

	public int getnumber() {
		return n;
	}
	
}

public class Lab {

	public static void main(String[] args) {
	Mynumber M = new Mynumber();
	M.setnumber(10);
	System.out.println("My.getnumber:"+M.getnumber());
	}
}
Published 19 original articles · Likes2 · Visits 1099

Guess you like

Origin blog.csdn.net/qq_42692319/article/details/102642803