Java review _ Chapter 5, object-oriented (on)

Chapter 5, Object Oriented (on)

  • Naming rules

    • Must be made up of meaningful words
      • Class name, capitalize the first letter of each word (Student
      • Member variable names, the first letter of the word is lowercase, and the first letter of each word is capitalized (phoneNumber
      • Method name, the first word is lowercase, the verb begins (goOut
  • static keyword

    • Modified are generic member variables and methods, which are shared by the class and are called through the class.
      • In particular, static member variables are generic and shared by all objects (objects are not recommended to call static variables and methods)
      • Therefore, the specification: use the class name to call generic methods and variables
    • Otherwise it is owned by the object (called instance variables, instance methods)
    • Therefore, static methods cannot directly call or access non-static members
    • The essential role of static is to distinguish whether variables and methods belong to a class or an instance object.
  • Person p = new Person()

    • In fact, p is a pointer variable (also called a reference variable), which holds the address of the actual object
    • p variables are stored in stack memory, objects are stored in heap memory
    • The object can only be accessed through the pointer p
    • When there is no pointer variable pointing to an object in the heap memory, the object will be recycled by the recycling mechanism of the JVM
    • Therefore, if you want to destroy an object, you only need to set the pointer variable pointing to it to null.
  • this

    • Which object is calling the method, this represents which object
    • It is too common for a member function to call another member function, so this can be omitted at this time
    • static method cannot use this
    • You can use this as the return value to return the object itself
  • method

    • In structure-oriented programming languages, functions are the basic unit
    • But in Java, classes are. Therefore, the method cannot exist independently, it can only belong to the class
    • In java, only the value is passed as a parameter. When passing in an object, what is passed is a pointer
  • Variable number of parameters method

    • public void test(int a, String… books){

for( String tmp :books){
sout(tmp);
}
}

  • You can pass in multiple books at this time

  • Access control character

    • private can only be accessed inside the current class
    • default Default, package access permission, can be accessed by other classes under the same package
    • Protected subclass access rights, not only can be accessed by other classes in the same package, but also by subclasses in different packages
      • Used for methods that only want to be overridden by subclasses, but do not want to be called directly by the outside world
    • public public access
  • package

    • Provides a multi-level namespace for classes, used to resolve class naming conflicts, class file management, etc.
    • Put the same group of functionally related classes in the same package to form a logical class library unit
    • Package structure
      • cn.ac.iie.cloud etc.
      • kensin.projectname
  • import

    • Import package After importing the package, you don’t need to add the package name prefix when calling a certain class
    • import static A certain class, after importing, you can directly call the static variables and methods in that class, you can omit the class name
  • Constructor

    • You can use this() to call another constructor in an overloaded constructor, but it must be the first statement of the constructor body
  • Class inheritance

    • extends means extension, that is, the child class is an extension of the parent class, but it does inherit the members of the parent class
    • A java class can only have one direct parent class, but it can have an infinite number of brief parent classes
    • All classes inherit from java.lang.Object by default
    • The subclass can override the method in the parent class, if you call the method in the parent class that is covered by the subclass: super.
    • super is used to call the member variables and methods of the subclass inherited from the parent class
    • Overloading is a method of the same name in the same class, and overriding is a subclass overriding the method of the same name in the parent class
    • In the constructor of the subclass, you can explicitly call the constructor of the parent class super(,)
    • The constructor of the parent class is always executed before the constructor of the child class
  • Polymorphism

    • Variables of the same type exhibit a variety of different behavioral characteristics when calling the same method. This is polymorphism.
    • For example, the base class is printer printer
      • Printer p1=new CaisePrinter
      • Printer p2=new HeibaiPrinter
      • At this time, call the printing method of p1, p2, and print out different effects, namely polymorphism
    • instanceof judges whether the previous object is the latter class, or an instance of its subclass, instance class
      • Generally, before forced conversion, judge whether it can be successfully converted to avoid ClassCastException
  • Inheritance and composition

    • Encapsulation: The class should encapsulate its internal information and implementation details, and only expose the necessary methods to other classes. (Low coupling)

    • But inheritance will destroy the encapsulation of the parent class, and can even override the method of the parent class, resulting in a serious coupling between the child class and the parent class

    • The principle of designing the parent class:

      • Try to hide the internal data of the parent class. Try to set it as private and prevent subclasses from accessing it.
      • Don't let subclasses freely access the methods of modifying the parent class.
        • The auxiliary methods in the parent class should be private to prevent subclasses from overriding.
        • The methods in the parent class need to be called externally, but do not want the subclass to modify, use final modification
        • The method in the parent class hopes to be overridden by the subclass, but does not want to be accessed by other classes. Use protected
        • Try not to call methods that will be overridden by subclasses in the constructor of the parent class
    • When do you need to derive new subclasses?

      • Subclasses need additional attributes, not just changes in attribute values
      • Subcategories need to add their own unique behavior
    • Combination: Combine objects of the old class as member variables of the new class to implement the method of the new class

      • Usually use private to modify the old class of objects, because you want the user to see the new class of methods
      • The Person class needs to reuse the methods of the Arm class so that arm can be used as a member variable of Person
      • There is no essential difference in system overhead between composition and inheritance
  • Initialization block

    • Normal initialization block, static initialization block (initialize the class variables of the entire class)
    • Execution sequence: initialization block -> constructor -> member variable assignment

Guess you like

Origin blog.csdn.net/xiaohaigary/article/details/108848546