[译]understanding constructors

简介:

key words:
constructor      构造函数         method        方法            instance          实例
object             对象              modifier         修饰符         return type      返回类型
static method   静态方法         superclass      超类           Inheritance      继承
platypus          鸭嘴兽
我们说一个构造函数是方法就好比说澳洲鸭嘴兽是另一个哺乳动物一样。 为了了解鸭嘴兽,知道其与其他的哺乳动物的差别对我们来说非常重要。同理,了解构造函数,知道构造函数与其他方法的区别对我们同样重要。对于任何学习Java,尤其是为了获得资格证书的学生来说,都需要知道这些区别。在这篇文章中,我将会一一道来。在文章结尾,Table1 总结了constructor(构造函数)和method(方法)的重要区别。
Purpose and Function (目的与功能)
构造函数都有一个目的:创建一个类的实例。也可以叫做创建一个对象,如下:

Platypus p1  =   new  Platypus();


相比之下,方法(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 指的是,在同一个类中拥有不同的参数列表的另一个构造函数。代码如下:

public   class  Platypus {
       String name;
       Platypus(String input) {
               name 
=  input;
       }
       Platypus() {
               
this ( " John/Mary Doe " );
       }
       
public   static   void  main(String args[]) {
               Platypus p1 
=   new  Platypus( " digger " );
               Platypus p2 
=   new  Platypus();
       }
}

在代码中,有两个构造函数。
第一个构造函数通过一个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 将会执行超类中被覆盖的方法,如下所示:

class  Mammal {
       
void  getBirthInfo() {
               System.out.println(
" born alive. " );
       }
}
class  Platypus  extends  Mammal {
       
void  getBirthInfo() {
               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。
代码如下:

public   class  SuperClassDemo {
        SuperClassDemo() {}
}
class  Child  extends  SuperClassDemo {
       Child() {
               
super ();
       }
}

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

public   class  Example {}


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

public   class  Example {
       Example() {}
}


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

public   class  TestConstructors {
       TestConstructors() {}
}


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

public   class  TestConstructors {
       TestConstructors() {
               
super();        
         }
}


初学者可能会有疑问:TestConstructors 并没有继承任何类,为什么它会调用父类的构造函数呢?
答案是:在 Java 中如果没有显示的继承一个类,则默认为继承自 Object 类。

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

public   class  Example {} 



public   class  Example {
       Example() {
             
super() ;
       }
}

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

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

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

public   class  Example {
        
public   void  sayHi {
                system.out.println(
" Hi " );
        }
        Example() {}
}
public   class  SubClass  extends  Example {
}

类 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
本文转自BlogJavaOo缘来是你oO的博客,原文链接: [译]understanding constructors,如需转载请自行联系原博主。

相关文章
|
11月前
|
人工智能 前端开发 测试技术
探索前端与 AI 的结合:如何用 GPT-4 助力开发效率
本文介绍了 GPT-4 如何成为前端开发者的“神队友”,让开发变得更加高效愉快。无论是需求到代码的自动生成、快速调试和性能优化,还是自动化测试和技术选型,GPT-4 都能提供极大的帮助。通过智能生成代码、捕捉 BUG、优化性能、自动化测试生成以及技术支持,GPT-4 成为开发者不可或缺的工具,帮助他们从繁重的手动任务中解脱出来,专注于创新和创意。GPT-4 正在彻底改变开发流程,让开发者从“辛苦码农”转变为“效率王者”。
371 0
探索前端与 AI 的结合:如何用 GPT-4 助力开发效率
|
存储 数据可视化 Python
|
Web App开发 编解码 算法
Web端H.265播放器研发解密
音视频编解码对于前端工程师是一个比较少涉足的领域,涉及到流媒体技术中的文本、图形、图像、音频和视频多种理论知识的学习,才能够应用到具体实践中,本团队在多媒体领域深耕两年多,才算是有一定产出,我们自研web播放器并支持h.265解码,在码率优化的大背景下(保持画质不变情况下,应用图像增强、roi区域检测、智能场景分类和h265编解码等多种技术能力,将码流降低50%。
Web端H.265播放器研发解密
|
存储 对象存储 CDN
OSS 回源功能
浅谈 OSS 回源功能,也称镜像回源,可以类比 Nginx 的 PCRE 满足一定条件下触发的 rewrite 功能,但是 OSS 的功能更加丰富。大致分为了两种主要功能,一种是 404 回源,另外是重定向回源。
6556 0
OSS 回源功能
|
2天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1083 0
|
11天前
|
人工智能 运维 安全