Java create internal class

1. Concept of internal class

Defining a class inside another class or inside a method is called an inner class; Inner classes are also an embodiment of encapsulation.

(1) The class defined outside the class name <> curly bracket cannot be called an internal class even in a file;

// There can be multiple classes in the same Java source file // But this approach cannot be called an inner class // At most one is modified by public public class Test < >class B

Just as Test and B are two independent classes, they have nothing to do with each other before;
(2) The internal class and external class share the same java source file, but after compilation, the internal class will form its own separate bytecode file;

Just like class A and class B in the definition, after compilation, two bytecode files will be generated.

As follows:

2. Classification and examples of internal classes

According to the different definition positions of internal classes, internal classes can be divided into member internal classes, local internal classes and anonymous internal classes, and member internal classes can be divided into ordinary internal classes and static internal classes according to whether they are modified by static or not;

2.1 member internal class

2.1.1 general internal class

Member inner classes that are not modified by static are called ordinary inner classes;

// Test common inner classes public class TestInnerClass < //Member variables of external class: a, b int a; int b; public void methodA()< //methodB() ; Compilation failure reason: external class cannot directly access internal class members //External class to access internal class members, //1) First create an internal class object; 2) Access internal members through this object InnerClass ic=new InnerClass(); ic.methodB(); >//Member inner class: normal inner class class InnerClass < //Member variables of the inner class: B, this & 0 (added by the compiler), which will point to the outer class object in the future int b; void methodB()< a=10; methodA(); b=20; //Assign values to the internal class's own member variables (proximity principle) // When the internal class member variable has the same name as the external class member variable, //How to assign a value to an external b? TestInnerClass.this.b=200; >> public static void main(String[] args) < // How to use ordinary inner classes //First create an object ti and access internal members through the object TestInnerClass ti=new TestInnerClass(); ti.methodA(); // InnerClass ic=new InnerClass(); compilation failed // An object of an inner class can only be created by using an object of an outer class InnerClass ic=ti.new InnerClass(); ic.methodB(); >>

When the break point is running, you can see the following interface. There are two created objects ti and ic. In the external class of the program, we define two member variables A and b. in the internal class, we only define one b, but we see two. The more this&0 is the object compiled and automatically added to us to point to the external class (that is, the compiler will maintain a reference to an external class object for the internal class) (it may be a little windy)

Читайте также:  Php soap server handle

The following can be obtained from the above:
(1) Any member in the external class can be accessed directly in the ordinary internal class method;

(2) The members of the ordinary inner class are located in the same location as the members of the outer class, so they are also constrained by access qualifiers such as public and private;

(3) When accessing a member with the same name in an internal class method, you have priority to access your own. If you want to access a member with the same name in an external class, you need to use:

External class name.this.Member name with the same name

(4) Ordinary internal class objects can only be created on the premise of having external class objects;

(5) The members in the internal class cannot be accessed directly in the external class. If you want to access, you must first create the object of the internal class;

2.1.2 static internal class

Internal member classes modified by static are called static internal classes;

//Testing static inner classes public class OutClass < private int a; //Static member variable b static int b; public void methodA()< a=10; System.out.println(a); >public static void methodB() < System.out.println(b); >// InnerClass is a static inner class static class InnerClass < public void methodInner()< // Only static member variables and methods of the external class can be accessed in the internal class // a=100; compilation failed because a is not a static member variable in an external class // methodA(); / / compilation failed. Reason: methodA() is not a static class member method b=100; methodB(); >> public static void main(String[] args) < //Static member variable access: class name. Member variable name System.out.println(OutClass.b); // Creation of static internal class objects //No external class objects are required InnerClass ic=new InnerClass(); // Static member method access innerClass.methodInner(); >>

(1) In the internal class, you can only access static members in the external class (including static member variables and static member methods);

Читайте также:  Php time to iso 8601

(2) When creating an internal class object, you do not need to create an external class object first;

(3) The internal class of the member will generate an independent bytecode file after compilation. The naming format is: external class name $internal class name;

2.2 local internal class (not commonly used)

Defined in the method body or code block <> of the external class, the internal class can only be used in its defined location, and cannot be used in other locations;

//Test local inner classes public class OutClass < int a=20; public void method()< int b=10; // InnerClass is a local inner class // It is defined in the (method) method body of the external class (OutClass) class InnerClass< //static int c; compilation failed. Static members cannot be defined in local inner classes public void methodInnerClass()< System.out.println(a); System.out.println(b); >> // It can only be used inside the method body, not elsewhere InnerClass innerClass=new InnerClass(); innerClass.methodInnerClass(); > public static void main(String[] args) < // OutClass.InnerClass.ic=null; // Compilation failed because a local inner class was used externally >>

Local internal classes cannot be modified by public, static and other access qualifiers. If a modifier is added, an error will be reported. After the cursor is clicked, a prompt message will be displayed as follows:

As can be seen from the above:

(1) Local inner classes can only be used inside the defined method body;
(2) Cannot be modified by public, static and other modifiers;
(3) Static members cannot be defined in local inner classes;

2.3 anonymous inner class

Anonymous inner class belongs to local inner class. The difference is that it is a local inner class without name. It is usually used with anonymous objects (the interface part introduces the use of this class in detail);

Читайте также:  Nested data classes kotlin

2.4 object printing (add one)

For private member variables, we need to provide access methods of Set and Get. When there are many private members, IDEA can be used to generate them directly. The generation method is alt+insert+Set or alt+insert+Get;

(1) Basic type print: the result is the set value;
(2) Reference type printing: prints the address equivalent to the object, not the object itself;

Example: define a Person class

public class Person < String name; private String gender; private int age; public Person(String name, String gender, int age) < this.name = name; this.gender = gender; this.age = age; >public static void main(String[] args) < //Basic type int a=10; System.out.println(a); //reference type Person p=new Person("Han Lei","male",13); System.out.println(p); >>
10 day20210913.Person@1b6d3586

What should I do if I want to print the contents of the object?

Method: override toString()
Shortcut key: (alt+insert+toString)

After using toString(), run again. The results are as follows:

Welcome criticism and correction!

Added by poisedforflight on Sat, 18 Sep 2021 20:35:38 +0300

  • Java — 6234
  • Python — 2579
  • Javascript — 2100
  • Database — 1608
  • Linux — 1477
  • Back-end — 1449
  • Front-end — 1432
  • Spring — 1358
  • Algorithm — 1311
  • Android — 1124
  • MySQL — 1040
  • C++ — 1022
  • Programming — 966
  • network — 827
  • data structure — 820
  • Attribute — 785
  • C — 721
  • github — 646
  • less — 645
  • SQL — 639

Источник

Оцените статью