Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the member inner class in Java inner class

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

It is believed that many inexperienced people have no idea about how to understand the member inner class in the Java inner class. therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

In Java, a class can be defined in another class or in a method, and such a class is called an inner class. Generally speaking, internal classes in a broad sense include these four types: member inner class, local inner class, anonymous inner class and static inner class. Let's take a look at the usage of these four inner classes.

Member inner class

The member inner class is the most common inner class, which is defined as inside another class and looks like the following:

Class Circle {double radius = 0; public Circle (double radius) {this.radius = radius;} class Draw {/ / inner class public void drawSahpe () {System.out.println ("drawshape");}

In this way, the class Draw looks like a member of the class Circle, and Circle is called the outer class. The member inner class has unconditional access to all member properties and member methods of the external class (including private members and static members).

Class Circle {private double radius = 0; public static int count = 1; public Circle (double radius) {this.radius = radius;} class Draw {/ / Inner class public void drawSahpe () {System.out.println (radius); / / private member System.out.println (count) of external class; / / static member of external class}

Note, however, that when a member's inner class has a member variable or method with the same name as the outer class, a hidden phenomenon occurs, that is, members of the member's inner class are accessed by default. If you want to access a member of an external class with the same name, you need to access it in the following form:

one

two

External class. This. Member variable

External class. This. Member method

Although the member inner class can unconditionally access the members of the external class, the external class wants to access the members of the member inner class is not so arbitrary. If you want to access a member of a member's inner class in an external class, you must first create an object of the member's inner class, and then access it through a reference to that object:

Class Circle {private double radius = 0; public Circle (double radius) {this.radius = radius; getDrawInstance () .drawSahpe (); / / you must first create an object of the member's inner class before accessing} private Draw getDrawInstance () {return new Draw () } class Draw {/ / Inner class public void drawSahpe () {System.out.println (radius); / / private member of the external class}

The general way to create a member inner class object is as follows:

Public class Test {public static void main (String [] args) {/ / first way: Outter outter = new Outter (); Outter.Inner inner = outter.new Inner (); / / must be created through Outter object / / second way: Outter.Inner inner1 = outter.getInnerInstance ();}} class Outter {private Inner inner = null Public Outter () {} public Inner getInnerInstance () {if (inner = = null) inner = new Inner (); return inner;} class Inner {public Inner () {}

Inner classes can have private access, protected access, public access, and package access. For example, in the above example, if the member inner class Inner is decorated with private, it can only be accessed inside the external class. If it is decorated with public, it can be accessed anywhere; if it is decorated with protected, it can only be accessed under the same package or if it inherits the external class; if it is the default access permission, it can only be accessed under the same package. This is a little different from external classes, which can only be decorated with public and package access permissions. Personally, I understand that because the inner class of a member looks like a member of an external class, it can have a variety of permission modifications like members of the class.

After reading the above, have you mastered how to understand the inner class of the members in the Java inner class? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report