class=”markdown_views prism-atom-one-dark”>
Knowledge points:
Define a method with parameters
return type () {
//The body of the method –parameters– What is the parameter?
}
Method parameter passing
The difference between basic data types and reference data types when passing parameters
Basic data type, the operation passes the value of the variable, and changing the value of one variable will not affect the value of another variable. Reference data types (classes, arrays and interfaces), assignment is to pass the reference of the original object (which can be understood as a memory address) to another reference
Construction method
Access modifier constructor name ( ) {
//Initialization code
}
No return value type, the method name is the same as the class name, parameters can be specified, and the system provides a default parameterless construction method
Constructor overload
Custom constructors have the same method name but different parameter items, regardless of the return value and access modifier
Method overloading —–> same name but different parameters
Note: If there is a construction method in the class, the system will no longer provide a no-argument construction. If there is no construction method in the class, the system will automatically generate a no-argument construction
Usage of this keyword
Call property this.health = 100;
Call method this.print();
Call the constructor:
this();
this(“Xiao Hei”, 100, 100, “Xiong”);
Method overloading
In the same class
the method name is the same
the number or type of parameters is different
has nothing to do with the return value and access modifier
Member variables and local variables —-Member variables outside the method in the class
The location of the variable declaration determines the variable scope
Variable scope determines the area in the program where the variable can be accessed by variable name
The difference between member variables and local variables
The scope is different
The scope of a local variable is limited to the method in which it is defined
The scope of a member variable is visible within the entire class
The initial value is different
Java will give member variables an initial value
Java will not give initial values to local variables
Note: Local variables with the same name are not allowed in the same method. Local variables with the same name are allowed in different methods
Note: In the same class, when a member variable and a local variable have the same name, the local variable has a higher priority
One of the three major characteristics of object-oriented – encapsulation
Hide some information of the class inside the class, and do not allow direct access by external programs, but use the methods provided by the class to realize the operation and access to the hidden information
Two general principles of encapsulation
Hide all attributes, hide as many things as possible, and provide a convenient interface to the outside world
Encapsulation benefits
Why do you need a package
Documents are divided into categories, easy to find and manage
Use directories to solve file name conflicts
Package naming convention
The package name consists of lowercase letters and cannot start or end with a dot
It is best to add a unique prefix before the package name, usually using the network domain name with organization inversion
The subsequent part of the package name varies according to the internal specifications of different organizations
Notes on using packages
A class refers to two classes with the same name from different packages
Must be distinguished by the full class name
Each package is independent, the top-level package will not contain classes from sub-packages
package The order of import and import is fixed
package must be on the first line (ignoring the comment line) the first line of valid code
only one package statement is allowed
followed by import
followed by the declaration of the class
/p>
Access modification of class members
Scope **** modifier | In the same class | In the same package | In subclasses | anywhere |
---|---|---|---|---|
private | Yes | No | No | No |
Default Modifier | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
static modifier
static can be used to modify
member variables
static variablesspan class=”token punctuation”>) {
if(age18||age30) {
System.out.println(“Please enter the correct age”);
return;
}
this.age = age;
}
public int getXid() {
return xid;
}
public void setXid(int xid) {
this.xid = xid;
}
//method
//1 private method
private void ff1() {
}
//The method of default modifier modification
void ff2() {
}
//The method of modification with package modifier
protected void ff3() {
}
//public method
public void ff4() {
System.out.println(name+” says: Study hard and make progress every day!”);
}
}
public class TestStudent {
public static void main(String[ ] args) {
Student stu = new Student();
stu.ff2();
stu.ff3();
stu.ff4();
//The default access modifier modified attribute can be called under the same package
stu.age=18;
stu.name="Zhang San";
stu.xid=00544;
}
}
n class=”token punctuation”>();
stu.ff3();
stu.ff4();
//The default access modifier modified attribute can be called under the same package
stu.age=18;
stu.name=“Zhang San”;
stu.xid=00544;
}
}