I. Anonymous objects
(1) An object without a name
(2) Application Scenarios
A: Call the method, just call it once.
classcar{//describes the property. Color, number of tires. String color; intNumber ; //describes the behavior. voidrun () {System.out.println (color+":"+Number ); }}classcardemo{ Public Static voidMain (string[] args) {//calling methods with anonymous objects NewCar (). run (); NewCar (). run (); }}
B: Can be passed as an actual parameter.
classStudent { Public voidShow () {System.out.println ("I love learning."); }}classStudentdemo { Public voidmethod (Student s) {s.show (); }}classNonamedemo { Public Static voidMain (string[] args) {//call with a nameStudent s =NewStudent (); S.show (); S.show (); System.out.println ("--------------"); //Anonymous Objects//new Student (); //Anonymous Object Invocation method NewStudent (). Show (); NewStudent (). Show ();//This is actually recreating a new object.System.out.println ("--------------"); //Anonymous objects are passed as actual argumentsStudentdemo SD =NewStudentdemo (); //Student ss = new Student (); //Sd.method (ss);//here s is an actual parameter//Anonymous ObjectsSd.method (NewStudent ()); //in a NewStudentdemo (). Method (NewStudent ()); }}
The difference between a basic type and a reference type as a parameter pass
1. The basic type is passed as a parameter (the value in the main method does not change)
|
Class Demo
{
public static void Main (string[] args)
{
int x = 4;
Show ( x);
System.out.println ( "x=" +x);//4
}
public static void show (int x)
{
x = 5;
}
}
|
|
Description
When a primitive type is passed as a parameter, it is actually copying the value in the basic type variable x space to the calling method Show (), when x is copied in the Show () method, and in the Show () method x variables, this will only affect the x in Show . When the Show method executes, the program returns to the main method after the stack, and the x value in the Main method is the original value.
2. The reference parameter is passed as a value
Description
When a reference variable is passed as a parameter, it is actually a copy of the memory address (reference) in the reference variable space to the d reference variable passed to the Show method . At this point there will be two references pointing to the same object in the heap. When you execute d.x=6 in the Show Method , the objects in the heap are found based on the reference held by D, and the value of its X property is changed to the 6,show method stack. Since the two references point to the same object, no matter which reference changes the value in the referenced object, the other reference is used again as the changed value.
Three, Package
(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
(5) using setxxx (), GetXXX () to provide external
Iv. Construction Methods
(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
Modifier constructor Method Name (parameter list) {
}
/*constructor: Initialize the data of the object: A: The method name is the same as the class name B: There is no return value type, and even void has no C: no specific return value*/classStudent {PrivateString name;//NULL Private intAge//0 PublicStudent () {System.out.println ("This is the construction method"); }}classConstructdemo { Public Static voidMain (string[] args) {//Creating ObjectsStudent s =NewStudent (); System.out.println (s); //[email protected] }}
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.
C: There can be more than one constructor in a class, and multiple constructors exist in the form of overloads
D: The constructor also has a return statement, which is used to end the initialization action.
E: constructor can be modified by private, function: Other program cannot create object of this class
(4) How to assign a value to a member variable
A:setxxx ()
B: With parameter construction method
(5) The difference between the general method and the construction method
The constructor executes when the object is created and executes only once.
The general function is called by the object after it is created, and can be called multiple times.
Thinking:
After you have a constructor to initialize the properties of an object, do you need the corresponding set and get methods?
The corresponding set and get methods are required because the object needs to be modified and accessed when the corresponding property value is created, which can only be manipulated by a set or get method.
Five, this keyword
(1) A Reference object representing the current class
Remember: Which object calls the method, and the inside of the method represents that object
(2) statements that call other constructors must be defined in the first row of the constructor, because the initialization action is performed first.
Java Learning Notes---object-oriented 2