In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what is the use of the class in Java. It is very detailed and has a certain reference value. Friends who are interested must finish it!
1: using inheritance in Java
Object-oriented programming in the most powerful function is class inheritance, class inheritance allows you to write a new program on top of an existing class, for example, you want to create a screen can display and fill it with the rectangular class, you can start from scratch or use the old rectangular class, the following section will show you how to inherit the existing Rectangle class without rewriting the code.
For example, the establishment of a fillRect class, this class can use all the defined data and member functions in the Rectangle class, such as: width, height and other data and getArea and other member functions, can be implemented using inherited methods. Use the extands keyword to enable Java programmers to inherit member functions of existing classes. In order to inherit the Rectangle class, you must reference the old Rectangle class, you must reference the old Rectangle class, and reference it in the description of the new class, such as:
Import Shapes.Rectangle
Class fillRect extands Rectangle
{
.
}
2: overloading of member functions
After inheritance, how do you improve the fillRect analogy to the Rectangle class? We can implement a new drawRect member function with the following code, which will greatly shorten the code and fill the rectangle instead of just drawing the outline of the rectangle:
Private String makeString (chr ch,int num)
{
StringBuffer str=new StringBuffer ()
For (int iridanumwiti > 0positimuri -)
Str.append (ch)
Return str.toString ()
}
Public void drawRect ()
{
For (int iTuneheighttern I > 0 bori Muhami -)
System.out.println (makeString (#), width))
}
Notice that we use the StringBuffer class here. StringBuffer is used because String can only produce a static type-its size cannot be changed, while StringBuffer can produce a variable length string type.
Here, the drawRect member function is overloaded, and you can replace the old member function with the new member function by using the same member function name. However, member functions that are specified as 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 add what you want, but you must create a new constructor function to distinguish between the two different classes.
The full picture of the new class is shown below, and you can find that by inheriting the Rectangle class, the code becomes very simple and straightforward.
Class fillRect extands Rectangle
{
Public fillRect (int w.int h)
{
Supper (WBH)
Private String makeString (char ch,int num)
{
StringBuffer str=new StringBuffer ()
For (int iridanumwiti > 0positimuri -)
Str.append (ch)
Return str.toString ()
}
Public void drawRect ()
{
For (int iTuneheighttern I > 0 bori Muhami -)
System.out.printlm (makeString (#), width))
}
}
}
3: use the interface
Java can create a class called interface, in which all member functions are abstract, that is, they are only described but not defined, and you can illustrate an interface as shown below.
Public interface interfaceName
/ / description of member function
The default reference type of the member function in the interface is private, and the internal variables of the interface (interface) are immutable and are always static and final.
By using the keyword implement, you can inherit an interface when defining a class. Unlike extends, however, a class can inherit multiple interfaces at the same time.
What are the advantages of using interfaces? By creating an interface, you can specify a set of abstract member functions without having to implement it, and all classes that inherit this interface will have member functions with the same prototype. For example, if you want all shapes to have a draw () member function, you can create an interface and name it with Shape:
Public interface Shape
Void draw ()
Now, whenever you create a class that inherits from Shape, you will have a member function draw ().
4: class conversion
The conversion of a class is similar to the conversion between different types of variables, but not the same.
We can convert an object of a parent class into a subclass object. The following code shows an example of a class transformation, where class Y inherits from class X:
Y y=new Y ()
X x
Xfanty
It is important to note that conversion between two subclasses is not possible.
5Rank nullpaper 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
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 an argument, you can also use null instead.
The this variable points to the object itself, and a class can get an object variable that represents itself through the this variable.
The supper variable is a variable that points to the constructor function of the parent class of the class. You can quickly complete the design of the constructor function of the subclass by calling it.
The above is all the content of this article "what is the use of classes in Java?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.