final关键字讲解

1.修饰类

     当用final修饰一个类时,表明这个类不能被继承。也就是说,如果一个类你永远不会让他被继承,就可以用final进行修饰。
     final类中的成员变量可以根据需要设为final,但是要注意final类中的所有成员方法都会被隐式地指定为final方法

2.修饰方法

  “使用final方法的原因有两个。第一个原因是把方法锁定,以防任何继承类修改它的含义;第二个原因是效率。在早期的Java实现版本中,会将final方法转为内嵌调用。但是如果方法过于庞大,可能看不到内嵌调用带来的任何性能提升。在最近的Java版本中,不需要使用final方法进行这些优化了。“代表这个方法不可以被子类的方法重写如果你认为一个方法的功能已经足够完整了,子类中不需要改变的话,你可以声明此方法为final。final方法比非final方法要快,因为在编译的时候已经静态绑定了,不需要在运行时再动态绑定。

注: 类的private方法会隐式地被指定为final方法。

3.修饰变量
     对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改;如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象。
  1. final成员变量必须在声明的时候初始化或者在构造器中初始化,否则就会报编译错误。

4.关于final的重要知识点

  1. final关键字可以用于成员变量、本地变量、方法以及类。
  2. final成员变量必须在声明的时候初始化或者在构造器中初始化,否则就会报编译错误。
  3. 你不能够对final变量再次赋值。
  4. 本地变量必须在声明时赋值。
  5. 在匿名类中所有变量都必须是final变量。
  6. final方法不能被重写。
  7. final类不能被继承。
  8. final关键字不同于finally关键字,后者用于异常处理。
  9. final关键字容易与finalize()方法搞混,后者是在Object类中定义的方法,是在垃圾回收之前被JVM调用的方法。
  10. 接口中声明的所有变量本身是final的。
  11. final和abstract这两个关键字是反相关的,final类就不可能是abstract的。
  12. final方法在编译阶段绑定,称为静态绑定(static binding)。
  13. 没有在声明时初始化final变量的称为空白final变量(blank final variable),它们必须在构造器中初始化,或者调用this()初始化。不这么做的话,编译器会报错“final变量(变量名)需要进行初始化”。
  14. 将类、方法、变量声明为final能够提高性能,这样JVM就有机会进行估计,然后优化。

5.代码讲解

/**
 * Utils - 系统设置
 */
@SuppressWarnings("unchecked")
public final class SettingUtils
{

    public static final Logger logger = LoggerFactory.getLogger(SettingUtils.class);

    /** CacheManager */
    private static final CacheManager cacheManager = CacheManager.create();

    
    /**
     * 不可实例化
     */
    private SettingUtils()
    {
    }

    /**
     * 获取系统设置
     * 
     * @return 系统设置
     */
    public static Setting get()
    {
        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        net.sf.ehcache.Element cacheElement = cache.get(Setting.CACHE_KEY);
        Setting setting;
        if (cacheElement != null)
        {
            setting = (Setting) cacheElement.getObjectValue();
        }
        else
        {
            setting = new Setting();
            try
            {
                File preschoolEduXmlFile = new ClassPathResource(CommonAttributes.PRESCHOOLEDU_XML_PATH).getFile();
                Document document = new SAXReader().read(preschoolEduXmlFile);
                List
    
    
     
      elements = document.selectNodes("/preschoolEdu/setting");
                for (Element element : elements)
                {
                    String name = element.attributeValue("name");
                    String value = element.attributeValue("value");
                    try
                    {
                        beanUtils.setProperty(setting, name, value);
                    }
                    catch (IllegalAccessException e)
                    {
                        e.printStackTrace();
                    }
                    catch (InvocationTargetException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
            cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
            if("".equals(setting.getEaseAppKey()))
            {
            	logger.error("The easeAppKey is null");
            }
        }
        return setting;
    }

    /**
     * 设置系统设置
     * 
     * @param setting 系统设置
     */
    public static void set(Setting setting)
    {
        try
        {
            File preschoolEduXmlFile = new ClassPathResource(CommonAttributes.PRESCHOOLEDU_XML_PATH).getFile();
            Document document = new SAXReader().read(preschoolEduXmlFile);
            List
     
     
      
       elements = document.selectNodes("/preschoolEdu/setting");
            for (Element element : elements)
            {
                try
                {
                    String name = element.attributeValue("name");
                    String value = beanUtils.getProperty(setting, name);
                    Attribute attribute = element.attribute("value");
                    attribute.setValue(value);
                }
                catch (IllegalAccessException e)
                {
                    e.printStackTrace();
                    logger.error(e.getMessage());
                }
                catch (InvocationTargetException e)
                {
                    e.printStackTrace();
                    logger.error(e.getMessage());
                }
                catch (NoSuchMethodException e)
                {
                    e.printStackTrace();
                    logger.error(e.getMessage());
                }
            }

            FileOutputStream fileOutputStream = null;
            XMLWriter xmlWriter = null;
            try
            {
                OutputFormat outputFormat = OutputFormat.createPrettyPrint();
                outputFormat.setEncoding("UTF-8");
                outputFormat.setIndent(true);
                outputFormat.setIndent("	");
                outputFormat.setNewlines(true);
                fileOutputStream = new FileOutputStream(preschoolEduXmlFile);
                xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
                xmlWriter.write(document);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                if (xmlWriter != null)
                {
                    try
                    {
                        xmlWriter.close();
                    }
                    catch (IOException e)
                    {
                    }
                }
                IOUtils.closeQuietly(fileOutputStream);
            }

            Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
            cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
        }
        catch (Exception e)
        {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
    }

}

     
     
    
    

  • @SuppressWarnings("unchecked")

在java编译过程中会出现很多警告,有很多是安全的,但是每次编译有很多警告影响我们对error的过滤和修改,我们可以在代码中加上

@SuppressWarnings(“XXXX”) 来解决

例如:@SuppressWarnings("deprecation")表示不显示使用了不赞成使用的类或方法时的警告

  • SettingUtils 被定义为final类 表示该类是不可以被继承的。get set方法被隐式定义为final( 不可被再修改)
可以通过SettingUtils.get()方法获取系统设置。


6.Ehcache

 查看 : chakanhttp://blog.csdn.net/qq_21989939/article/details/47952323

7.  ClassPathResource

File preschoolEduXmlFile = new ClassPathResource(“/preschoolEdu.properties”).getFile();
ClassPathResource类,如果没有指定相对的类名,该类将从类的根路径开始寻找某个resource,如果指定了相对的类名,则根据指定类的相对路径来查找某个resource。

SAX解析xml 查看:http://blog.csdn.net/qq_21989939/article/details/48105827


参考:
http://www.importnew.com/7553.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值