Seventh Day of Java learning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.