Dark Horse programmer-object-oriented-internal class, dark horse object-oriented

Source: Internet
Author: User

Dark Horse programmer-object-oriented-internal class, dark horse object-oriented

------ Java training, Android training, iOS training, and. Net training. We look forward to communicating with you! -------

Internal class:

Internal class access rules:
1. The internal class can directly access members in the external class, including private
The reason for direct access to Members in the external class is that the internal class holds an external class reference in the format of: External class name. this
2. to access an internal class, an internal class object must be created.

Class outer {int num = 5; class inner {System. out. println ("num =" + num); // you can directly call an external member} void method () {inner in = new inner (); // create the internal Class Object show (); // call the internal class method }}
Access format:
1. When the internal class defines the member location of the external class and is not private, it can be in other external classes.
Internal class objects can be directly created
Format:
External class name. Internal class name variable name = External Class Object. Internal class object;
Outer. Inner in = new outer (). new Inner ();
2. When the internal class is in the member position, it can be modified by the member modifier.
For example: private: encapsulate internal classes in external classes
Static: Internal classes have static features.
After the internal class is modified as static, you can only directly access static members in the external class.
In other external classes, how do I directly access non-static members of static internal classes?
New outer. Inner (). function ();
In other external classes, how do I directly access static members of static internal classes?
Outer. Inner. function ();
Note: When a static member is defined in an internal class, the internal class must be static.
When the static method in the external class accesses the internal class, the internal class must also be static
When describing a thing, there is something inside it. This class is described by an internal class.
Because internal transactions are using external transaction content

Class Body {private class zhangcheng // internal class {} public void show () {new zhangcheng (); // an object must be created} class Outer {private static int x = 3; static class Inner {static void function () {System. out. println ("inner:" + x) ;}} static class Inner2 {void show () {System. out. println ("inner2 show") ;}} public static void method () {new Inner2 (). show () ;}} class InnerClassDemo2 {public static void main (String args []) {Outer. inner. function (); new Outer. inner (). function (); // directly access the Outer in the internal class. inner in = new Outer (). new Inner (); // create an internal Class Object in. function (); // call the internal class method }}
When defining an internal class:
1. It cannot be modified by the member Modifier
2. You can directly access members in the external class because they also hold references in the external class.
However, you cannot access the variables in the local region where the object is located. You can only access the local variables modified by final.

class Outer{int x = 3;void method(final int a){final int y = 4;class Inner{void function(){System.out.println(y);}}new Inner().function();}class InnerClassDemo{public static void main(String args[]){Outer out = new Outer();out.method(7);out.method(8);}}
Anonymous internal class:
1. The anonymous internal class is in short format of the internal class.
2. Prerequisites for anonymous internal classes:
An internal class must inherit a class or implement an interface.
3. Formats of anonymous internal classes:
New parent class or interface () {defines the content of the subclass}
4. In fact, the anonymous internal class is an anonymous subclass object, which is a bit fat and can be understood as an object with content.
5. It is recommended that no more than three methods are defined in the anonymous internal class.

Abstract class AbsDemo {abstract void show ();} class outer {int x = 3; class Inner extends AbsDemo {int num = 90; void show () {System. out. println ('show: "+ num);} void abc () {System. out. println ("abc") ;}} public void function () {AbsDemo d = new AbsDemo () {int num = 9; void show () {System. out. println ("num =" + num);} void abc () {System. out. println ("abcccc") ;}; // pay attention to this semicolon d. show (); // OK, implemented interface d. abc (); // error, custom method, which does not implement an interface or inherit a class. abstract classes cannot inherit, only implement} class InnerclassDemo {public static void main (String args []) {new Outer (). function () ;}} interface Inter {void method ();} class test {static class Inner implements Inter {public void method () {System. out. println ("method run") ;}} static Inter function () {return new Inter () {public void method () {System. out. println ("method run") ;}}}} class Innerclasstest {public static void main (String args []) {// test. function (): There is a static method function in the test class // method (): the solution after the function operation is an object, and it is an Inner type object // because only an Inter type object can call the method test. function (). method (); inter in = test. function (); in. method (); show (new Inter () {public void method () {System. out. println ("method show run") ;}}) ;}public static void show (Inter in) {in. method () ;}} class Innertest {public static void main (String args []) {new object () {public static function (){}}. function ();}}





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.