Vital Java Travel Seven

xiaoxiao2021-03-06  14

Abstract class and abstract method

Abstract class: Create a public interface for all classes derived from it. The only reason for creating this public interface is that each subclass uses its own way to implement this interface. It defines a basic form that you can say this is all of the derived categories. There is also a saying that "Abstract Base Class is simplified to abstract class abstract class". When you want to manipulate a group of classes through a public interface, you can use an abstract class. Through dynamic binding mechanisms, those derivatives that meet the characteristics will be called.

If you have a Abstract Class like Instrument, then this kind of

It's not what it means. That is to say that INSTRUMENT is only used to define the interface, not

Specific implementation, there is no meaning of creating an Instrument object, not to mention that you may also be banned.

Let users do this. To do this, let the Instrument method print the error message, but this will leave the problem to the runtime, so the user needs to perform a detailed measurement.

test. A better way is to find this problem at compile.

Java provides a mechanism called "Abstract Method" to solve

this problem. This is a method that has not been completed; this method only has a statement, no positive

Text. Below is the statement of abstract methods:

Abstract void f ();

Classs containing abstract methods are called "Abstract Class". If the class contains one or more abstract methods, then this class must be defined as Abstract. (Otherwise, the compiler will report an error.)

Since the abstract class is a class that has not been completed, then if someone wants to create an abstract class object, what is the compiler? The compiler can't securely create an abstract class object, so it will report an error. Thus, the compiler guarantees the pureness of the abstract class, and you don't have to worry that it will be misused.

If you inherit an abstract class, but also intend to create this new class, you must implement all the abstraction methods defined by the base class. If you don't do this (you can choose not to do this), then this inherited class has become an abstract class, the compiler will force you to declare this class with the Abstract keyword. Creating an Abstract class that does not contain the Abstract method is completely possible. This technique can be used to "ways to create Abstract, but to ban others from creating this class object".

Call order of constructor

In the process of creating a school-based object, the constructor of the base class is always called first, so that the primary class is traced back, each base class constructor will be called. This approach is very reasonable because the constructor has a special task: it knows that the object is not created correctly. Detective classes can only access its own members, it can't see the base class (because they are usually private). Only the base class constructor knows how to initialize its members, and only it has permission to initialize. Therefore, "all the constructors are called once" will become very important, otherwise the object can not be created. This is why the compiler will force each derived class to call the constructor of its base class. If you don't explicitly call the constructor of the base class in the constructor of the derived class, the compiler will quietly call the default constructor. If there is no default constructor, the compiler will report an error. (If the class does not construct a function, the compiler will automatically prepare a default constructor for you.)

The order of calling the constructor of complex objects is like this:

1. Call the constructor of the base class. This is a recursive process, so you will create a inheritance system first.

The root, then the next level is derived, and so on until the last inherited structure

Function.

2. Member objects are initialized in the order of their statements.

3. Perform the text of the constructor of the inheritance class.

Abstract Class Glyph {Abstract Void Draw ();

Glyph () {

System.out.println ("Glyph () Before Draw ()");

Draw ();

System.out.println ("glyph () after draw ()");

}

}

Class roundglyph extends glyph {

Private int RADIUS = 1;

Roundglyph (int R) {

RADIUS = R;

System.out.println (

Roundglyph.Roundglyph (), RADIUS = " RADIUS);

}

Void Draw () {

System.out.println (

Roundglyph.draw (), Radius = " RADIUS);

}

}

Public class polyconstructors {

Private static test monitor = new test ();

Public static void main (String [] args) {

New rounceph (5);

Monitor.expect (new string "{

"Glyph () before draw ()",

"Roundglyph.draw (), Radius = 0",

"Glyph () after draw ()",

"ROUNDGLYPH.ROUNDGLYPH (), RADIUS = 5"

});

}

}

1. Before making other work, the memory that is assigned to this object will be initialized to two into

zero.

2. As mentioned above, the construction function of the base class first. At this time, the overwritten DRAW () method (yes, call before calling the constructor of the RoundGlyph), this

It is found that the value of RADIUS is still zero due to the influence of the first step.

3. Data members are initialized in order of their declaration.

4. Call the body of the derived class constructor.

A good constructor should be, "use the least amount of work to set the status of the object, and avoid calling the method as much as possible." The way the constructor can securely call the method, the FINAL method of the base class.

转载请注明原文地址:https://www.9cbs.com/read-49836.html

New Post(0)