01-java Study Notes 01

Source: Internet
Author: User

1. Object-oriented

Object-oriented: Three features: encapsulation, inheritance, polymorphism

Class and object relationship classes are: a description of things in real life. Object: It is this kind of thing, the real existence of the individual.
1 classCar2 {3String color = "Yellow";4      intnum = 4;5  6      voidRun ()7      {8System.out.println (color+ "..." +num);9      }Ten } One   A  Public classCardemo - { -       Public Static voidMain (string[] args) the      { -Car C =NewCar (); - C.run (); -      } +}

 Heap and Stack 1. Stack. The Java system must know the exact life cycle of all items stored in the stack to move the stack pointer up or down. This constraint limits the flexibility of the program, so while some Java data is stored on the stack-especially object references-Java objects are not stored in them. 2. Heap. A common memory pool, for storing all Java objects。 The benefit of a heap that differs from the stack is that the compiler does not need to know how long the stored data will survive in the heap. So there is great flexibility in allocating storage in the heap. When you need an object, just use the NewWrite a simple line of code that, when executed, automatically stores allocations in the heap. Of course, there is a cost to this flexibility: storage allocation and cleanup with heaps can take more time than storage allocation with stacks (if you can create objects in the stack as in C + + in Java). 2. member variables and local variables 1) The scope of the function is different. The member variable acts on the entire class, and the local variable acts in the function, or in the statement. 2) The positional member variable in memory is in heap memory, because the object is established before it exists. The local variable is on the stack. 3. Application of Anonymous objects
New Car (); // Anonymous Objects  new= 5;   New Car (). num = 5; New Car (). Color = "Blue";

Anonymous object invocation property does not make sense//anonymous object invocation method makes sense//anonymous object Usage one: When the object is called only once, it can be done with an anonymous object, which is easier to write. If you make more than one member call to an object, you must give the object a name. How anonymous objects are used two: Anonymous objects can be passed as actual parameters
Main () {     //Car c = new car ();      // Show (c);     Show (new Car ());   // Strong reference Soft reference weak reference virtual reference? publicstaticvoid  Show (Car c) {     = 5;      = "Yellow";     C.run ();}

 4. Encapsulation (encapsulation) encapsulation: refers to the properties and implementation details of hidden objects, and provides public access only externally.     Benefit: Isolate the change.     Easy to use.     Improve reusability. Improve security.     Encapsulation principle: Hide content that does not need to be provided externally. Hides properties, providing public methods for accessing them. Private: Proprietary, permission modifier: Used to decorate a member (member variable, member function) in a class. After the age is privatized, objects outside the class cannot be accessed directly, even if an object is established. But people should have age, and they need to provide a way to access the ages in the person class. Note: Private is only a representation of the package. The reason why external access is provided is that logic judgments and other statements can be added to the access mode. Manipulate the data that is accessed. Improve the robustness of your code.
 Public classPersondemo { Public Static voidMain (string[] args) {person P=NewPerson (); P.setage (20); P.setage (-20); P.setage (140); }} classperson{Private intAge ;  Public voidSetage (inta) {if(A <0| | A >130) System. Out.println ("Error" ); Else{ Age=A;              Speak (); }                            }                Public intGetage () {returnAge ; }        voidspeak () {System. OUT.PRINTLN ("Age=" +Age ); }} 

5. Constructor features: 1. The function name is the same as the class name 2. Do not define return value type 3. You cannot write a return statement:
1. The feature object of the default constructor is called the function of the constructor constructor that corresponds to it: can be used to initialize an object when a constructor is not defined in a class, the system defaults to the constructor (person () {}) that adds an empty argument to the class when the constructor is defined. The default constructor is gone. Constructors and general functions are not, in the same notation. are also different on the run. Constructors run when an object is established. Initializes the object. The general method is that the object is called to execute, to add objects to the object has the function of an object establishment, the constructor runs only once and the general method can be called by the object more than once when the constructor is defined? When a thing is analyzed, the thing that exists has some characteristics or behavior, then it is defined in the constructor.
classperson2{PrivateString name; Private intAge ; //Person () {}Person2 () {System. OUT.PRINTLN ("A:name" +name + "... age=" +Age ); } Person2 (String N) {name=N; System. Out.println ("A:name" +name + "... age=" +Age );; } Person2 (String N,inta) {name=N; Age=A; System. Out.println ("A:name" +name + "... age=" +Age );; }} classpersondemo2{ Public Static voidMain (string[] args) {Person2 P=NewPerson2 (); Person2 P1=NewPerson2 ("Mike"); Person2 P2=NewPerson2 ("Mike2", 99); }}

6. Building blocks of code: initializing objects to an object is run as soon as it is established and takes precedence over the execution of the constructor function. Differences from constructors: uniform initialization for all objectsThe constructor is the initialization of the corresponding objectThe initialization content of different object commonalities is defined in the Construction code block 
class person{          {          System.out.printf (person run);     }      Private int Age ;}

 7.this keywords distinguish a parameter from a data member  This statement: used to call each other between constructors. You can call only oneand can only be defined at the beginning of the constructorcalling the constructor in the constructor

01-java Study Notes 01

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.