In-depth research

Source: Internet
Author: User
Class-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1: Use inheritance in Java
The most powerful feature in Object-Oriented Programming is class inheritance. class inheritance allows you to write new programs on an existing class. For example, you want to create a rectangle class that can be displayed on the screen and filled with it. You can start from scratch or use the old rectangle class, the following section describes how to inherit an existing Rectangle class without rewriting the code.
For example, to create a fillRect class, this class can use all the defined data and member functions in the Rectangle class, such as data such as width and height, and member functions such as getArea, but it is implemented using an inherited method. Use the extands keyword to enable Java programmers to inherit existing class member functions. to inherit the Rectangle class, you must reference the old Rectangle class and you must reference the old Rectangle class, and reference it in the description of the new class, for example:

Import Shapes. Rectangle;

Class fillRect extands Rectangle
{
.....

}


2: overload of member functions
After inheritance, how can we make the fillRect analogy to the Rectangle class improve? We can use the following code to implement a new drawRect member function, which will greatly shorten the code and fill the rectangle, rather than 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 the StringBuffer class is used here. The reason why StringBuffer is used is that String can only generate one static type-its size cannot be changed, and 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 a new member function to replace the old member function. However, member functions described as final cannot be overloaded.
Note: you do not have to include the same code as the inherited class in the new class, but you only need to add what you want, but you must create a new constructor, to distinguish these two different classes.
The overall picture of the new class is as follows. You can find that the Code becomes very simple and clear 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: Use Interfaces
Java can create a class called interface. In this class, all member functions are abstract, that is, they are not defined only by the description, you can declare an interface as follows.
Public interface interfaceName
// Member function description

The default reference type of the member functions in the interface is private. The internal variables of the interface cannot be changed and are always static and final.
By using the keyword implement, You can inherit an interface when defining a class. However, unlike extends, a class can inherit multiple interfaces at the same time.
What are the advantages of using interfaces? By creating an interface, you can declare a complete set of abstract member functions without having to implement them. All classes that inherit this interface will have the same original form of member functions. For example, if you want all shapes to have a draw () member function, you can create an interface and name it using Shape:

Public interface Shape
Void draw ();

Now, whenever you create a class that inherits the Shape, you will have a member function draw ().

4: class conversion
Class conversion is similar to the conversion between different types of variables, but not the same.
We can convert a parent class object into a subclass object. The following code illustrates an example of class conversion, where class Y is inherited from Class X:

Y y = new Y ();
X x;
X = y;

Note that the conversion between two subclasses is not acceptable.

5: null, this and supper Variables
All classes have three variables: null, this, and supper.
The null variable does not point to any actual object, but to an empty object, as shown in the following example:

Rectangle rect = null;

In this example, a variable of the rectangle class is generated, but no actual object is created. In addition, if a member function requires an object as a parameter, you can use null instead.
This variable points to the object itself. A class can use this variable to obtain an object variable representing itself.
The supper variable is a variable pointing to the class parent class constructor. You can call it to quickly design the constructor of the subclass.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.