Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java inner classes     - Python implementation Bursa transition model (Programming)

- Confrontation dragged Library - Web front-end encryption slow (Linux)

- The basic principles of AIX system security (Linux)

- NGINX Plus now fully supports HTTP / 2 (Server)

- Docker startups use continuous integration deployment (Server)

- PostgreSQL using the system cache to improve operational efficiency (Database)

- OpenSSL for secure transmission and use of skills of files between Windows and Linux (Linux)

- PXE + Kickstart automatically install CentOS 6.5 (Linux)

- C ++ function object (Programming)

- Windows7 / 8 / 8.1 hard drive to install Ubuntu 14.04 dual system (Linux)

- Ubuntu How to install Pacman (Linux)

- MySQL function: group_concat () function (Database)

- LVM basic concepts, management (Linux)

- Daemon under Linux (Linux)

- How to install Hadoop on CentOS7 (Server)

- How to install CentOS CentOS web page (Server)

- Hadoop 1 and 2.x installation notes (Server)

- C ++ casts New Standard Comments (Programming)

- Servlet life cycle works (Programming)

- Php and MySQL command add to the environment variable method in Linux system (Linux)

 
         
  Java inner classes
     
  Add Date : 2018-11-21      
         
         
         
  (Modified static members belong to the entire class, not part of a single object)

Definition: The definition of a class into another class interior, this inside the class definition is called inner class (and some into nested classes), including inner classes classes are referred to as external class (also known as host some classes) .

1. Non-static inner classes

Not using a modified static inner classes is non-static inner classes.

Note: a. java static members are not permitted in non-static inner classes

b. When you call a non-static inner class instance methods, must have a non-static member inside class instance, non-static inner class instance must be externally parasitic instance within

c. If a local variable outside the class member variables with the internal class member variables and methods inside the class of the same name may be defined to be distinguished by this, external class as the class name .this.

public class Test {

    private String prop = "instance variables of the class";
    
    public class InClass {
        private String prop = "inner class instance variables";
        public void info () {
            String prop = "local";
            System.out.println ( "instance variables of the class values:" + Test.this.prop);
            System.out.println ( "inner class instance variables values:" + this.prop);
            System.out.println ( "local variables:" + prop);
        }
    }
    public void test () {
        InClass ic = new InClass ();
        ic.info ();
    }
    public static void main (String [] args) {
        / *
        * Test ts = new Test ();
        * Ts.test ();
        * Role as the code from top to bottom
        * /
        new Test () test ().;
    }
}

Use non-static inner classes:

Syntax defined elsewhere outside of the outer class inner class: OuterClass.InnerClass VarName

Place outside outside the class create instances of non-static inner class syntax: OuterInstace .new InnerConstructor ()

class Out {
    class In
    {
        public In (String msg) {
            System.out.println (msg);
        }
    }
}
public class CreateInnerInstance {
    public static void main (String [] args) {
        //OuterClass.InnerClass VarName = new OutInstance.new InnearConstructor ();
        Out.In in = new Out () new In ( "test message");
        /*Out.In In;
        * Out out = new Out ();
        * In2 = out.new In ( "test message");
 
* /
}
}

2. static inner classes

Static inner classes are modified with static inner classes, also known as class within the class.

a. Static inner classes can not access instance members of the outer class, class members can only be accessed outside the class (the same as the static modification).

c. External class still can not access a static internal class member, but you can use the class name static inner classes as the caller to access a static internal class class members, you can also use static inner class object to the caller to access static inner classes instance variables.

public class StaticInnerClassTest {
    private int prop = 5;
    private static int prop1 = 6;
    static class InClass {
        public void accessOuterProp () {
            
private static int prop = 5;
private int prop1 = 6;
//System.out.println(prop); // This code to be wrong, static inner class can not access non-static members of the outer class
            
System.out.println (prop1);
        }
    }
    public void accessInnerPro () {
// Static inner classes of the class name to access static inner class class members
    
 
System.out.println (InClass.prop);
// As the caller to access the static inner class instance variables static inner class object
SYstem.out.println (new InClass () prop1.);
    }
}

Use outside of the outer class uses a static inner classes :( and use non-static comparison, similar)
Syntax: new OuterClass.InnerConstructor ()

class Out {
    static class In
    {
        public In () {
            System.out.println ( "static inner class constructor");
        }
    }
}
public class CreateStaticInnerInstance {
    public static void main (String [] args) {
        Out.In in = new Out.In ();
    }
}

3. Anonymous inner classes
If a method is defined inside the class into, then this class is inside a local inner class, local inner class is valid only in the method.

Defining anonymous inner class format:

new implements the interface | parent class constructor (argument list)
{
  Class body part
}

As can be seen, anonymous inner class must inherit from a parent class or implement an interface.

interface Product {
    public Double getPrice ();
    public String getName ();
}
public class AnonymTest {
    
    public void ce (Product p) {
        System.out.println ( "bought a" + p.getName () + ", spent" + p.getPrice ());
    }
    public static void main (String [] args) {
        AnonymTest at = new AnonymTest ();
        at.ce (new Product () {
            @Override
            public Double getPrice () {
                return 0.5;
            }
            @Override
            public String getName () {
                return "hot bar";
            }
        });
    }
}
     
         
         
         
  More:      
 
- Analysis of C ++ exception mechanism (Programming)
- LVM management parameters commonly used commands explained in detail (Linux)
- View and modify Linux machine name (Linux)
- MySQL innodb_table_stats table does not exist solution (Database)
- Python cause yum upgrade error (Linux)
- After you change the GRUB boot disk partition repair (Linux)
- Reset CentOS / RHEL root account password 7 (Linux)
- Use OpenWrt build WDS wireless network extension on V2 WHR-G300N (Linux)
- Linux / BSD firewall M0n0wall Profile (Linux)
- Download Google Android source code under Ubuntu 4.4 (Linux)
- MongoDB 3.2 Cluster Setup (Database)
- Use Nginx as a load balancer (Server)
- CentOS 7 Test Marathon start Docker container (Server)
- Docker ecosystem security is gradually maturing (Server)
- ogg start being given libnnz11.so: can not open shared object file (Database)
- Php and MySQL command add to the environment variable method in Linux system (Linux)
- Use install_updates upgrade GAMIT / GLOBK (Linux)
- CentOS7 minimized installation can not find the 'ifconfig' command - Repair Tips (Linux)
- Python decorators to learn and practice the actual usage scenarios (Programming)
- FreeBSD install Gnome 3 Desktop (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.