In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to distinguish between the inner class and the exception class of Java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to distinguish between the inner class and the exception class of Java.
1 Internal class
Java supports declaring another class in one class, which is called an inner class, and a class that contains an inner class becomes an outer embedded class of the inner class.
Class variables and class methods cannot be declared in the class body of an inner class. An inner class can be used in the body of an embedded class to declare an object as a member of the embedded class.
Rules for the use of inner classes:
(1) declaring an inner class is like declaring a method or variable in a class, and a class treats the inner class as its own member.
(2) an object in the class body of an embedded class that can be declared by an inner class as a member of the embedded class.
(3) the member variables of the embedded class are still valid in the inner class, and the methods in the inner class can also call the methods in the embedded class.
(4) Class variables and methods cannot be declared in the class body of an inner class.
(5) two .class files are generated when the embedded class and the inner class are compiled.
For example, if a certain type of farm raises a special kind of cattle, but does not want other farms to raise this special kind of cattle, then this type of farm can take the class that created this special breed of cattle as its own internal class.
In example 1 (Example1.1.java) below, there is a RedCowForm (Red Bull Farm) class with an inner class named RedCow (Red Bull).
RedCowForm.java
Public class RedCowForm {static String formName; RedCow cow; / / inner class declaration object RedCowForm () {} RedCowForm (String s) {cow = new RedCow (150,112,5000); formName = s;} public void showCowMess () {cow.speak ();} class RedCow {/ / inner class declaration String cowName = "Red Bull"; int height, weight, price RedCow (int h, int w, int p) {height = h; weight = w; price = p;} void speak () {System.out.println ("I am" + cowName + ", height:" + height + "cm weight:" + weight + "kg, living in" + formName);}} / / Internal Class end} 115526523
Example1.1.java
Public class Example1_1 {public static void main (String [] args) {RedCowForm form = new RedCowForm (Red Bull Farm); form.showCowMess (); form.cow.speak ();}}
It should be noted that the name of the bytecode file of the inner class generated by the Java compiler is different from that of the usual class, and the bytecode file corresponding to the inner class is in the format of "embedded class name $internal class name". For example, the bytecode file of the inner class in example 1 is RedCowForm$RedCow.class. Therefore, don't forget the bytecode file of the inner class when you need to copy the bytecode file to other developers.
Inner classes can be decorated as static inner classes, for example, the inner class declaration in example 1 can be static classRedCow. A class is a data type, so the static inner class is a static data type in the outer embedded class, so that the program can use the static inner class to create objects in other classes. It is important to note, however, that static inner classes cannot manipulate instance member variables in embedded classes.
If you change the inner class RedCow in example 1 to static inner class, you can use the Example1_ 1 in example 1
Add the following code to the main method of the main class.
RedCowForm.RedCow redCow = new RedCowForm.RedCow (180,119pr 6000); redCow.speak ()
Note: a non-inner class cannot be a static class
2 Anonymous Class 2.1 Anonymous classes related to subclasses
When creating a subclass object, there is a class body in addition to using the constructor of the parent class, which is considered to be a class body after the subclass is removed from the class declaration, which is called an anonymous class.
Assuming that Bank is a class, the following code creates an object with a subclass of Bank (anonymous class):
New Bank () {Class body of anonymous class}
Anonymous classes related to subclasses:
(1) an anonymous class is a subclass. Because no name is available, it is impossible to declare an object with an anonymous class, but you can create an object directly with an anonymous class.
(2) Anonymous classes can inherit or override the methods of the parent class.
(3) when using an anonymous class, you must create an object directly with an anonymous class in a class, so the anonymous class must be an inner class.
(4) anonymous classes can access member variables and methods in embedded classes, and static member variables and static methods cannot be declared in the class body of anonymous classes.
(5) because the anonymous class is a subclass, but does not have a class name, when creating an object with an anonymous class, use the constructor of the parent class directly.
Example 2: there are 4 classes in this class: (Example2_1. Java), ShowBoard class,
OutputAlphabet type, OutputEnglish. Java . The object of this anonymous class is responsible for outputting the Greek alphabet.
OutputAlphabet.java
Abstract class OutputAlphabet {public abstract void output ();}
OutputAlphabet .java
Public class OutputEnglish extends OutputAlphabet {/ / output the English alphabet subclass public void output () {for (char caterpillar
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.