Java internal class, Java
Here, I will explain the basic knowledge of internal classes. Before learning about Android development, we should have some knowledge of internal departments and classes, in actual development of Android, the definition of internal classes is very common. All the small editors should explain it here to understand the syntax definition rules of internal classes.
Objective:
Understand internal class definitions
Master the advantages and disadvantages of internal classes
Understand the usage format of internal classes
Measure the test taker's knowledge about the role of static declarations of internal classes.
Internal class definition
In Class B, A class is defined as A, which is usually called an internal class in Class B. The internal class can be declared as public or private. When an internal class is declared as public or private, its access restrictions are exactly the same as that of member variables and member methods, and the internal class is often defined to implement high cohesion between classes, the code is not open to the outside, so that some sensitive data is not so easily accessed.
Syntax
Identifier class external class name {// external class member identifier class internal class name {// internal class member }}
Form
In internal class learning, the most common internal class has the following three forms:
Member internal class, method internal class, anonymous internal class
Features:
Member internal class: an external class is defined inside an external class and is called an external class. This class features high cohesion and is not open to the outside world.
Internal class of a method (Local internal class): The class defined inside a method is usually called internal class of a method or internal class of a local method. This class is transient and non-external.
Anonymous internal class: To perform anonymous internal class operations, you must be an abstract class or an interface in order to use internal class operations. When implementing an internal class operation, at the same time, it is a feature of rewriting unimplemented methods and high cohesion.
2. Master the advantages and disadvantages of internal classes
When explaining the advantages and disadvantages of internal classes, we can write two programs to briefly compare the advantages and disadvantages of internal classes.
Package anonymousinner; public class OutClass {// defines an external class String attribute private String info = "internal class "; // define an internal class Inner {// write an internal class method public void printInfo () {System. out. println (info) ;}/// defines an external class method public void print () {// executes the call internal class method operation new Inner (). printInfo ();} public static void main (String [] args) {new OutClass (). print ();}}
2. Case 2
Package anonymousinner. test; class Out {// define a String attribute private String info = "internal class"; // output the Inner Method Content public void print () {new Inner (this ). printInfo () ;}public String getInfo () {return this.info ;}} class Inner {private Out out; public Inner (Out out Out) {this. out = out;} // compile an internal class method public void printInfo () {System. out. println (out. getInfo () ;}} public class OutClassTwo {public static void main (String [] args) {// test output new Out (). print ();}}
Based on the code of the above two cases, we can see that the amount of code used for internal classes is usually more than the amount of code used for non-internal class cases, and the complexity is also increased, the biggest advantage of using an internal class is that you can easily access private attributes in an external class. The biggest disadvantage of using an internal class is that in a normal class operation, the class only defines attributes or methods. If an internal class is defined, the overall structure of the Code is broken.
In the above case, an internal class is also explained.
The following describes the internal class of the method.
Package anonymousinner; public class FunClass {private String info = "external class attribute value"; public static void main (String [] args) {// execute the test new FunClass (). print ();} public void print () {class Inner {// defines an internal class method with the parameter public void printInfo (String str) {System. out. println ("whose value is this:" + str); System. out. println ("whose value is this:" + info) ;}}; new Inner (). printInfo ("internal class parameter value ");}}
Use static to declare internal classes
When static is used to declare an internal class, the declared internal class is called an external class and can be called externally directly outside the class, if you want to access the attributes of an external class, you must have the static access permission. Otherwise, you cannot directly call the attributes of an external class.
Package anonymousinner. test; class A {private static String name = "XiaoMing"; static class B {public void said () {System. out. println (name + "") ;}} public class StaticInnerClass {public static void main (String [] args) {// execute the call new. B (). said ();}}
Once the internal class is modified with static, this internal class can be called by the outside, what is the call form? You can call the API in the following format:
External class. Internal class internal object = External class instance. new internal class ()
Anonymous internal class
In actual development, if a class is only used once in this program, it can be defined as an anonymous internal class. The anonymous internal class is developed on the Inheritance of abstract classes and interfaces, it often plays an important role in implementing multi-state operations. In Android learning, anonymous internal classes are very common programming forms, and many java frameworks are also used, we mainly learn to understand the syntax definition and usage of anonymous internal classes.
Package anonymousinner; // defines an interface IUSB {public void start ();} // defines a class to implement this interface class Conputer implements IUSB {public void start () {System. out. println ("USB started on computer");} public class Test {public static void main (String [] args) {// implement new Test () directly in the form of an interface (). print (new Conputer (); // implement new Test () anonymously (). print2 ();} // The first method is to directly call public void print (IUSB usb) {usb. start ();} public void print2 () {// write an anonymous internal class IUSB comput2 = new IUSB () {public void start () {System. out. println ("computer 2 USB Boot") ;}}; comput2.start ();}}
When using anonymous internal, if the computer class only needs to be used once, should we declare a class out of the box? The answer is no, so the benefits of anonymous internal classes are reflected in this.
The above is the description of the anonymous internal class! If you have any questions about the content written in the editor or are not correct, please leave a message for the editor. Thank you.