Introduction to Java - Section 8 - Object-Oriented and Procedure-Oriented and Methods
1. Object-oriented and process-oriented
1. Object Oriented
2. Process Oriented
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
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; }
public class New { public void draw(int x) { System.out.println(x); } }
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.println(x+1);
} }
public class Test { public static void main(String [] args) { New ne = new New(); ne.draw(11);//output 12 } }
void sayHello(){ System.out.println("Hello"); }
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; }
public class New { public Object draw(String x) { return x; } }The parent class of String is Object.
method name
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); } }
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 overloading1. 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; } }
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.