[JavaSE] Standard classes, classes and objects

Object-oriented is a kind of code programming thinking, which is used every day. Must be flexible

Standard classes, classes and objects

The difference between member variables and local variables:

  • who am I? (I have default values) Where am I? (Heap) Where am I going? (Go with the subject)
  • Member variables in the heap memory (new in the heap), life cycle: disappear with the object, with a default initial value
  • Local variables are in the method area (in the Stack stack), and disappear when the method is called. No initial value must be assigned before it can be used

Class, object-oriented OOP

What is a class?

Classes are groups (templates), which have the upward abstract meaning of instance objects.

  • For example, human beings are classes, and everyone belongs to a common class—human beings; instances are specific objects, and people are instantiations/objects of humans.

Three characteristics: encapsulation, inheritance, polymorphism

Encapsulation

  • Method is a kind of encapsulation, class is also a kind of encapsulation
    • Member variables are hidden inside and cannot be directly operated by the outside world (Private)
    • External programs cannot directly access variables, they can only be accessed by public methods provided to the outside by the class . [SetXxx && getXxx] Protect member variables, set assignment conditions in the method to ensure the rationality of the parameters
  • Construction method:
    • Construction method: reduce the amount of code, so that the class has better encapsulation. (If you don't write it, the new object needs to be assigned a value again.)
    • Construction method: The construction method is a special method, mainly to complete the creation of the object and the initialization of the object data . Make the class have better encapsulation . (If you don’t write it, a new object needs a separate line of code to assign a value to a property. [When you use it, you know how convenient it is to write])

Standard class/Java bean: member variables, methods,

The four components of JavaBean:

  1. All member variables must be modified with the private keyword
  2. Write Getter/Setter methods for each member variable
  3. Write a parameterless constructor
  4. Write a full parameter constructor

Inheritance: what the classes have in common

Make the subclass have the properties and methods of the parent class, you can also redefine in the subclass, and add properties and methods

Method rewriting

  • All the parameters must be the same
  • The subclass is not satisfied with the contract, so it is necessary to rewrite and write a contract

Tips:

  • By default, the first sentence of each subclass construction method is: super() "Parent-free construction"
  • Private variables and methods of the parent class are not inheritable

Polymorphism

Personal understanding of the three characteristics:

  • **Encapsulation: **The class contains the attributes and behaviors/methods of the object: people have age, gender, name, and some hidden attributes: thinking height, money, social relations, (can be regarded as private modification in the class Member variables of ); people can eat, watch TV, play games, sleep, and study, which are all human behaviors (that is, human methods). Abstracting various human attributes into human classes is the encapsulation of classes;
  • Inheritance : For example, if I inherit the functions of the parent class, and his cooking method is public, then I will too. I can also add a new method-the glory of the king, but my father will not, without this skill; (review the past, look forward to the future, the expansion of subcategories);
  • Polymorphism : The same method is executed differently in different objects. (Premise: need to have inheritance, rewrite, introduce father and new son)

Permission modifier

  • Static modified class variables and methods: sharing
  • Static member methods can only access static members

interface

Inner class

Guess you like

Origin blog.csdn.net/weixin_43801418/article/details/110481543