Java Basic Course Learning notes (7)

Source: Internet
Author: User

1: Differences between member variables and local variables (understanding)(1) Different member variables in a class: In a class in an out-of-method local variable: In a method definition or on a method declaration (2) in memory, a different member variable: a local variable in a heap: A different member variable in the stack (3) life cycle: As the object is created, local variables disappear as the object disappears: method is called and disappears as the method's call is complete (4) Initialize values for different member variables: There are default values for local variables: There is no default value, you must define, assign, and then use Precautions :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? (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. 3: Anonymous Object (understanding)(1) object without name (2) Application Scenario A: Call method, only once.   Note: It is not appropriate to call multiple times.    So what is the benefit of this anonymous invocation? Yes, the anonymous object call is complete garbage.  Can be reclaimed by the garbage collector. B: Anonymous objects can be passed as actual parameters 4: Encapsulation (understanding)(1) Encapsulation Overview: Refers to the hidden object properties and implementation details, only provide public access to the external. (2) Benefits: A: Hide implementation Details, provide public access B: Improve code reusability C: Improve code security (3) Design principles hide the implementation details that you don't want to know about, provide public access to the properties, and provide a public way to access them.  (4) Private is a manifestation of encapsulation. Encapsulation: class, method, private modifier member variable 5:private Keywords (master)(1) The meaning of the private, can be modified member variables and member methods (2) features: the member that is modified by private can only be accessed in this class (3) The application of private: The time to write a class later: All the member variables to private to provide the corresponding getxxx ()/se TXxx () method 6:this Keywords (master)(1) A Reference object that represents the current class remember: Which object invokes the method, and this inside of the method represents the application scenario for that object (2) This: A: resolves the problem of the local variable hidden member variable B: Actually this has other applications to explain tomorrow. 7: Construction Method (Master)(1) Function: Used to initialize the data of an object (2) format: A: The method name and the class name are the same B: no return value type, no even void can 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 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 this time, 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: Structural method with Parameters   (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;&N Bsp  }      public void setage (int age) {    this.age = age;   } } &nb Sp Test:  class Studentdemo {   public static void Main (string[] args) {   //mode 1  &NBSp Student S1 = new Student ();    S1.setname ("Brigitte");    S1.setage (+);    SYSTEM.OUT.PRINTLN ( S1.getname () + "---" +s1.getage ());      //mode 2    Student s2 = new Student ("Elina", "Max");  & nbsp 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) in the stack memory for the S Space (3) in the heap memory for the Student object application space (4) to the student's member variable to initialize the default. null,0 (5) displays initialization of the student's member variables. Brigitte, 27 (6) initializes the member variable by constructing the method. Elina, 30 (7) The object is constructed, assign 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: Load with the class B: Precedence and Object existence C: shared by all objects of the class this is actually the basis for us to judge whether to use static. Examples: Problems of drinking fountains and water cups D: can be called through the class name, either through the object name, or through the class name, recommended by the class name. (3) static memory diagram s static content in the method area(4) static precautions;A: There is no this object in the static methodB: Static can only access static (code tested too)A: How do you understand the This keyword in a static method? static is loaded as the class loads, and this is present as the object is created. static than object exists first. B: Static methods can only access static member variables and static method static methods: member variables: only static variable member methods: only access static member methods non-static methods: member variables: can be static or non-static member methods: but static   Member method, or a non-static member method. Simple to remember: static can only access static. (5) The difference between a static variable and a member variableA: Different static variables: Belong to class, class variable member variable: belongs to object, object variable, instance variable B: memory location Different static variable: method zone static Zone member variable: heap memory C: Life cycle different static variable: Static variable is loaded with class and disappears with class Member variables: member variables exist as objects are created and disappear as the object disappears D: Call different static variables: You can call by object name, or you can call member variables through the class name: Only the object name is called (6) The main method is static public: Permissions Max static: No  Call void with Create object: return value to the JVM is meaningless. Main: is a common name. String[] args: Can receive data, the flexibility format of the provider: Java maindemo Hello world Java Java maindemo 10 20 30

Java Basic Course Learning notes (7)

Related Article

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.