block与debug

block与debug

block

Block {}, has its own scope in java and can be divided into

Static code block Structure code block Normal block Synchronization code block
Declare location In class, outside method In class, outside method Method fynchronized(){}
effect The entire class performs certain initialization operations (static property assignment...) The construction code block is for the object initialization operation (assigning values ​​to static or non-static member properties...) Declare some temporary variables etc... Control concurrency
Timing of execution When the class is loaded for the first time, it is executed only once. If there are multiple static blocks, they are executed once from top to bottom. When creating an object, execute it before executing the constructor code, if there are multiple, execute it from top to bottom Follow method execution Follow the method execution

note:

  • The static code block is executed when the class is loaded for the first time; when the class is loaded multiple times, the static code block is executed only once; the static block is often used to initialize static variables.

  • It is executed when the class is initialized, not when the object is created.

  • Non-static members cannot be accessed in a static initialization block.

  • The building block is compiled to be executed before the constructor code to be executed

Static blocks, which are only loaded when the class is used for the first time.
The building block is executed before the constructor and is executed every time an object is created

debug

In the learning or development process, it is unavoidable to encounter bugs. In order to be able to debug quickly, you can use the debug debugging tool.

Debugging a Java program is very simple, mainly including setting breakpoints, starting debugging, single-stepping, and ending debugging.

Debug interface window:

1) Set a breakpoint

2) Start debugging

Eclipse provides several ways to start the debugging of the program (Launch), namely through the menu (Run -> Debug), the icon ("green bug"), and the right button -> Debug As.

A prompt pops up, you need to switch to the Debug work area, tick "Remember my decision", remember your choice, you will not be prompted next time, and then click [Yes].

3) Single step

Mainly use the several views mentioned above for debugging, and several buttons in the debug view have shortcut keys:

Step Return(F7): Indicates to exit the current method and return to the calling layer.

Step Over (F6): Indicates to run the next line of code.

Step Into (F5): Indicates to enter the current method.

4) End debugging

Terminate the debugging of the local program through the Terminate command.

Object Orientation-Inheritance

inherit

"A bird on the tree and two rabbits under the tree. How many kinds of animals, how many kinds of creatures?" There is the concept of inheritance.

Inheritance: the son inherits the father's inheritance

The essence of inheritance lies in abstraction. Class is the abstraction of objects, and inheritance is the abstraction of a certain batch of classes, so as to achieve better modeling of the real world.

The role of inheritance: the use of inheritance can improve code reusability.

How to use inheritance:

Parent class|Super class|Base class: Abstract according to some subclasses, extract the image part, and define it in the parent class

Subclass|Derived Class: The subclass inherits the parent class, has the right to use the content in the parent class, and can define the new content of the subclass, so the subclass is the continuation + extension of the parent

The extends keyword means "extend". The child class is an extension of the parent class.

In java, use the <font color="red">extends </font> keyword to implement the inheritance mechanism of the class, the grammar rules:

<modifier> class <name> [extends <superclass>]{} 
//父类
class Person{
    public String name;
    public int age;

    public void sleep(){
        System.out.println("休息");
    }
}
//子类
//教师类
class Teacher extends Person{
    public String subject;

    public Teacher() {
    }

    public void teach(){
        System.out.println("传授知识");
    }

}

//学生类
class Student extends Person{
    public String school;

    public Student() {
    }

    public void study(){
        System.out.println("在知识的海洋畅游!");
    }
}

note:

  • The child class inherits the member variables and member methods of the parent class, but does not inherit the construction method of the parent class
  • There is only single inheritance in java, not multiple inheritance like C++. Multiple inheritance can cause confusion, making the inheritance chain too complicated and the system difficult to maintain. Just like in our reality, if you have multiple parents, what a chaotic world is that. Multiple inheritance is to achieve code reusability, but it introduces complexity, which makes the relationship between system classes confused.
  • Multiple inheritance in java can be achieved through interfaces
  • If you do not call extends when you define a class, its parent class is java.lang.Object.

Inherited characteristics:

advantage:

  • Through inheritance, class definition can be simplified and code reuse can be realized|improved code reusability
  • Can be better extended
  • Once the subclass inherits the parent class, it can have the right to use the members in the parent class, or it can extend the definition of the unique content of the subclass
  • Java is single inheritance, simple to implement

Disadvantages:

  • The child class and the parent class are tightly coupled (high coupling), the child class depends on the implementation of the parent class, and the child class lacks independence.
  • Not easy to maintain
  • Single inheritance of a subclass can only have one parent class, which is not flexible enough and not convenient for later maintenance

Guess you like

Origin blog.51cto.com/14966126/2542625