Class research

zhaozj2021-02-12  182

INDUSTOMS APRODI ANSE 07, 10:29

1: Use inheritance in Java

The most powerful functionality for object-oriented program is the inheritance of the class. The inheritance of the class allows you to write new programs on a already existing class, for example, you want to build a rectangle that can be displayed on the screen and populating it. Class, you can start from scratch or using the old rectangular class, the following section will introduce you how to inherit the existing Rectangle class without rewriting the code.

For example, establish a FillRect class that can be implemented using all the defined data and members functions in the Rectangle class, such as: Width, Height and other member functions such as Getarea, the use of inheritance methods. Using the extands keyword allows the Java programmer to inherit the already class member function. In order to inherit the Rectangle class, you must quote the old Rectangle class, you must reference the old Rectangle class, and reference it in the new category, such as:

Import shapes.Rectangle;

Class FillRect ExtAnds Rectangle

{

.....

}

2: Overload of member functions

After inheriting, how to make the FillRect class improve than the Rectangle class? We can use the following code to implement a new DrawRect member function, which will greatly shorten the code and can fill the rectangle instead of just draw the outline of the rectangle:

Private string makeString (chr ch, int num)

{

StringBuffer str = new stringbuffer ();

For (int I = NUM; I> 0; I -)

Str.Append (CH);

Return str.tostring ();

}

Public void DrawRect ()

{

For (int i = height; i> 0; I -)

System.out.println (MakeString ("#", width);

}

Note that we use the StringBuffer class. The reason why StringBuffer is because String can only produce a static type - its size cannot be changed, while StringBuffer can generate a variable length string type.

Here, the DrawRect member function is overloaded, by using the same member function name, you can use the new member function instead of the old member function. However, those who are illustrated for Final cannot be overloaded.

Note that you don't have to include the same code as the inherited class in the new class, but you only need to join what you want, but you have to build a new constructor function to distinguish these two different classes.

The whole picture of the new class is as follows, you can find that the code becomes very simple by inheriting the Rectangle class.

Class FillRect ExtAnds Rectangle

{

Public FillRect (int W, INT H)

{

Supper (W, h);

Private string makeString (char ch, int Num)

{

StringBuffer str = new stringbuffer ();

For (int I = NUM; I> 0; I -)

Str.Append (CH);

Return str.tostring ();

}

Public void DrawRect ()

{

For (int i = height; i> 0; I -)

System.out.printlm (MakeString ("#", width);

}

}

}

3: Using the interface

Java can create a class called an interface (Interface), in this class, all member functions are abstract, that is, they all have not explained, you can explain an interface as follows.

Public Interface InterfaceName

// Member function description

The default reference type of the member function in the interface is the internal variable of the Private, the interface. It is not changeable, and always static and final.

By using keyword import, you can inherit an interface when defining a class. However, it is different from Extends that a class can inherit multiple interfaces at the same time.

What is the advantage of using the interface? By creating an interface, you can explain a complete set of abstract member functions without having to implement it, all classes inherited this interface, will have the same original member function. For example, you want all Shapes to have a Draw () member function, you can create an interface and name your shape:

Public Interface Shape

Void Draw ();

Now, no matter when you create a class inherited from Shape, you will have a member function DRAW ().

4: Conversion of class

The transformation of the class is similar to the conversion between different types of variables, but is different.

We can convert a parent class object into a sub-class object, the following code descriptions an example of a class conversion, where the Y class is inherited from the X class:

Y y y = new y ();

X x;

X = Y;

It should be noted that the conversion between the two subclasses is not possible.

5: NULL, THIS and SUPPER variables

All classes have three variables: null, this and support.

NULL variables do not point to any actual object, but point to an empty object, as shown in the following example:

Rectangle Rect = NULL;

This example produces a variable of a rectangular class, but does not create an actual object. In addition, if a member function requires an object as a parameter, you can also use NULL instead.

The THIS variable points to the object itself, a class can get a target variable representing its own object via the THIS variable.

The SUPPER variable is a variable that points to the parent class constructor, you can quickly complete the design of the constructor function of the subclass by calling it.

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

New Post(0)