1, Class
Common members of class:
Properties and behaviors (methods or functions)
1. Member variables (attributes) and local variables
(1) Member variable
- Definition format: data type variable name = variable value
- Directly defined in a pair of * * {} * * of the class;
- You can specify the permission of a property when declaring it, and use the permission modifier. Common permission modifiers: private, public, default, protected, etc;
- Load memory into heap space (non static);
(2) Local variable
- Definition format: data type variable name = variable value
- Variables declared within methods, method parameters, code blocks, constructor parameters, and constructors;
- Permission modifiers cannot be used;
- There is no default initialization value, so you must explicitly assign a value before calling a local variable
- Load memory into stack space;
2. Constructor
(1) Characteristics
- Have the same name as the class;
- Do not declare the return value type;
- It cannot be modified by static, final, synchronized, abstract or native, and cannot have a return statement return value;
(2) Function
- Create objects;
- Initialize the object;
Person p new person();//create object person pe new person("sd",3);//Initialize object
(3) Creation of constructor
//Test class public class SuccessionTwo { public static void main(String[] args) { //Initializing object properties through constructor SuccessionOne s = new SuccessionOne(19,"sdf"); } //Encapsulation class public class SuccessionOne { //1. Encapsulation properties private int age; private String name; //2. Declaration constructor public SuccessionOne() {//Constructor (usually a constructor without formal parameters is created) age = 18; } public SuccessionOne(int a,String b) {//constructor age = a; name = b; } //3. Method void setAge(int i) { if(i >= 0 && i <= 130) { age = i; return; } System.out.println("Wrong input!"); } }
(4) Explain
- If there is no constructor of the definition class displayed, the system provides an empty parameter constructor (create object) by default;
- Define constructor format: permission modifier class name (formal parameter list) {};
- Multiple constructors defined in a class constitute overloads with each other;
- Once the class constructor is defined, the system will no longer provide the default null parameter constructor;
- There will be at least one constructor in a class;
3. Method
(1) Method declaration
Permission modifier return value type method name(parameter list ){ Method body; }
(1) Permission modifier
(2) Return value type
Return value type: the return value type must be specified when the method is declared. At the same time, in the method, you need to use the return keyword to return variables or constants of the specified type.
Unused return value type: when a method is declared, it is represented by void. Generally, return is not used in methods that do not have a return value. However, if it is used, it can only be "return"; "Means to end this method.
(3) Formal parameter list
Method can declare 0, 1, or more formal parameters.
Format: data type 1, parameter 1, data type 2, parameter 2
(4)return keyword
Scope of use: used in the method body;
Functions: 1. End method 2. For methods with return value type, use the "return data" method to return the required data;
Note: you cannot declare an execution statement after return;
(2) Method overload
In the same class, more than one method with the same name is allowed, as long as their parameter number or parameter type are different; (independent of permission modifier and return value type)
public class OverLoad { public static void main(String[] args) { OverLoad test = new OverLoad(); int sum = test.mol(8); int mult = test.mol(1, 2); String chars = "sfljd"; test.mol(chars); System.out.println(sum + mult); } //Method overload public int mol(int i) { return i*i; } public int mol(int a,int b) { return a*b; } public void mol(String c) { System.out.println(c); } }
(3) Variable number of formal parameters
javaSE 5.0 support;
Allows you to directly define formal parameters that can match multiple arguments.
Specific use:
- Variable number parameter format: data type... Variable name
- When calling a method with variable number of formal parameters, the number of formal parameters passed in can be: 0, 1, 2
- Methods with variable number formal parameters have the same name as those in this class, and methods with different formal parameters constitute overloads
- Arrays with the same method name and parameter type as those in this class do not constitute overloads
- The variable number parameter is in the parameter of the method and must be declared at the end (there can only be one variable number parameter)
public void show(int a,int c,String ... strs){ }
(4) Value Passing Mechanism of method parameters
Basic data type of parameter: pass the "data value" of the basic data type variable of the argument to the formal parameter
public class ValueTransfer { public static void main(String[] args) { int m = 20; int n = 3; ValueTransfer v = new ValueTransfer(); v.swap(m, n); System.out.println(m + "," + n); } public void swap(int m,int n) { int temp = m; m = n; n = temp; } } //Results: 20, 3
Parameter is a reference data type: pass the "address value" of the parameter reference data type variable to the parameter
public class ValueTransferr { public static void main(String[] args) { Date date = new Date(); ValueTransferr v = new ValueTransferr(); v.swap(date); System.out.println(date.m + "," + date.n); } public void swap(Date date) { int temp = date.m; //date is the reference data type, date.m = date.n; date.n = temp; } } class Date{ int m = 30; int n = 3; }
2, Object
1. Create object
Class name object name = new Class name();
2. Anonymous objects
Understanding: when creating an object, there are only statements to create the object, but there is no explicit assignment to a variable;
features:
- Calling anonymous objects several times creates several objects, resulting in a waste of space;
- Anonymous objects can be used as parameters and return values of methods;
new Get().fangFa(); new Get().zhi = 209;