The most in-depth understanding of the web —- Java object-oriented

Class and object understanding:

Objective things are objects, so we often say that everything is objects.

Class and object relationships:

  • Class: A class is an abstraction of a class of things in real life that share common properties and behaviorCopy the code
  • Object: a real entity that can be seen and touchedCopy the code
  • Simple understanding: a class is a description of a thing, and an object is a concrete thingCopy the code

Define the class:

Steps for class definition:

1. Public class name {// member variable 2. 3. // Datatype variable 2 of variable 2; 4.... 5. // Member methods 6. // Method 1; 7. // Method 2; }Copy the code

Use of objects

Create object format:

  1. Class name Object name = new class name ();

The format of the call member:

  1. Object name. Member variable; Object name. Member method ();

Sample code:

1. Public class PhoneDemo {2. Public static void main(String[] args) {3. 6. System.out.println(p.rand); 7. System.out.println(p.price); 8. 9. 10. p.price=2999; 11. 12. System.out.println(p.brand); 13. System.out.println(p.price); // Use the member method 16.p.call (); 17. p.sendMessage(); 18. 19.}}Copy the code

Member variables and local variables

  • Different locations within a class: member variables (outside of a method in a class), local variables (inside a method or on a method declaration)

  • Different locations in memory: member variables (heap memory), local variables (stack memory)

  • Different life cycles: member variables (exist with the object and disappear with the object), local variables (exist with the method call and disappear with the method call)

  • Different initialization values: member variables (with default initialization values), local variables (without default initialization values, must be defined, assigned to use)

Encapsulate ideas (understanding)

  • Encapsulation overview: is one of the three characteristics of object-oriented (encapsulation, inheritance, polymorphism) is object-oriented programming language simulation of the objective world, the objective world member variables are hidden inside the object, the outside can not be directly operated

  • Encapsulation principle: Some information of a class is hidden inside the class, and external programs are not allowed to access it directly. Instead, methods provided by the class are used to operate and access the hidden information

  • Encapsulation benefits: through the method to inform the operation of the member variable, improve the security of the code, the code is encapsulated by the method, improve the reuse of the code

Explore and learn more about Java expertise: Good Knowledge Education

Understand employment, career planning, interview, training institutions related to the dry goods search: IT brother

— — — — — — — —

Copyright notice: This article is originally published BY CSDN blogger “I’m waiting for you”. It follows CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement. Original link: blog.csdn.net/qq_44912827…