zoukankan      html  css  js  c++  java
  • 类名.class 与 类名.this 详解

    类名.class 与 类名.this 详解

    今天在看 PropertyPlaceholderConfigurer 源码时,突然看到一个 PropertyPlaceholderConfigurer.this.resolvePlaceholder 的用法,以前只知道有个 "类名.class" 的用法,今天才知道还有个 "类名.this"

    类名.class

    我们知道在 java 中,一个类在被加载的时候虚拟机就会自动的生成一个这个类的一个 Class 类型的 "类对象",每个类都对应着一个这样的类对象,通过这个 Class 类型的类对象,我们就能够使用 "内省与反射" 机制,访问一个类的信息,比如:对应类中的方法有哪些,成员域有哪些等等;获取一个类的 "类对象" 的方法之一就是通过使用 "类名.class" 这个方式返回一个 Class 类型的对象,其他的获取这个 Class 对象的方法如下:

    1. 利用对象调用 getClass() 方法获取该对象的 Class 实例

    2. 使用 Class 的静态方法 forName(),用类的名字获取一个 Class 实例

    3. 运用 "类名.class" 的方式获取 Class 实例,对基本数据类型的封装类,还可以采用 "类名.type" 来获取对应的基本数据类型的 Class 实例。

    这个应该都比较熟了,就不过多介绍了,下面着重介绍一下 "类名.this"

    类名.this

    这个语法的应用主要有两个方面:

    (1) 当在一个类的内部类中,如果需要访问外部类的方法或者成员域的时候,如果使用 "this.成员域" 调用的显然是内部类的域 ,如果我们想要访问外部类的域的时候,就要必须使用 "外部类.this.成员域"

    public class PropertyPlaceholderConfigurer extends PlaceholderConfigurerSupport {
    
        protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) {
            String propVal = null;
            if (systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) {
                propVal = resolveSystemProperty(placeholder);
            }
            if (propVal == null) {
                propVal = resolvePlaceholder(placeholder, props);
            }
            if (propVal == null && systemPropertiesMode == SYSTEM_PROPERTIES_MODE_FALLBACK) {
                propVal = resolveSystemProperty(placeholder);
            }
            return propVal;
        }
    
        private class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver {
    
            private final Properties props;
    
            private PropertyPlaceholderConfigurerResolver(Properties props) {
                this.props = props;
            }
    
            @Override
            public String resolvePlaceholder(String placeholderName) {
                return PropertyPlaceholderConfigurer.this.resolvePlaceholder(placeholderName, props, systemPropertiesMode);
            }
        }
    
    }
    

    内部类 PropertyPlaceholderConfigurerResolver 在 resolvePlaceholder 方法中调用外部类的方法为 PropertyPlaceholderConfigurer.this.resolvePlaceholder

    (2) 还有一个使用情况,那就是在是使用意图更加的清楚

    public class MainActivity extends Activity {
    
        @Override   
        protected void onCreate(Bundle savedInstanceState) {    
            super.onCreate(savedInstanceState);    
            setContentView(R.layout.activity_main);   
    
            Intent intent = new Intent(MainActivity.this , OtherActivity.class) ;  
        }
    }
    
  • 相关阅读:
    查看系统运行时间和系统当前时间
    根据端口查进程信息
    SELinux深入理解
    nginx配置文件
    centos7 nginx配置httpsCenos(6.6/7.1)下从源码安装Python+Django+uwsgi+nginx环境部署(二)
    Linux的加密认证功能以及openssl详解
    linux中shell变量$#,$@,$0,$1,$2的含义解释
    理解GRUB2工作原理及配置选项与方法
    在用busybox制作系统过程中遇到的问题
    内核编译选配(VMware篇)
  • 原文地址:https://www.cnblogs.com/binarylei/p/8433282.html
Copyright © 2011-2022 走看看