Java BASICS (1)-internal class, rewriting and overloading

Source: Internet
Author: User

1. Rewrite <override> and reload <overloading>

Rewriting is a manifestation of Java polymorphism relative to the parent class and the overload is relative to the current class.

2. Java internal class

At the beginning, there is no need to elaborate on every function. The top-down list is as follows:

<1> internal class definition: as the name suggests, the class defined inside the class is an internal class.

<2> simple examples of internal classes:

public class OutClass(){  public class InsideClass()  {
    ...  }}

<3> use of internal classes: (using an internal class to access external class member variables)

Public class Test2 {
Private int num; Public Test2 () {} public Test2 (INT num) {This. num = num;} private class inner {public Test2 gettest2 () {return test2.this;} public Test2 newtest2 () {return New Test2 ();}} public static void main (string [] ARGs) {Test2 test = new Test2 (5); test2.inner inner = test. new inner (); Test2 Test2 = inner. gettest2 (); Test2 test3 = inner. newtest2 (); system. out. println (test2.num); system. out. println (test3.num );}
}
Output result: 5 0
Explanation: As long as an internal class object is created through an external class, the internal class is associated with the external class, and the external class member variables and methods can be accessed without restrictions.
Use the following method: External class. This;
Principle: When an external class creates an internal class object, the internal class will secretly capture a reference pointing to the external class. The specific mechanism is a compiler problem and it is not necessary to go into details at the beginning.

<4> encapsulation and hiding of internal classes:

Public class example {private class insideclass implements interfacetest {public void test () {system. out. println ("this is test") ;}} public interfacetest getin () {return New insideclass ();}}
// Test the public class testexample {public static void main (string ARGs []) {Example A = new example (); interfacetest a1 =. getin (); a1.test ();}}
Explanation: Obviously, the interfacetest () method of the example class returns an insideclass object, and the insideclass is private. If we do not take the code seriously
We cannot see the specific implementation of insideclass. This backup seems to be relatively high-end.

OK, that's it!

 

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.