1: Differences between member variables and local variables (understanding)
(1) different positions in the class
member variables: Outside methods in class
Local Variables: In a method definition or on a method declaration
(2) different locations in memory
member variables: in the heap
Local variables: in the stack
(3) different life cycle
member variables: As objects are created, they disappear as the object disappears
Local Variables: As the method is called, it disappears as the method's call is complete
(4) different initialization values
member variables: with default values
Local Variables: No default values, must be defined, assigned, and then used
2: Class as a form parameter problem? (understanding)
(1) If you see a method that requires a parameter that is a class name, you should know that what is actually needed here is a specific object.
(1) object with no name
Span style= "FONT-SIZE:16PX;" > (2) application scenario
a: Call the method, just call it once.
b: Can be passed as an actual parameter.
4: Encapsulation (understanding)
(1) Hide implementation details, Provides public access methods
(2) Benefits:
a: Hide Implementation Details, Provides a common way to access
b: Improve code reusability
c: Improve code security
(3) design principles
encapsulation: class, method, private modifier member variable
5:private Keywords (master)
(1) Private meaning, can modify member variables and member methods
(2) Features:
Members that are modified by private can only be accessed in this class
(3) Application of private:
To write a class later:
Give all the member variables to private.
provides the corresponding getxxx ()/setxxx () method
6:this Keywords (master)
(1) A Reference object representing the current class
Remember: Which object calls the method, and the inside of the method represents that object
(2) This application scenario:
A: Fixed the problem of local variable hidden member variable
B: In fact this has other applications, to be explained tomorrow.
7: Construction Method (Master)
(1) function: Used to initialize the data of an object
(2) format:
A: The method name is the same as the class name
B: There is no return value type, and even void cannot have
C: no return value
Study Questions: Can there be a return statement in the construction method?
you can. It's OK if we write this way: return;
in fact, at the end of any method of void type you can write: return;
(3) Considerations for construction methods
A: If we do not write a construction method, the system will provide a default parameterless construction method
B: If we give a construction method, the system will no longer provide a default construction method
If this is the case, we have to use the parameterless construction method and we have to give it ourselves.
Recommendation: Always manually give a non-parametric construction method.
(4) How to assign a value to a member variable
a:setxxx ()
B: With parameter construction method
(5) standard case
class Student {
private String name;
private int age;
Public Student () {}
Public Student (String name,int age) {
this.name = name;
this.age = age;
}
Public String GetName () {
return name;
}
Public void SetName (String name) {
this.name = name;
}
public int Getage () {
return age;
}
Public void Setage (int.) {
this.age = age;
}
}
Test:
class Studentdemo {
Public static void Main (string[] args) {
//Mode 1
Student S1 = new Student ();
s1.setname ("Brigitte");
S1.setage (+);
System.out.println (S1.getname () + "---" +s1.getage ());
//Mode 2
Student s2 = new Student ("Elina", +);
System.out.println (S2.getname () + "---" +s2.getage ());
}
}
8: Code: Student s = new Student (); What did it do? (understanding)
(1) load the Student.class file into memory
(2) Open space in stack memory for S
(3) Application space for student objects in heap memory
(4) default initialization of the student's member variables. null,0
(5) display initialization of the student's member variables. Brigitte,
(6) Initialize the member variables by constructing the method. Elina,
(7) The object is constructed, assigning the address to the S variable
9: Object-oriented exercises (mastering)
(1) definition and testing of standard mobile phone class
(2) The demo class has a summation method, the test class is tested.
When do I define a member variable?
when the variable is used to describe a class.
(3) Rectangular case
(4) Employee Case
(5) MyMath case (self-provided subtraction and test)
10:static Keywords (understanding)
(1) The meaning of the static. member variables and member methods can be decorated.
(2) Static characteristics:
A: Loaded with the load of the class
B: Priority and object Presence
C: Shared by all objects of the class
this is actually the basis for us to judge whether we should use static.
examples: Problems of drinking fountains and water cups
D: Can be called by the class name
Called by the name of the object, or by the class name, is recommended by the class name.
(3) static memory diagram
Static content in the method area in the static area
(4) static precautions;
A: There is no this object in the static method
B: Static can only access static (code tested too)
(5) The difference between a static variable and a member variable
A: belong to different
static variable: Belongs to class, class variable
member variables: Belong to object, object variable, instance variable
B: Different memory locations
Static variables: Static area of the method area
member Variable: heap memory
C: Different life cycle
static variables: Static variables are loaded as the class is loaded and disappear as the class disappears
member variables: member variables are present as the object is created and disappear as the object disappears
D: Call different
static variables: can be called by object name, or by class name
member Variable: can only be called by object name
(6) The main method is static
Public : Maximum Permissions
static: Do not create object calls
void: Return value does not make sense to the JVM
main: is a common name.
string[] args: Can receive data, flexibility of the provider
format: Java maindemo Hello World Java
Java Maindemo
An object-oriented explanation in Java