Vivid understanding of object-oriented Java

Object-Oriented

  1. Object-oriented: the characteristics (attributes) and behavior (methods) point of view to analyze the way the real world affairs
  2. Object-oriented programming: the use of object-oriented approach to the analysis, and then use any one object-oriented programming language translation process

Class class

  1. What is: human (abstract description of multiple objects with the same common characteristics and behavior of members of the member variables and methods described behaviors include descriptions of features in the Java language, is a template to create the object)
  2. Why: The object too much, too much amount of code duplication, but also to describe, so the extract to form a class of their things in common!

Objects new

  1. What it is: I am the person (an objective entity, embodied in the Java language is an area of ​​memory space)
  2. Why: The class is an abstract concept, can not work, you have to work in order to rely on this kind of entities (objects)!

Quote

  1. What is: Cao Yang (variables reference data type declaration in the Java language is called a reference type variable, referred to as the "reference")
  2. Why: The essence is to create objects in the heap to create a storage space, if this space is not recorded, the next time use do not know how to find, in order to record this piece of storage space, we have to declare a reference!

Member variables and member methods

  1. Member variables: height, weight (feature)
  2. Member method: eat, drink (behavior)

JVM memory structure

  1. Class: method area
  2. Object: heap
  3. Quote: stack area
  4. Member variable: static (area method), non-static (heap)
  5. Member method: Warehouse District
  6. Local variables: stack area, as defined in the function, the function to end the memory release
  7. String: constant pool

Construction method

  1. What is: the same method name with the class name, no return value, void nor write
  2. Why: I hope that at the same time to create a Person object when the name and age you need to be able to specify the constructor!

Method overloading

  1. What is: there are parameters like construction and constructor with no arguments this way, the same method name, list the number of parameters, different order types can be distinguished when calling the method, this relationship is called overloading
  2. Why: One way, need to pass a different argument list, which requires method overloading, easy call, remember to save the method name too!

this keyword

  1. What: represents the object currently being constructed in the constructor, the object currently being invoked on behalf of the members of the method method
  2. Why: parameter variable names and variable names members of the same parameter for us to know this variable is assigned to the member variables, but they have different scope, the principle of proximity in vivo method member variable could get in, then you will need to the this keyword

Parameter passing

  1. Basic data types
    • Changing the variable parameter will not affect the argument
    • I was born to laugh, with age I can not laugh?
  2. Reference data types
    • Changing the parameter variable points will affect the contents of the content pointed to by the argument variables
    • I did not ride a bike, but I learned eh
  3. Reference data types
    • It does not affect the content of the argument variables point to point when the parameter variable is changed before changing the target content to
    • I did not ride a bike nor the programming, but I learned to ride a bike did not learn programming, I do not programming or not Well

Recursion

  1. What it is: method calls itself, factorial, Tower of Hanoi
  2. How to use: to find the exit condition (n = 1) + rule (n = n) = "if judgment calls itself +

Package

  1. What is: A reasonable mechanism to ensure that the member variable values ​​and privacy
  2. Why: main method is not unique to a particular class should not be placed in class, into a single test class. In the test class member variables can easily change information, legal but unreasonable, it will not give up the package!
  3. how to use:
    • My car is my own (privatization member variables) only I can open;
    • I take you to open (public ownership get and set methods) you can open, I borrow your car you drive bad nor Austria (set method Riga judgment);
    • I can listen to and drove off (in the tone set constructor method)

The static keyword

  1. What is: Just like in a class definition to members of a property or method of a member default value
  2. Why: In Chinese nationality are the same instance of the Person class, assign different memory space, resulting in waste. If you want to nationality are placed in a memory space you have to use the static keyword
  3. How to use: drinking fountains (shared by all objects) can be added, cups can not add

Singleton

  1. What is: can be obtained in the main process and can only get an object class Singleton, Singleton class is a singleton, a task manager
  2. Starving type:
    • ID number has one and only one of their own (privatization constructor);
    • With the ID number can prove this man (pointing to the referenced object of this type of this type of class), how to let others know this person do you have to add on a static account, but not anyone can use your ID number ah you have to add a private;
    • How to find this person does have to tell him you can find by ID number (provided the public get method responsible for the above object is returned out), that people do not know you ID number is Shaa you have to add a static told him
  3. Lazy type:
    • Black households not on the account, was arrested only on the account
    • This is the case, but unreasonable Oh, is not recommended!

Inheritance extends

  1. What is: teachers are the students who had to eat and sleep (Communist Party of the class content of the package is extracted to form a common category, other classes this mechanism is called inheritance public class absorption)
  2. Why: In addition to eating and sleeping, teachers, students had to drink water, it can not be added one by one, ah, directly in Riga a human on the line (increase code reusability, scalability and maintainability)
  3. how to use:
    • Son with the father can not same generation also can not control mother called his wife ah (subclass can not inherit the parent class constructor and private methods), the son of a famous father was also famous ah, you can not always be called so and so who is the son of it, but you do not talk to your dad with a name right (private characteristics can be inherited but can not access)
    • Son born father of genetic gene (when the class object constructor is automatically configured without the parent class constructor initializes parameters inherited from the parent class member variables down, equivalent to an increase in the first line constructor subclass Code super();effect)
    • A father can have more sons, but only one son of a father (Java language supports only single inheritance)
    • Son must be the biological father to inherit his genes (using inheritance must satisfy the logic: sub-class is a parent class)

The method of rewriting @Override + super

  1. What is: Dad is not enough money to spend, you have earned yourself
  2. Why: Worker class inherits Person class calls the show () method is the Person class, can only print the Person class in some characteristics, can not print the Worker class in the newly added feature, this time we must use the method overrides

Access control

  1. His stuff can only own use (private modified content can only be used in this class)
  2. Methods to teach you that you love ye ye use with (public modified content can be used in any position)

Wrapped package

  1. What is: is the folder
  2. Why: Let everyone write a class, all the same last name, sent me to the conflict, in a different directory just fine (easy to manage, to avoid naming collision)
  3. How to use: the company or organization's domain name anti-write the project name module name class name

final keyword

  1. What is: not to have children, BU, who have their own ideas can not change (intended as "final and irrevocable", the keyword class can be modified, member methods, member variables, etc.)
  2. Why: final keyword to prevent abuse of inheritance, inadvertently changing the method of rewriting or value
  3. Contrast this:
    • this: This class (case member variables of the same name and formal parameters)
    • super: parent (used super(实参)embodiment may be configured to call the parent class, use super.of the method in the parent class may be rewritten call)

Objects created execution flow

  1. Single object: main method in default = "display initialize =" building block = "= body construction method" is created
  2. Subclass object: a static code block parent class = "subclass static block of code =" building block parent = "parent class constructor body =" building block subclass = "subclass constructor body

Polymorphism

  1. What it is: drink: Coke Sprite Red Bull (the same thing manifested in various forms)
  2. why:
    • Printing can print both rounded rectangle (the Shape parent class) (parameter passing method for forming a multi-state programming shielding difference different subclass by general bring different results)
    • Directly in the method body, abstract classes / interfaces is not new objects, only to sub-class objects, needs to be replaced when the latter only needs to subclass new type code unchanged elsewhere modifications will take effect immediately, improved code maintainability
    • Calender class is abstract object is not new, the return type is Calender type, but the actual return an object class subclass
  3. how to use:
    • Can be found by his father, "his father Lincoln," but his father is his father, will still be shoe repair shoe repair, that he can be president but he is not the father of President (when the reference point to the parent class type of subclass object, the parent class reference types can directly call the parent class's unique methods, subclass unique method can not be called directly)
    • Wolf dressed in sheep's clothing, compiled parent class, subclass run, in fact, memory neutron class is a subclass of objects, static is to commission a class hierarchy (if the child class overrides the methods, static tone of the parent class, non-static tone class)

Casts instanceof

  1. What it is: automatic conversion into (child to parent), and casts (parent to child)
  2. Why: polymorphic properties and methods can not be used directly extended subclass, but through the cast solution

Abstract class abstract

  1. What: Template (mainly abstract class that can not be specific examples of using the abstract keyword modified class and has an abstract method of class)
  2. Why :( abstract method has no method body, so calling abstract method does not make sense, in order to prevent the programmer does not call abstract methods carefully, Java abstract class can not officially on the provisions of new objects)
  3. how to use:
    • Like to know what to learn, but what it will not do anything, then I want you doing this subject, but I can ask what you have learned, and then himself to learn the real meaning (abstract class is not itself create objects that are inheritance, when a class inherits the abstract class must override abstract methods, otherwise the class has become an abstract class)
    • Not common and final modification methods (rewritable and can not be overridden)
    • Static and can not be modified together (can not call and be called)
    • Not common and private modification (not inherited and rewrite)

Interface interface + implements

  1. What it is: not only the member variables constant, only abstract methods (special abstract class)
  2. Why: You have only a father, but you also want to inherit your mother's genes (gold out both metals, and count money, but in Java only supports single inheritance, so that the gold class while achieving metal interfaces and currency interfaces, make up no more inheritance defect)
  3. how to use:
    • Classes and interfaces: something different implements, the same thing extends, there is more of an interface
    • Abstract classes and interfaces: abstract class can have a constructor, member methods, member variables, methods can be increased without affecting the subclasses (because it can increase what methods are OK, but the interface can only add an abstract method implementation class must override), jdk1 after .8 interface can have non-abstract methods, however, must default
    • As a method of referencing parameter interface type, argument can be an object class that implements the interface, the use of anonymous inner classes, and the type of the interface reference obtained

Anonymous inner classes

  1. What is: If you need to create an object of a class period of the program (usually this class needs to implement an interface or extend a class), and the object is created the value of this class would not exist, this class does not have to be named, said the anonymous inner classes
  2. Why: If this method is called only once, in order to write it in a class occupied a block of memory space in the method area, bit of a waste, so we create an anonymous inner class
Published 13 original articles · won praise 3 · Views 310

Guess you like

Origin blog.csdn.net/weixin_45158297/article/details/103132660