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) in-memory locations are different
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 variable: has default value, Int,byte,short is 0,char to "\u0000", application type is NULL, Boolean type is False
Local variables: No default values, must be defined, assigned, and then used
The local variable name can be the same as the member variable name, when used in the method, using the nearest principle.
2: Class as a form parameter problem? Understand
(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.
3: Anonymous Object (understanding)
(1) An object without a name
(2) Application Scenarios
A: Call the method, just call it once.
B: Can be passed as an actual parameter.
(3) What are the benefits of anonymous invocation ? Yes, the anonymous object call is complete garbage, can be reclaimed by the garbage collector.
/*
Anonymous object: Is an object without a name
*/
Class Student {
public void Show () {
System.out.println ("I love Learning");
}
}
Class Studentdemo {
public void Method (Student s) {
S.show ();
}
}
class Nonamedemo {
Public static void Main (string[] args) {
//Call with a name
Student s = new Student ();
s.show ();
s.show ();
System.out.println ("--------------");
//Anonymous object
//new Student ();
//Anonymous object invocation method
new Student (). Show ();
New Student (). Show ();//This is actually re-creating a new object .
System.out.println ("--------------");
//Anonymous object passed as actual parameter
Studentdemo sd = new Studentdemo ();
//student ss = new Student ();
//sd.method (ss);//Here S is an actual parameter
//Anonymous object
Sd.method (New Student ());
//In a
new Studentdemo (). Method (New Student ());
}
}
4: Encapsulation (understanding)
(1) Hide implementation Details and provide public access
(2) Benefits:
A: Hide implementation details and provide public access
B: Improve the reusability of code
C: Improve the security of your code
(3) Design principles
Hide the implementation details that you don't want to know about, and provide a common way to access them.
(4) Private is a manifestation of encapsulation.
Encapsulation: class, method, private modifier member variable
/*
define a student class:
member variable: name,age
member Method: Show () method
In the course of using this case, we found a problem:
by assigning a value to a member variable through an object, you can assign some illegal data.
this is unreasonable.
It should look like this: Before assigning a value, judge the data first.
What is the right place to judge?
The Studentdemo class is a test class, and the test class typically creates only objects and invokes methods.
Therefore, this judgment should be defined in the student class.
Can we make data judgments in the position of the member variables?
is not possible, because to do the data verification, must rely on some logical statements.
logical statements should be defined in the method, so we finally decided to provide a method in the student class
to validate the data.
according to our previous analysis, we have given a method to verify.
However, it does not call the method to assign the value, or the direct assignment,
so our method doesn't work.
I should have asked you to use my method instead of directly calling member variables to assign values.
How do I force a request not to use member variables directly?
in this case, Java provides a keyword for the private
Private: Privately-owned. member variables and member methods can be decorated.
Note: Members that are modified by private are only accessible in this class.
In fact, what I'm talking about now is a package of ideas.
encapsulation: Refers to the properties and implementation details of hidden objects, providing public access only to the outside.
*/
class Student {
//Name
String name;
//Age
private int age;
//Write a method to verify the data
/*
return value type: void
parameter list: int a
*/
Public void Setage (int a) {
if (A < 0 | | Age >) {
System.out.println ("The Age you give is a problem");
}else {
Age = A;
}
}
//show () method that displays all member variable values
Public Void Show () {
System.out.println ("Name:" +name);
System.out.println ("Age:" +age);
}
}
class Studentdemo {
Public static void Main (string[] args) {
//Create student Objects
Student s = new Student ();
s.show ();
System.out.println ("--------------");
//Assign a value to a member variable
s.name = "Brigitte";
//s.age =;
S.setage (+);
s.show ();
System.out.println ("--------------");
//Assign an age value
//s.age = 27;//This data is unreasonable
//Give value by method
s.setage ( -27);
s.show ();
System.out.println ("--------------");
}
}
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?
OK. 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 (27);
System.out.println (S1.getname () + "---" +s1.getage ());
Mode 2
Student s2 = new Student ("Elina", 30);
System.out.println (S2.getname () + "---" +s2.getage ());
}
}
8: Code: Student s = new Student (); What did it do? Understand
(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, 27
(6) Initialize the member variables by constructing the method. Elina, 30
(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, and 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 10 20 30
JAVA_DAY07 (Java Object-oriented explanation)