1: The difference between a member variable and a local variable (understanding) (1The position in the class is different from the member variable: The outside of the method in the class: The method definition or the method declaration (2) different member variables in memory: Local variables in the heap: in the Stack (3different member variables of the life cycle: As objects are created, local variables disappear as the object disappears: As the method is called, it disappears as the method's call is complete (4initialize values for different member variables: There are default values for local variables: There is no default value, you must define, assign, and then use caution: The local variable name can be the same as the member variable name, using the nearest principle when used in a method. 2: Class as a form parameter problem?(understanding) (1If 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 (2Scenario A: Call the method, just call it once. B: Can be passed as an actual parameter. Sd.method (NewStudent ()); NewStudentdemo (). Method (NewStudent ()); C: Anonymous object call is complete garbage, can be recycled by the garbage collector4: Encapsulation (understanding) (1) hides the implementation details and provides a common way to access (2) Benefits: A: Hide implementation Details, provide public access B: Improve code reusability C: Improve code security (3The design principle hides the implementation details that do not want to be known to the outside world and provides public access to them (4Private is a manifestation of encapsulation. Encapsulation: class, method, private modifier member variable5:p rivate Keywords (master) (1) Private meaning, you can modify member variables and member methods (2) Feature: a member that is modified by private can only be accessed in this class (3Private Application: The time to write a class later: to give all the member variables to private to provide the corresponding getxxx ()/setxxx () method6: this keyword (master) (1) Represents the reference object of the current class remember: Which object invokes the method, and this inside of the method represents that object (2This scenario: A: Solves the problem of the local variable hidden member variable B: Actually this also has other applications, to be explained tomorrow. 7: Construction Method (Master) (1) function: Used to initialize the data of an object (2format: A: The method name and the class name are the same B: There is no return value type, and even void cannot have C: No return value Study questions: Can I have a return statement in the constructor method??you can. It is OK for us to write this way:return; In fact, at the end of any method of void type you can write on:return; (3Considerations for the construction method A: If we do not write the 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 we want to use the parameterless construction method, we must Give A. Recommendation: Always manually give a non-parametric construction method. (4the way to assign a value to a member variable a:setxxx () B: With a parameter construction method (5) Standard CaseclassStudent {PrivateString name; Private intAge ; PublicStudent () {} PublicStudent (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }} Test:classStudentdemo { Public Static voidMain (string[] args) {//Mode 1Student S1 =NewStudent (); S1.setname ("Brigitte"); S1.setage (27); System.out.println (S1.getname ()+"---"+s1.getage ()); //Mode 2Student s2 =NewStudent ("Elina", 30); System.out.println (S2.getname ()+"---"+s2.getage ()); } }
Seventh Day of Java learning