In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain "what is the method of reusing classes in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the method of reusing classes in Java"?
I. inheritance (inheritance)
1. When you overriding a function in derived class, you can only override the interface in base class, that is, the public or protected or friendly function in base class. If you try to overriding a private function, you just add a function to derived class, even though the compilation is successful. Such as
Class Cleanser {
Private void prt () {/ (b)
System.out.println ("Cleanser.prt ()")
}
}
Public class ExplicitStatic extends Cleanser {
Public void prt () {
System.out.println ("ExplicitStatic.prt ()")
}
Public static void main (String [] args) {
Cleanser x = new ExplicitStatic ()
X.prt (); / / (a)
}
}
Because prt () in Cleanser is private, it cannot be overridden in its derived class. Prt () in ExplicitStatic is just a function in ExplicitStatic, so an error occurs when you try to call prt () through polymorphism at (a). If you remove the private at (b), the result is
ExplicitStatic.prt ()
2. The use of Super
1) the superclass (parent class) of the current class can be called through the keyword super.
Example 6.1.1.1
Class Base {
Base () {System.out.println ("Base ()");}
Public void scrub () {System.out.println ("Base.scrub ()");}
}
Class Cleanser extends Base {
Private String s = new String ("Cleanser")
Public void append (String a) {Signora;}
Public void dilute () {append ("dilute ()");}
Public void apply () {append ("apply ()");}
Public void scrub () {append ("scrub ()");}
Public void print () {System.out.println (s);}
Cleanser () {
System.out.println ("Cleanser ():" + s)
}
Public static void testStatic () {
System.out.println ("testStatic ()")
}
Public static void main (String [] args) {
Cleanser x = new Cleanser ()
X.dilute (); x.apply (); x.scrub (); x.print ()
}
}
Public class ExplicitStatic extends Cleanser {
ExplicitStatic () {
System.out.println ("ExplicitStatic ()")
}
Public void scrub () {
Append ("Detergen.scrub ()")
Super.testStatic ()
Super.scrub (); / / calls Cleanser.scrub ()
}
Public void foam () {append ("foam ()");}
Public static void main (String [] args) {
ExplicitStatic x = new ExplicitStatic ()
X.dilute (); x.apply (); x.scrub (); x.foam ()
X.print (); System.out.println ("Test base class:")
Cleanser.main (args)
TestStatic ()
}
}
Running result:
Base ()
Cleanser (): Cleanser
ExplicitStatic ()
TestStatic ()
Cleanser dilute () apply () Detergen.scrub () scrub () foam ()
Test base class:
Base ()
Cleanser (): Cleanser
Cleanser dilute () apply () scrub ()
TestStatic ()
2) when a member in superclass is called through super, the most recent member is called.
Example 6.1.1.2
Class Base {
Protected String baseS = "Base"; / / (a)
/ / private String baseS = "Base"
Base () {System.out.println ("Base ()");}
}
Class Cleanser extends Base {
Protected String baseS = "Cleanser"; / / (b)
Public String s = new String ("Cleanser")
Cleanser () {
System.out.println ("Cleanser ():" + s)
}
Cleanser (String a) {
System.out.println ("Cleanser (" + a + "): s =" + s)
}
}
Public class ExplicitStatic extends Cleanser {
String S2 = s
String baseS = super.baseS; / / (c)
ExplicitStatic () {
Super ("ExplicitStatic")
System.out.println ("ExplicitStatic (): S2 =" + S2 + ", baseS =" + baseS + "super.baseS =" + super.baseS)
BaseS = "ExplicitStatic"
System.out.println ("baseS =" + baseS + ", super.baseS =" + super.baseS)
}
Public static void main (String [] args) {
ExplicitStatic x = new ExplicitStatic ()
}
}
Result 1:
Base ()
Cleanser (ExplicitStatic): s = Cleanser
ExplicitStatic (): S2 = Cleanser, baseS = Cleanser,super.baseS = Cleanser
BaseS = ExplicitStatic, super.baseS = Cleanser
In the above example, there are String bases instances in all three class. If you call baseS directly in ExplicitStatic, you are actually calling baseS in the current class ExplicitStatic (that is, the member at (c)); if you call baseS through super.bases, you are calling the baseS member closest to the current class ExplicitStatic, that is, the baseS instance in Cleanser class (that is, (b)), and the result is shown in result 1. If you comment out the statement at (b), baseS in Base class will be called, with the result shown in result 2.
Result 2:
Base ()
Cleanser (ExplicitStatic): s = Cleanser
ExplicitStatic (): S2 = Cleanser, baseS = Base,super.baseS = Base
BaseS = ExplicitStatic, super.baseS = Base
3. Initialization of Base class
2.1 when you generate a derived class object, it contains base class sub-objects (subobject). This sub-object is exactly the same as the other base class object you generate.
The constructor of base class can be called through super (), but it must be placed on the first line of the constructor and can only be used in the constructor.
2.3 the initialization sequence is:
1) load code (.class file)
2) initialize the static members of class in the order of "from the inside to the outside", that is, starting from base class.
3) call the constructor of base class in the constructor of derived class.
If the constructor of base class is not explicitly called through super () in the constructor of derived class, the compiler calls the default constructor of bass class and automatically generates the corresponding invocation statement, resulting in an instance of base class. If the constructor of the parent class is called through super () in the constructor of the derived class, the specified constructor is called. The calling order of the constructor is "from the inside out".
4) call the constructor of derived class.
* *: when base class does not have a default constructor, the constructor that calls base class must be displayed through super in the constructor of derived class.
Example: the initialization process of the following code is:
1) load the code for ExplicitStatic (load the ExplicitStatic.class file).
2) find ExplicitStatic about the keyword extends, load the base class code of ExplicitStatic (load Cleanser.class file).
3) find Cleanser about the keyword extends, load the base class code of Cleanser (load Base.class file).
4) initialize static members in Base class.
5) initialize static members in Cleanser class.
6) initialize static members in ExplicitStatic class.
If you comment out the code at (c), the initialization is over.
7) allocate storage space for the ExplicitStatic object and initialize the storage space to 0.
8) call super ("ExplicitStatic") in the construction of ExplicitStatic class (explicitly call the constructor of the parent class in the constructor of ExplicitStatic class) in an attempt to produce an instance of Cleanser class.
9) allocate storage space for Cleanser objects and initialize the storage space to 0.
10) because Cleanser class inherits from Base class, the constructor of the parent class is called automatically through super () in the constructor of the Cleanser class (because there is no explicit call to the constructor of the parent class, so the constructor of the parent class is automatically called) in an attempt to generate an instance of Cleanser class.
11) generate an instance of Base class. Initialize the member variable before calling the constructor.
12) go back to Cleanser class and generate an instance. Initialize the member data in Cleanser class before executing the rest of the constructor Cleanser (String a).
13) go back to ExplicitStatic class and generate an instance. First initialize the member data in ExplicitStatic class, and then execute the rest of the constructor ExplicitStatic () (System.out.println ("ExplicitStatic ()").
Class Base {
Static int S1 = prt ("S1 initialized.", 11)
Int i1 = prt ("i1 initialized.", 12)
Base () {
System.out.println ("Base ()")
System.out.println ("S1 =" + S1 + ", i1 =" + i1)
Draw (); / / (d)
}
Void draw () {
System.out.println ("base.draw:s1 =" + S1 + ", i1 =" + i1)
}
Static int prt (String s, int num) {
System.out.println (s)
Return num
}
}
Class Cleanser extends Base {
Static int S2 = prt ("S2 initialized.", 21)
Int i2 = prt ("i2 initialized.", 22)
Cleanser () {
System.out.println ("Cleanser ()")
System.out.println ("S2 =" + S2 + ", i2 =" + i2)
}
Cleanser (String a) {
/ / super (); (b)
System.out.println ("Cleanser (" + a + ")")
System.out.println ("S2 =" + S2 + ", i2 =" + i2)
}
Void draw () {
System.out.println ("Cleanser.draw:s2 =" + S2 + ", i2 =" + i2)
}
}
Public class ExplicitStatic extends Cleanser {
Static int S3 = prt ("S3 initialized.", 31)
Int i3 = prt ("i3 initialized", 31)
ExplicitStatic () {
Super ("ExplicitStatic"); / / (a)
System.out.println ("ExplicitStatic ()")
System.out.println ("S3 =" + S3 + ", i3 =" + i3)
}
Public static void main (String [] args) {
ExplicitStatic x = new ExplicitStatic (); / / (c)
}
}
Results:
S1 initialized.
S2 initialized.
S3 initialized.
/ / if you comment out the code at (c) and the output ends here, the following result will not be output
I1 initialized.
Base ()
S1 = 11, i1 = 12
Results at Cleanser.draw:s2 = 21, i2 = 0max / (d)
I2 initialized.
Results at Cleanser (ExplicitStatic) / / (a)
S2 = 21, i2 = 22
I3 initialized
ExplicitStatic ()
S3 = 31, i3 = 31
I2 is 0 because i2 in Cleanser is not initialized when draw () is called in Base (), while storage space is initialized to 0 when the storage space is allocated to the Cleanser object.
2.4.The (a) in the code and result indicates that the base class instance of the current class is generated first, otherwise the base class member cannot be called in the derived class. When calling Cleanser class's constructor Cleanser (String a), there is no explicit call to Base class's constructor with super in Cleanser (String a), so the system automatically generates statements that call Base class's default constructor, such as (b).
4. The quick choice between combination and inheritance
. 1) inheritance represents a "is-a" relationship, for example, a truck is a kind of car; a combination represents a "has-a" relationship, such as a car has four wheels.
2) whether the new class needs to be transformed up to base class.
5. Access rights in inheritance
The protect variable can be called by descendant classes. For example, baseS in Base class can be called by Cleanser class and ExplicitStatic class.
Class Base {
Protected String baseS = "Base"
/ / private String baseS = "Base"
Base () {System.out.println ("Base ()");}
}
Class Cleanser extends Base {
Protected String baseS = "Cleanser"
Public String s = new String ("Cleanser")
Cleanser () {
System.out.println ("Cleanser ():" + s)
}
Cleanser (String a) {
System.out.println ("Cleanser (" + a + "): s =" + s)
}
}
Public class ExplicitStatic extends Cleanser {
String S2 = s
String baseS = super.baseS
ExplicitStatic () {
Super ("ExplicitStatic")
System.out.println ("ExplicitStatic (): S2 =" + S2 + ", baseS =" + baseS + "super.baseS =" + super.baseS)
BaseS = "ExplicitStatic"
System.out.println ("baseS =" + baseS + ", super.baseS =" + super.baseS)
}
Public static void main (String [] args) {
ExplicitStatic x = new ExplicitStatic ()
}
}
Results:
Base ()
Cleanser (ExplicitStatic): s = Cleanser
ExplicitStatic (): S2 = Cleanser, baseS = Cleanser, super.baseS = Cleanser
BaseS = ExplicitStatic, super.baseS = Cleanser
II. Keyword final
1.Final data
1.1 final data
1) when a base type is defined as final, its data value cannot be changed. Such as
Final int i = 9
I compiled error / compilation error, cannot change the value of I
2) when object reference is defined as final, what cannot be changed is the reference, not the object itself. Such as
Class Value {
Int I = 1
}
Public class ExplicitStatic extends Cleanser {
Public static void main (String [] args) {
Final Value v = new Value (); / / v.I = 1
V. I = 2
/ / v = new Value ()
}
}
Because v is final, you cannot redirect v to an object through new Value (); however, the value of the object that v points to can be changed (v. iException +).
1.2 blank finals
We can declare the data member as final without giving the initial value, which is blank finals. However, blank finals must and can only be initialized in the constructor.
Public class ExplicitStatic {
Final int ib
Final int i = 1
ExplicitStatic ()
{
Ib = 2 + + / (a)
/ / I = 3; (b)
System.out.println ("I =" + I + ", ib =" + ib)
}
Public static void main (String [] args) {
ExplicitStatic ex = new ExplicitStatic ()
}
}
Ib is blank finals, so it can be initialized in the constructor. If you comment out the code at (a), ib has no initial value and there is an error in compilation. While I has been initialized at the definition, the value of I cannot be changed, and the code compilation error at (b).
* *: non-blank finals members cannot change their values even in the constructor
2.Final methods
1) functions declared as final cannot be overridden
2) all private functions in class will naturally be final.
1. Final classes
1) when a class is declared as final, it means that it cannot be inherited, but the data member of class is not final and can be changed. Such as
Class SmallBrain {}
XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" / >
Final class Dinosaur {
Int I = 7
Int j = I
SmallBrain x = new SmallBrain ()
Void f () {}
}
/ / cannot inherit final function
/ / class Further extends Dinosaur {}
Public class ExplicitStatic {
Public static void main (String [] args) {
Dinosaur n = new Dinosaur ()
F ()
N. I = 40 non-final data members in the final class can be changed.
N. Jacks +
}
}
2) all functions in final class are naturally final, because no one can overwrite them.
At this point, I believe you have a deeper understanding of "what is the method of repeated use of classes in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.