多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
> 1. 可以将一个类定义在一个类的内部,这个类就是内部类 > 2. 当创建一个内部类的时候,它无形中就与外围类有了一种联系,依赖于这种联系,它可以无限制地访问外围类的元素。 > ~~~ public class OuterClass { private String name ; private int age; /**省略getter和setter方法**/ public class InnerClass{ public InnerClass(){ name = "chenssy"; age = 23; } public void display(){ System.out.println("name:" + getName() +" ;age:" + getAge()); } } public static void main(String[] args) { OuterClass outerClass = new OuterClass(); OuterClass.InnerClass innerClass = outerClass.new InnerClass(); innerClass.display(); } } ~~~ 输出 ~~~ name:chenssy ;age:23 ~~~ 结论: 1. 内部类持有外围类的引用 2. 所以内部类可以无限制的访问外部类的成员(即使是private修饰的) 3. 必须要利用外部类的对象通过.new来创建内部类 outerClass.new InnerClass(); ~~~ package cn.com.bigssl.crypto; import com.aexit.motordriver.commons.utils.TimingDBConnection; import java.io.FileInputStream; import java.io.InputStream; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.PrivateKey; import java.security.cert.X509Certificate; import java.util.Enumeration; /** * Created by hanxt on 16-10-21. */ public class TimingCertificate { private PrivateKey privateKey; private String SerialNumber; static class Nested{ static KeyStore keyStore = null; static String passwd = "1"; static { String osType = System.getProperty("os.name"); String path = System.getProperty("user.home") + "/application/jilinax.pfx"; try { keyStore = KeyStore.getInstance("PKCS12"); InputStream input = new FileInputStream(path); keyStore.load(input, passwd.toCharArray()); } catch (Exception e) { e.printStackTrace(); } } } public void readPfx() throws Exception { Enumeration<String> aliases = Nested.keyStore.aliases(); if (!aliases.hasMoreElements()) throw new RuntimeException("no alias found"); String alias = aliases.nextElement(); X509Certificate cert = (X509Certificate) Nested.keyStore.getCertificate(alias); SerialNumber = cert.getSerialNumber().toString();//序列号 SerialNumber = Long.toHexString(Long.parseLong(SerialNumber)).toUpperCase(); privateKey = (PrivateKey) Nested.keyStore.getKey(alias, Nested.passwd.toCharArray());//私钥 } public PrivateKey getPrivateKey() { return privateKey; } public String getSerialNumber() { return SerialNumber; } public static void main(String[] args){ System.out.println(System.getProperty("user.home")); } } ~~~ ### 静态内部类 ~~~ package JavaTest.SingletonTest; /** * Created by dailin on 2017/11/23. */ public class Outer { static { System.out.println("load outer class..."); } public static int printTest(){ System.out.println("printTest静态方法"); return 1; } //静态内部类 static class StaticInner { static { System.out.println("load static inner class..."); } static void staticInnerMethod() { System.out.println("static inner method..."); } } public static void main(String[] args) { Outer outer = new Outer(); //此刻其内部类是否也会被加载? Outer outer1 = new Outer(); //此刻其内部类是否也会被加载? System.out.println("===========分割线==========="); Outer.StaticInner.staticInnerMethod(); //调用内部类的静态方法 StaticInner.staticInnerMethod(); //调用内部类的静态方法 Outer.printTest(); } } ~~~ ~~~ load outer class... //虽然建立两个对象,但是static块只执行一次 ===========分割线=========== load static inner class... static inner method... static inner method... printTest静态方法 ~~~ 1. 加载一个类时,其内部类不会同时被加载。一个类被加载,当且仅当其某个静态成员(静态域、构造器、静态方法等)被调用时发生。 2. 静态代码块只执行一次