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

What are the inner classes and common API in JavaSE

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail what are the internal classes and commonly used API in JavaSE. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1. Inner Class 1.1 Overview of Internal classes

Inner class: defines a class in a class. For example, if you define a class B within a class A, class B is called an inner class.

Format: public class class name {modifier class class name {}}

Example

Public class Outer {public class Inter {/ / Inner Class}}

Access characteristics of inner classes

1. Inner classes can directly access external class members, including private

two。 If you want to access the inner class, you have to create an object.

Give an example

Public class Outer {/ / create external class private int age=9; public class Inter {/ / create inner class / / inner class creation method public void show () {/ / inner class can directly access external class System.out.println (age) }} public void method () {/ / here is the call of the external class, so the show () method cannot be called directly as below, / / otherwise an error will be reported and / / show () cannot be accessed directly; this cannot be done / / instead, the inner class object should be created and Inter i=new Inter () should be accessed through the object; i.show () }} 1.2 member inner class

It can be divided into two forms according to the position of the inner class in the class.

In the member position of the class: the member inner class

In the local position of the class: the local inner class

/ / Class public class Outer {/ / create external class private int age=9; public class Inter {/ / create inner class / / Internal class creation method public void show () {/ / Internal class can directly access external class System.out.println (age);} / / Test class

So why not let it report it wrong? How to create it?

External class. Inner class object name = new external class .new inner class ()

Outer.Inter oi=new Outer () .new Inter ()

/ / Test class public class Main {public static void main (String [] args) {Outer.Inter oi=new Outer () .new Inter (); / / construct inner class object oi.show ();}}

However, in general, for the sake of security, the inner class will not be defined as public, but will be set to private, the above Outer.Inter oi=new Outer (). New Inter (); you can't construct the object of the inner class in this way, otherwise an error will be reported, so how to create it?

It's simple, because the external class can access the inner class by creating an object of the inner class, so you can write the methods of the external class in the test class for indirect access.

/ / Class public class Outer {/ / create external class private int age=9; private class Inter {/ / create internal private class / / Internal class creation method public void show () {/ / Internal class can directly access external class System.out.println (age);}} public void method () {Inter i=new Inter () / / create an inner class object to access i.show () indirectly; / / call method}}

So you can output the value of the inner class.

Running result

nine

1.3 Local inner class

A local inner class is a class defined in a method, so it is inaccessible to the outside world, so you need to create an object inside the method and use it to directly access members of the external class or local variables inside the method.

/ / Class public class Outer {/ / create external class private int age = 9; public void method () {/ / locally create inner class class Inter {/ / create inner class private int age2 = 2 age / local variable can also be public void show () {System.out.println (age); System.out.println (age2) }} Inter I = new Inter (); / / create an inner class object to access i.show () indirectly; / / call method}} public class Main {public static void main (String [] args) {Outer o=new Outer (); o.method (); / / indirect call method}} 1.4 anonymous inner class

Premise: there is a class or interface, where the class can be either concrete or abstract

Format: new class name or interface name () {override method}

Example: new Inter () {public void show () {}}

Explanation: new Inter () is a hidden object, inherits this class, or implements this interface

Essence: is an anonymous object of a subclass that inherits this class or implements the interface

An anonymous inner class is a form of a local inner class, which should also be written in the method.

/ / premise: a class or interface public interface Inter {/ / write interface void show (); / / write abstract method}

Then since it is an object, you can call the method through the object.

Public class Outer {/ / create an external class public void method () {new Inter () {/ / this is the object @ Override// rewriting method public void show () {System.out.println ("anonymous inner class");}. Show (); / / call methods through objects}}

This is the key point: new Inter () {/ / this is the object @ Override// rewriting method public void show () {System.out.println ("anonymous inner class");}}. Show (); / / the method is called through the object, where the object call method is adopted, and the object as a whole is the object, and then .show () calls the method.

/ / Test class public class Main {public static void main (String [] args) {Outer o=new Outer (); o.method (); / / call method}}

Next, there is a question, you will say, if I want to call the method many times, do I have to write this method many times? The answer is no, since it is an inner class, it can be done in a polymorphic form, that is, Inter i=new Inter () {}, and then call the show () method through I

Public class Outer {/ / create external class public void method () {Inter I = new Inter () {/ / polymorphic form, left interface name, right object @ Override// rewriting method public void show () {System.out.println ("anonymous inner class");}; i.show () / / follow compilation to the left and execute to the right to see i.show ();} 1.5 use of anonymous inner classes in development

After learning the anonymous inner class, what use does it have in development?

In development, if you want to implement an interface method, you do not need to create a class, such as the cat jump written earlier, and if you want to write a dog jump, you have to rebuild a class, which is too tedious and takes up space. since the inner class is essentially the object of the class, direct use will greatly reduce the inefficiency of the code.

/ / Interface public interface Jummping {void jump (); / / Abstract class} / / Operation class public class JumppingOperator {public void useOperator (Jummping j) {/ / interface name as formal parameter, new Jumpping () {} j.jump (); / / Internal class object call method} public class Jumpping {public static void main (String [] args) {JumppingOperator j = new JumppingOperator () / / create an operation class object j.useOperator (new Jummping () {@ Override public void jump () {System.out.println ("cat jumped high");}}) / / implement / / reuse with anonymous inner classes, there is no need to create Cat classes or Dog classes j.useOperator (new Jummping () {@ Override public void jump () {System.out.println);}});}} 2. Commonly used API2.1Math

Public final class Math extends Object is modified by final and is the final class. See APi manual for details.

You know from the help documentation that it has no constructor, so how do you use the members of the class?

At this point, you need to see whether the members are static. If so, you can call the

Common methods

Public class Main {public static void main (String [] args) {/ / absolute value System.out.println (Math.abs (- 88)); System.out.println (Math.abs (88)); System.out.println ("-") / / returns a System.out.println value greater than or equal to the parameter as an integer, (rounded up) System.out.println (1.12); System.out.println ("-") / / the integer value of the parameter whose value is less than or equal to 0 is an integer, (rounded down) System.out.println (Math.floor (1.21)); System.out.println ("-"); / / returns the maximum value System.out.println (Math.max (1focus 2)) System.out.println ("-"); / / returns the minimum value System.out.println (Math.min (2Magne6)); System.out.println ("-"); / / returns the b power System.out.println of a (Math.pow (2pyr2)) System.out.println ("-"); / / returns the rounded integer value System.out.println (Math.round (1.51)); System.out.println ("-") / / returns random values, [0-1.0) System.out.println (Math.random ()); / / returns 1-100 integer random values System.out.println ((int) (Math.random () * 100) + 1);}}

Running result

eighty-eight

eighty-eight

-

2.0

-

1.0

-

two

-

two

-

4.0

-

two

-

0.6041376042702374

seventy-eight

2.2 System

Cannot instantiate, cannot create objects, statically decorate

Common methods

Public class Main {public static void main (String [] args) {System.out.println ("start"); System.exit (0); / / the java virtual machine has stopped running and cannot execute the following statement System.out.println ("end");}}

Running result

Start

Public class Main {public static void main (String [] args) {System.out.println (System.currentTimeMillis () * 1.0Universe); System.out.println ("start calculating the time to execute the following program"); long start=System.currentTimeMillis (); / / defined by lang, because the number for (int I = 0; I) is too large

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

Development

Wechat

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

12
Report