understanding constructors                  --How constructors differ from methods

By Robert Nielsen, JavaWorld.com, 10/13/00

[译] 马嘉楠       2007.04.20

key words:

constructor      构造函数         method        方法            instance          实例

object             对象              modifier         修饰符         return type      返回类型

static method   静态方法         superclass      超类           Inheritance      继承

platypus          鸭嘴兽

我们说一个构造函数是方法就好比说澳洲鸭嘴兽是另一个哺乳动物一样。 为了了解鸭嘴兽,知道其与其他的哺乳动物的差别对我们来说非常重要。同理,了解构造函数,知道构造函数与其他方法的区别对我们同样重要。对于任何学习Java,尤其是为了获得资格证书的学生来说,都需要知道这些区别。在这篇文章中,我将会一一道来。在文章结尾,Table1 总结了constructor(构造函数)和method(方法)的重要区别。

Purpose and Function (目的与功能)

构造函数都有一个目的:创建一个类的实例。也可以叫做创建一个对象,如下:

Platypus p1=newPlatypus();

相比之下,方法(method)的目的显得更为普通. 一个方法的基本功能就是为了执行Java的代码.

Signature differences(签名区别)(Signature 不知道怎么翻译好)

构造函数(constructors)和方法(methods)在以下三个方面存在差别:

·   修饰符   (modifiers)

·   返回类型(return type)

·   名字     (name)

像方法一样,构造函数可以有任意一种访问修饰符(access modifiers):公共的(public),保护的(protected),私有的(private)或者没有(常被称为package或者friendly)。不同于方法的是,构造函数只能被访问修饰符进行限定。因此,构造函数不能是abstract, static, final, natice or synchronized的。

两者的返回类型(return type)也是截然不同的。方法可以拥有任何有效的返回类型,或者方法没有返回值,这种情况下方法的返回类型为void。构造函数没有返回类型,也没有void。

最后,构造函数和方法在取名方面也有很大的不同。构造函数名字与其类名(class name)相同;按照惯例,方法使用其他与类名不相同的名字。如果Java程序员遵循通常惯例,方法名将会以小写字母开头,构造函数名字以大写字母开头。并且,因为与类名相同,构造函数名字通常是个名词,方法名字是个动词。

The use of "this" (this的使用)

构造函数和方法使用关键字 this 差别很大。

方法中的 this 指的是执行该方法的类的实例(instance)。静态方法(static method)不使用 this 。因为静态方法不属于任何一个类的实例,所以 this 无所指向。静态方法总体上属于一个类而非一个类的实例。

构造函数中的 this 指的是,在同一个类中拥有不同的参数列表的另一个构造函数。代码如下:

publicclassPlatypus {

String name;

Platypus(String input) {

name=input;

}

Platypus() {this("John/Mary Doe");

}publicstaticvoidmain(String args[]) {

Platypus p1=newPlatypus("digger");

Platypus p2=newPlatypus();

}

}

在代码中,有两个构造函数。

第一个构造函数通过一个String参数input给name进行赋值。

第二个构造函数没有参数,通过默认的名字"John/Mary Doe"来调用第一个构造函数。

如果在构造函数中使用 this,则必须在构造函数的第一行代码当中,否则编译器会报错。

注:在我这里的报错信息为 Constructor call must be the first statement in a constructor。

The use of "super"(super的使用)

方法和构造函数使用 super 的时候,指的都是超类(superclass),但也有所不同。

方法中使用 super 将会执行超类中被覆盖的方法,如下所示:

classMammal {voidgetBirthInfo() {

System.out.println("born alive.");

}

}classPlatypusextendsMammal {voidgetBirthInfo() {

System.out.println("hatch from eggs");

System.out.print("a mammal normally is");super.getBirthInfo();

}

}

在上面代码中,super.getBirthInfo() 将会调用超类 Mammal 中被覆盖的方法 getBirthInfo().

构造函数中使用 super 将会调用超类中的构造函数。

如果在构造函数中使用 super,则必须在构造函数的第一行代码当中,否则编译器会报错。

注:在我这里的报错信息为 Constructor call must be the first statement in a constructor。

代码如下:

publicclassSuperClassDemo {

SuperClassDemo() {}

}classChildextendsSuperClassDemo {

Child() {super();

}

}

Complier -- supplied code(编译器提供的代码)

当编译器自动为构造函数提供代码的时候,Java初学者可能会感到困惑。如果你的类中没有构造函数,编译器将会为你自动提供一个没有参数的构造函数。如果你的代码如下:

publicclassExample {}

功能上它等同于如下代码:

publicclassExample {

Example() {}

}

如果在你的构造函数的第一行代码当中没有使用 super,那么编译器会自动为你提供代码,插入 super。

如果你的代码如下:

publicclassTestConstructors {

TestConstructors() {}

}

功能上它等同于如下代码:

publicclassTestConstructors {

TestConstructors() {super();}

}

初学者可能会有疑问:TestConstructors 并没有继承任何类,为什么它会调用父类的构造函数呢?

答案是:在 Java 中如果没有显示的继承一个类,则默认为继承自 Object 类。

如果没有显示的声明一个构造函数,编译器自动提供一个没有参数的构造函数;如果一个构造函数没有显示的 super 调用,编译器自动提供一个没有参数的 super 调用。所以下面的两段代码在功能上是等价的:

publicclassExample {}

publicclassExample {

Example() {super();

}

}

Inheritance(继承)

下面情况有什么不对?

律师阅读类A的遗嘱。所有家庭成员围坐在大会议桌旁,并且有些人在轻声呜咽。律师说到:“我,类A,头脑清楚身体健康,将我所有的构造函数留给我的孩子”。

问题是构造函数不是通过继承得到的。然而幸运的是,子类可以自动的继承父类所有的方法,所以子类并不是一无所有。

记住:Java 方法可以通过继承得到,而构造函数不行。看下面代码:

publicclassExample {publicvoidsayHi {

system.out.println("Hi");

}

Example() {}

}publicclassSubClassextendsExample {

}

类 SubClass 自动继承父类的 sayHi 方法。然而,构造函数 Example() 不会被类 SubClass 所继承。

Summarizing the differences

构造函数与方法的区别就像鸭嘴兽与典型的哺乳动物一样。尤其是在目的(purpose),签名(signature),和 this 与 super 的使用方面。另外,在继承和编译器提供代码方面也有很大差异。记住所有的区别可能会非常辛苦,所以下面提供的一个表格,简单的概括了重要的差异方面。Topic

Constructors

Methods

Purpose

Create an instance of a class

Group Java statements

Modifiers

Cannot be abstract, final, native, static, or synchronized

Can be abstract, final, native, static, or synchronized

Return type

No return type, not even void

void or a valid return type

Name

Same name as the class (first letter is capitalized by convention) -- usually a noun

Any name except the class. Method names begin with a lowercase letter by convention -- usually the name of an action

this

Refers to another constructor in the same class. If used, it must be the first line of the constructor

Refers to an instance of the owning class. Cannot be used by static methods

super

Calls the constructor of the parent class. If used, must be the first line of the constructor

Calls an overridden method in the parent class

Inheritance

Constructors are not inherited

Methods are inherited

Compiler automatically supplies a default constructor

If the class has no constructor, a no-argument constructor is automatically supplied

Does not apply

Compiler automatically supplies a default call to the superclass constructor

If the constructor makes no zero-or-more argument calls to super, a no-argument call to super is made

Does not apply

原文:http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html

马嘉楠

jianan.ma@gmail.com

posted on 2007-04-20 18:01 马嘉楠 阅读(1223) 评论(3)  编辑  收藏 所属分类: 外文翻译

java constructors_[译]understanding constructors相关推荐

  1. Java转译URL参数

    Java转译URL参数 //调用 String input = URIEncoderUtil.encodeURIComponent(String input); public class URIEnc ...

  2. 恶魔语言java_说地道的Java语言(译)

    说地道的Java语言 --使非Java程序员能流畅地使用Java程序设计语言 使用一种程序设计语言,就应该专业地使用它.本文是IBM developerWorks中的一篇文章,它描述的都是Java编程 ...

  3. go语言如何调用java接口,[译] 如何在 Go 中使用接口

    在开始使用 Go 编程之前,我的大部分工作都是用 Python 完成的.作为一名 Python 程序员,我发现学习使用 Go 中的接口是非常困难的.基础很简单,而且我知道如何在标准库中使用接口,但是我 ...

  4. php 转义md5 和java 转译的区别_CTF|PHP中的命令参数注入

    直奔主题,首先来了解两个 PHP 函数的定义. 命令参数注入 escapeshellarg 把字符串转码为可以在 shell 命令里使用的参数. escapeshellcmd 对字符串中可能会欺骗 s ...

  5. java easing_[译] 动画中缓动(easing)的基础知识

    自然界中没有东西是从一个点线性地移动到另一点. 在现实中,事物在运动时可能加速或减速. 我们的大脑习惯于期待这种运动,因此在做动画时,应利用此规律. 自然的运动会让用户对你的应用感觉更舒服,从而产生更 ...

  6. java 反序列化工具 marshalsec改造 加入dubbo-hessian2 exploit

    0x00 前言 1. 描述 官方github描述: Java Unmarshaller Security - Turning your data into code execution "将 ...

  7. 如何正确的创建和销毁Java对象

    作者 | RonTech 来源 | https://blog.csdn.net/zyhlwzy/article/details/78937421 一.介绍 Java由Sun Microsystems发 ...

  8. java核心教程_核心Java教程

    java核心教程 Welcome to Core Java Tutorial. I have written a lot on Core Java and Java EE frameworks. Th ...

  9. java多线程_Java多线程

    java多线程 Multithreading in Java is a very important topic. I have written a lot about Threads in Java ...

最新文章

  1. vmrun 批量创建vmware虚拟机
  2. CreateEvent的用法
  3. [湖南师大集训2018 7 26] hunger 解题报告 (SPFA)
  4. 【项目管理】合同和采购
  5. Polygon对象和Polyline对象的组成形式
  6. [转]Sublime Text 2 C++编译运行简单配置
  7. 关于docker的几个问题
  8. java匿名对象_面向对象
  9. linux系统资源管理系统,linux基础4系统资源管理
  10. 小虾教你网购组装电脑单
  11. 【农业物联网】智慧农业模型应用多案例分享
  12. “一见杨过误终生”,《神雕侠侣》2014年 超清1080P未删节版52集全
  13. cdr怎么把矩形去掉一个边_cdr画矩形怎么把中间弄掉
  14. android 支付宝微信原生以及HTML调用原生SDK
  15. C语言学习笔记—链表(四)链表的删除
  16. 关系型数据库保证数据完整性和一致性的方法
  17. 【ATT 与 Intel】汇编与C语言相互调用及内联汇编
  18. 树上差分的整理(点的树上差分和边的树上差分)
  19. EPB电子驻车制动系统Simulink模型 模型包括:有刷直流电机+执行器模型,电机参数m文件,SSM模块,PBC模块,数据处理模块,与Carsim联防进行过验证
  20. 【数集项目之 MCDF】(三) 仲裁器 arbiter

热门文章

  1. 条件独立的理解及举例
  2. gcc/g++ 参数总结
  3. C++基础::shared_ptr 编程细节(三)
  4. 编程规范 —— 变量的命名
  5. go token验证_GitHub - goflyfox/gtoken: 基于gf框架的token插件,通过服务端验证方式实现token认证;...
  6. python 制作抽奖箱_丽水本地抽奖箱制作公司,抽奖箱制作-优质服务!
  7. python怎么读取excel-Python|读、写Excel文件(三种模块三种方式)
  8. php和python-PHP与Python语言有哪些区别之处?选择哪一个好?
  9. python教程-Python教程
  10. python 计算机程序设计-程序设计入门—Python