Introduction to Java - Section 8 - Object-Oriented and Procedure-Oriented and Methods

Introduction to Java - Section 8 - Object-Oriented and Procedure-Oriented and Methods

1. Object-oriented and process-oriented

1. Object Oriented

    • Object-oriented is a programming idea that highlights the important role of objects in the programming process. What is object-oriented? Simply put, it is to let the object become the bridge of "communication" between classes, and to make the class form an organic whole through the object.
    • Object-oriented programming language is object -centric and message-driven, that is, program = object + message.
2. Process Oriented   
    • Procedural programming languages ​​are process -centric and algorithm-driven, that is, program = algorithm +
3. Difference

    1. Object-oriented is to think about problems from a macro perspective, while process-oriented can be said to think about problems from the details

     2. In object-oriented, there is also process-oriented.

2. Method

1. Construct a method

    • A method is used to define a certain behavior (or function) of a class, and its grammatical structure is as follows:

access control [ modifier ] return value type method name ([ parameter ])  {

          // method body

}

E.g

public void sayHello(){
	System.out.println("Hello");
}

protected final void show(int x){
		System.out.println(x);
}
	
private static int add(int x, int y){
	return x+y;
}

The access control character in the method is used to limit the scope of use of the method in other classes .

public class New {
	
	public void draw(int x) {
		System.out.println(x);
	}

}
There are four types of access control characters: public , protected , friendly and private
• The static modifier is used to restrict how a method can be called:
public class New {
	
	public static void draw(int x) {
		System.out.println(x+1);
	}

}

 
    
public class Test {
	
	public static void main(String [] args) {
		New.draw(11);
		New ne = new New();
		
	}

}

Non- static modified methods can only be called using objects created by the class
public class New {
	
	public void draw(int x) {
		System.out.printlnx+1);
	}

}
public class Test {
	
	public static void main(String [] args) {
		New ne = new New();
		ne.draw(11);//output 12
	}

}
If the method has no return value, it needs to be represented by void .

void sayHello(){
	System.out.println("Hello");
}

• The method returns the data of the basic data type, the return value type must be the data type to which the returned data belongs or the data type with higher precision (for the data of the numerical type).
boolean compare(int x, int y){
		return x>y;
}
	
int add (int x, int y) {
	return x+y;
}

double subtraction(int x, int y){
	return x-y;
}
• The method returns the data of the reference data type, the return value type must be the data type to which the returned data belongs or its parent class
If the method has a return value, the data must be returned with the help of the return keyword;
public class New {
	
	public Object draw(String x) {
		return x;	
	}
}
The parent class of String is Object.
method name
Follow the rules for naming identifiers; the first letter must be lowercase, and if it consists of multiple words, the first letter must be capitalized from the second word; method names generally consist of a verb or a gerund.
2. Parameters   
    • Methods can have multiple parameters, separated by commas (,) .
    • The parameters of the method are valid within the entire method.
    The data type in front of the method parameter is used to limit the data type of the specific data passed when calling the method
int multiplication(int x, int y){
		int result = x*y;
	return result;
}
Dynamic parameters:
public class Test {

	public static void print(int... numbers) {
		for (int number : numbers) {
			System.out.println(number);
		}
	}

	public static void main(String[] args) {
		print(1, 2, 3, 4, 5);
	}
}

Note :

      1. Dynamic parameters are essentially arrays;

      2. Dynamic parameters must be at the end of the parameter list;

      3. A method can only have one dynamic parameter;

3. Method overloading    
    • There are multiple methods in the same class with the same method name but different parameter lists. This phenomenon is called method overloading . The different parameter lists include the following situations:

               1. The number of parameters is different

               2. The corresponding types of parameters are different

public class New {
	
	int add(int a) {
		return a;	
	}
	int add(int a,int b) {
		return a+b;	
	}
	int add(int a,int b,int c) {
		return a+b+c;	
	}
}
Note :

               1. Different parameter lists do not include different parameter names, that is to say, if the method names are the same, the number and type of parameters in the method are the same, but the parameter names are different, so it cannot be called method overloading.

               2. Other components in the method do not participate in the comparison: access control specifiers, modifiers, and return value types.
















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325345157&siteId=291194637