JAVA base class

Source: Internet
Author: User

JAVA base class
JAVA Basics

2017-01-13

1. What are internal classes in java ?!

Member internal class

Local internal class

Anonymous internal class

Static internal class

2. Explanation of internal classes• Member internal class

Create another class within a class to become an internal class.

      1) use an internal class in a class to directly access the private attributes or methods of the class.

1 class Outer {2 // external class Outer 3 private int I = 111; // external class private variable 4 class Inner {5 // internal class Inner 6 private int j = 222; // internal class private variable 7 public void print () {8 System. out. println ("Private attributes of external classes" + I); 9 System. out. println ("Private attributes of internal classes" + j); 10} 11} 12} 13 public class ClassDemo5 {14 public static void main (String [] args) {15 Outer o = new Outer (); 16 Outer. inner I = o. new Inner (); 17 I. print (); 18} 19}

      2) instantiation of internal classes

Import object-oriented basics. Outer. Inner;
// Method 1: import the import object-oriented basic. Outer. Inner; Package
Outer. Inner I = o. new Inner (); // Method 2: External class. Internal Class Object Name = External Class Object. new internal class

3) External class access internal class

1 class Fatch {2 // external class Fatch 3 public class Student {4 // internal class Student 5 public void show () {6 // internal class show () method 7 System. out. println ("show () method of internal class Student"); 8} 9} 10 public void print () {11/** 12 * external classes cannot directly access internal class attributes or methods. 13 * You must create an internal class object to access 14 */15 new Student (). show (); // use an anonymous object to instantiate the class member of the internal class 16 System. out. println ("external Fatch print () method"); 17} 18} 19 public class ClassDemo4 {20 public static void main (String [] args) {21 Fatch f = new Fatch (); // instantiate an external Class Object 22 f. print (); // call the print () method of the external Class 23} 24}

      4) if the external class has the same member variables or methods as the internal class, the internal class accesses its own member variables or methods by default. If you want to access the member variables of the external class, you can use the this keyword.

1 class Outer1 {2 // external class 3 private int I = 10; // external class private variable 4 class Inner1 {5 // internal class 6 private int I = 20; // internal class private variable 7 public void show () {8 // internal class method 9 System. out. println ("internal class variable" + this. i); // use the this keyword to access the internal class member 10 System. out. println ("external class variable" + Outer1.this. i); // use the external class name. this. variable name to access external class member 11} 12} 13 public void print () {14 new Inner1 (). show (); // call internal class Method 15} 16} 17 public class ClassDemo6 {18 public static void main (String [] args) {19 new Outer1 (). print (); 20} 21 22}

   

• Note:

» Before instantiating an internal class, you must instantiate an external class.

» Internal classes can access their external class members at will, but external classes cannot directly access internal class members.

» Internal classes can be declared as private or protected access

» Internal class objects will depend on external class objects. Unless an external Class Object already exists, internal class objects will not appear in the class.

 

• Local internal class (method internal class)

Internal classes can be defined not only in the class, but also in the local position of the class. For example, internal classes can be defined in the class method or any scope.

1) define an internal class of a method in the external Class Method

1 class Demo1 {2 // external class Demo 3 final int I = 10; // constant defined by the external class 4 public void show () {// show () of the external class () method 5 class Inne {// internal class of the Method 6 int j = 20; // internal class variable of the Method 7 private void inshow () {// inshow () in the internal class of the Method () method 8 System. out. println ("external class constant" + I); 9 System. out. println ("internal class variable of the method" + j); 10} 11} 12 Inne in = new Inne (); // instantiate the internal class object 13 in. inshow (); // call internal class method 14} 15} 16 public class ClassDemo7 {17 public static void main (String [] args) {18 Demo1 d = new Demo1 (); 19 d. show (); 20} 21}

Note: If the parameters in the method need to be passed in, the parameter must be declared as final type.

 

• Static internal class

If an internal class is declared as static, this internal class is called an external class.

1) static internal classes can call static internal class methods through (new external class name. Internal class. Internal class method name)

1 class Outer2 {2 // external class 3 private static String str = "Zhang bojie "; // static variable 4 static class Inner2 {5 // static internal class 6 public void fun () {7 // static internal class method 8 System. out. println ("external class static variables" + "" + str); 9} 10} 11} 12 public class ClassDemo8 {13 public static void main (String [] args) {14 new Outer2.Inner2 (). fun (); 15 // The static internal class can use the new external class name. internal class. internal class method name to call static internal class Method 16} 17}

 

• Anonymous internal class

The anonymous internal class is an internal class without a name. Because there is no name, the anonymous internal class can only be used once. It is usually used to simplify code writing. there are also prerequisites for using an anonymous internal class: A parent class must be inherited or an interface must be implemented.

1) Implement anonymous internal classes

1 abstract class A2 {// abstract class A2 2 public abstract void eat (); 3} 4 public class ClassDemo9 {5 public static void main (String [] args) {6/** 7 * If the abstract class A2 is used only once, you can use an anonymous internal class to implement 8 */9 A2 a = new A2 () {10 public void eat () {11 System. out. println ("I want to eat"); 12} 13}; 14. eat (); 15} 16}

 

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.