JAVA internal class usage, JAVA class usage

Source: Internet
Author: User

JAVA internal class usage, JAVA class usage

1. What is an internal class?

The definition of a class is placed inside another class. This class is called an internal class.

2. What are the features of internal classes?

1. The internal class is still an independent class. After compilation, the internal class will be compiled into an independent. class file, but it is preceded by the class name and $ symbol of the external class.
2. Internal classes cannot be accessed in normal mode. An internal class is a member of an external class, so the internal class can freely access the member variables of the external class, whether it is private or not.
3. When the internal class is declared static, you cannot access the member variables of the external class. In this case, the internal class can only access the static member variables of the external class.

3. What types of internal classes are there?

1. Member internal class

For example:
1 package com. test01; 2 3 public class A {4 // internal class B inherits TestPojo and implements the TestInterface interface 5 class B extends TestPojo implements TestInterface {6 // internal class B's own Method 7 public void run () {8 System. out. println ("I'm running! "); 9} 10 // rewrite the interface method 11 public void testf () {12 System. out. println (" implementation interface! "); 13} 14} 15 // call internal class 16 public void test () {17 B B = new B (); 18 B. testf (); // method 19 B. run (); // call your own Method 20 B. testpojo (); // call Method 21 that inherits the parent class} 22 // test the main method 23 public static void main (String [] args) {24 A a = new A (); 25. test (); 26} 27} 28 // defines an interface using testf () 29 interface TestInterface {30 public void testf (); 31} 32 // defines a common class method testpojo () 33 class TestPojo {34 public void testpojo () {35 System. out. println ("I am a simple pojo class "); 36} 37} 38 // call the method 39 class Textone {40 public static void main (String [] args) {41. B B = new (). new B (); // call the internal class B42/** in Class A, which is equivalent to the Code 43 * A a = new A (); 44 *. B B =. new B (); 45 **/46 B. testf (); // method 47 B. run (); // call your own method 48 B. testpojo (); // call method 49} 50} That inherits the parent class}

2. Internal method class

1 package com. test01; 2 3 public class PerTest {4 public void test () {// define a Method 5 class Ne {// define a method internal class 6 public void fle () {// define the method of the internal class of the Method 7 System. out. println ("I'm flying! "); 8} 9}; 10 new Ne (). fle (); // call the internal class method 11} 12 public static void main (String [] args) {13 new PerTest (). test (); // test 14} 15}

Note: (1) the internal class of a method can only be instantiated within the method that defines the internal class. It cannot be instantiated outside of this method.
(2) internal class objects of a method cannot use non-final local variables of the method of the internal class.

Because the local variables of a method are located on the stack, they only exist in the life cycle of the method. When a method ends, its stack structure is deleted,
Local variables become history. However, after the method ends, the internal class objects created in the method may still exist in the heap!
For example, if a reference to it is passed to some other code and stored in a member variable. Because local changes cannot be guaranteed
The duration of the volume is the same as that of the class objects in the method. Therefore, internal class objects cannot use them. (This understanding comes from Baidu encyclopedia)

3. Anonymous internal class

1) Abstract anonymous internal class

        

1 package com. anonymous; 2 3 public class AbstractClass {4 public void test () {// The method is test 5 TestA a = new TestA () {// implement abstract class 6 @ Override 7 public void run () {// implement the abstract class method 8 System. out. println ("I'm using an abstract anonymous internal class"); 9} 10}; 11. run (); // call the internal class method 12} 13 public static void main (String [] args) {14 new AbstractClass (). test (); // test 15} 16} 17 // define an abstract class TestA abstract method as run () 18 abstract class TestA {19 public abstract void run (); 20}

2) Anonymous internal class of the interface

1 package com. anonymous; 2 3 public class TestAnonymous {4 MyInterface m = new MyInterface () {// implement interface 5 public void eat () {// rewrite the MyInterface interface Interface Method 6 System. out. println ("I'm eating! "); 7} 8}; 9 public void ss () {// method ss10 m. eat (); // call the override method 11} 12 public static void main (String [] args) {13 new TestAnonymous (). ss (); // test 14} 15} 16 // define an interface method as eat17 interface MyInterface {18 public void eat (); 19}

Note: The anonymous internal class can be defined in the method or in the class member. No matter the anonymous internal class, it cannot be directly called by the external class.

4. What is the role of internal classes?

Each internal class can inherit from one (Interface) implementation independently, so no matter whether the peripheral class has inherited a (Interface) implementation, it has no impact on the internal class. If no
Internal classes provide the ability to inherit multiple specific or abstract classes, and some design and programming problems are difficult to solve. From this perspective, the internal class makes the multi-inheritance solution complete.
The interface solves some problems, while the internal class effectively implements "Multi-inheritance ".

 

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.