zoukankan      html  css  js  c++  java
  • 为什么构造器不能是abstract, static, final, native or synchronized的?

    Unlike methods, a constructor cannot be abstract, static, final, native  or synchronized.

    1. A constructor is not inherited, so there is no need to declare it final

    2. As the same reason above, an abstract constructor could never be implemented.

    3. A constructor is always invoked with respect to an object, so it makes no sense for a constructor to be static.

    4. There is no practical need for a constructor to be synchronized, because it would lock the object under construction, which is normally not made available to other threads until all constructors for the object have completed their work.

    5. The lack of native constructors is an arbitrary language design choice that makes it easy for an implementation of the Java virtual machine to verify that superclass constructors are always properly invoked during object creation.

    翻译如下:

    不同于方法,构造器不能是abstract, static, final, native, strictfp, 或者synchronized的.

    1.构造器不是通过继承得到的,所以没有必要把它声明为final的。

    2.同理,一个抽象的构造器将永远不会被实现。(所以也不能声明为abstract的)

    3.构造器总是关联一个对象而被调用,所以把它声明为static是没有意义的。

    4.没有实际的需要把构造器定义成同步的,因为它将会在构造的时候锁住该对象,直到所有的构造器完成它们的工作,这个构造的过程对其它线程来说,通常是不可访问的。

    5.没有本地的构造器是任意一种语言的设计选择,这样会使得在创建对象的过程中JVM实现很容易去校验父类的构造器是否总是被正确地调用了。

    (本地化的方法情况特别复杂,所以JVM调用起来非常麻烦,需要考虑很多种情况,没有native关键字的情况下,JVM实现起来比较容易。)

    转自:http://www.blogjava.net/realsmy/archive/2007/04/10/109256.html

  • 相关阅读:
    asp.net字符串分割函数用法
    Nginx启动/重启脚本详解
    jQuery获取对象简单实现方法
    python字符串格式化之学习笔记
    asp.net中Winform开发框架之数据即时更新的实现
    Nginx错误提示:504 Gateway Time-out解决方法
    实用的php购物车程序
    sql datalength与len区别用法
    异步加载js文件的方法总结
    面向对象泛型问题
  • 原文地址:https://www.cnblogs.com/hdu-2010/p/4110106.html
Copyright © 2011-2022 走看看