是的,这是可能的:

public class Foo { private int x; public Foo() { this(1); } public Foo(int x) { this.x = x; } }

要链接到一个特定的超类构造函数而不是同一个类中的构造函数,请使用super而不是this 。 请注意, 您只能链接到一个构造函数 , 它必须是构造函数体中的第一个语句 。

另请参阅相关的问题 ,这是关于C#,但在相同的原则适用。

使用this(args) 。 首选模式是从最小的构造函数到最大的构造函数。

public class Cons { public Cons() { // A no arguments constructor that sends default values to the largest this(madeUpArg1Value,madeUpArg2Value,madeUpArg3Value); } public Cons(int arg1, int arg2) { // An example of a partial constructor that uses the passed in arguments // and sends a hidden default value to the largest this(arg1,arg2, madeUpArg3Value); } // Largest constructor that does the work public Cons(int arg1, int arg2, int arg3) { this.arg1 = arg1; this.arg2 = arg2; this.arg3 = arg3; } }

您也可以使用最近提倡的valueOf或“of”的方法:

public class Cons { public static Cons newCons(int arg1,...) { // This function is commonly called valueOf, like Integer.valueOf(..) // More recently called "of", like EnumSet.of(..) Cons c = new Cons(...); c.setArg1(....); return c; } }

要调用超类,请使用super(asdf) 。 对super的调用必须是构造函数中的第一个调用,否则将会出现编译器错误。

[ 注:我只是想添加一个方面,我没有在其他答案中看到:如何克服()必须在第一行)的要求的限制。 ]

在Java中,可以通过this()从构造函数中调用同一个类的另一个构造函数。 但请注意, this必须在第一行。

public class MyClass { public MyClass(double argument1, double argument2) { this(argument1, argument2, 0.0); } public MyClass(double argument1, double argument2, double argument3) { this.argument1 = argument1; this.argument2 = argument2; this.argument3 = argument3; } }

这必须出现在第一行看起来像一个很大的限制,但你可以通过静态方法构造其他构造函数的参数。 例如:

public class MyClass { public MyClass(double argument1, double argument2) { this(argument1, argument2, getDefaultArg3(argument1, argument2)); } public MyClass(double argument1, double argument2, double argument3) { this.argument1 = argument1; this.argument2 = argument2; this.argument3 = argument3; } private static double getDefaultArg3(double argument1, double argument2) { double argument3 = 0; // Calculate argument3 here if you like. return argument3; } }

当我需要从代码中调用另一个构造函数(而不是在第一行)时,我通常使用这样的帮助方法:

class MyClass { int field; MyClass() { init(0); } MyClass(int value) { if (value<0) { init(0); } else { init(value); } } void init(int x) { field = x; } }

但是大多数情况下,我会尽可能通过在第一行中调用更简单的构造函数来实现。 对于上面的例子

class MyClass { int field; MyClass(int value) { if (value<0) field = 0; else field = value; } MyClass() { this(0); } }

在构造函数中,可以使用this关键字来调用同一个类中的另一个构造函数。 这样做被称为显式构造函数调用 。

这里是另外一个Rectangle类,与Objects部分中的不同。

public class Rectangle { private int x, y; private int width, height; public Rectangle() { this(1, 1); } public Rectangle(int width, int height) { this( 0,0,width, height); } public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } }

这个类包含一组构造函数。 每个构造函数初始化矩形的一些或全部成员variables。

正如大家已经说过的,你使用this(…) ,这被称为显式的构造函数调用 。

但是,请记住,在这样一个显式的构造函数调用语句中, 您可能不会引用

任何实例variables或

任何实例方法或

在这个类或任何超类中声明的任何内部类,或者

this还是

super 。

正如JLS(§8.8.7.1)所述。

您可以使用“this”关键字从同一个类的另一个构造函数构造函数。 示例 –

class This1 { This1() { this("Hello"); System.out.println("Default constructor.."); } This1(int a) { this(); System.out.println("int as arg constructor.."); } This1(String s) { System.out.println("string as arg constructor.."); } public static void main(String args[]) { new This1(100); } }

输出 – string作为arg构造函数..默认构造函数.. int作为arg构造函数..

我给你打电话一个简单的方法

有两种types的构造函数:

默认的构造函数

参数化的构造函数

我将在一个例子中解释

class ConstructorDemo { ConstructorDemo()//Default Constructor { System.out.println("D.constructor "); } ConstructorDemo(int k)//Parameterized constructor { this();//-------------(1) System.out.println("P.Constructor ="+k); } public static void main(String[] args) { //this(); error because "must be first statement in constructor new ConstructorDemo();//-------(2) ConstructorDemo g=new ConstructorDemo(3);---(3) } }

在上面的例子中,我展示了三种types的调用

this()对此的调用必须是构造函数中的第一条语句

这是名称较less的对象。 这是自动调用默认的构造函数。 3.调用参数化的构造函数。

注意: 这必须是构造函数中的第一条语句。

是的,可以从另一个构造函数调用。 但有一个规则。 如果从一个构造函数向另一个构造函数调用,那么

新的构造函数调用必须是当前构造函数中的第一个语句

public class Product { private int productId; private String productName; private double productPrice; private String category; public Product(int id, String name) { this(id,name,1.0); } public Product(int id, String name, double price) { this(id,name,price,"DEFAULT"); } public Product(int id,String name,double price, String category){ this.productId=id; this.productName=name; this.productPrice=price; this.category=category; } }

所以像下面的东西不会工作。

public Product(int id, String name, double price) { System.out.println("Calling constructor with price"); this(id,name,price,"DEFAULT"); }

从另一个构造函数调用构造函数

class MyConstructorDemo extends ConstructorDemo { MyConstructorDemo() { this("calling another constructor"); } MyConstructorDemo(String arg) { System.out.print("This is passed String by another constructor :"+arg); } }

你也可以使用super()调用来调用父构造函数

关键字this可以用来从构造函数中调用构造函数,当为某个类编写几个构造函数时,有时候需要从另一个构造函数中调用一个构造函数来避免重复的代码。

贝娄是一个链接,我解释了关于构造函数和getters()和setters()的其他主题,我用了一个具有两个构造函数的类。 我希望这些解释和例子能帮助你。

Setter方法或构造函数

是的,可以使用this()从另一个构造器中调用另一个构造器。

class example{ private int a = 1; example(){ this(5) //here another constructor called based on constructor argument System.out.println("number a is "+a); } example(int b){ System.out.println("number b is "+b); }

是的,任何数量的构造函数都可以存在于一个类中,并且可以由另一个构造函数使用this()[请不要将this()构造函数调用与this关键字混淆]。

this()或this(args)应该是构造函数中的第一行。

Example: Class Test{ Test(){ this(10) // calls the constructor with integer args, Test(int a) } Test(int a){ this(10.5) // call the constructor with double arg, Test(double a) } Test(double a){ System.out.println("I am a double arg constructor"); } }

这被称为构造函数重载。

请注意,对于构造函数,只有重载概念是适用的,而不是inheritance或重写。

有一些devise模式可以满足复杂构build的需要 – 如果不能简洁地完成,可以创build工厂方法或工厂类。

随着最新的Java和lambda的添加,很容易创build一个构造函数,它可以接受任何你想要的初始化代码。

class LambdaInitedClass { public LamdaInitedClass(Consumer init) { init.accept(this); } }

与…通话

new LambdaInitedClass(l -> { // init l any way you want });

java 调用其他构造函数_我如何在Java中调用另一个构造函数?相关推荐

  1. java recordset 记录数_【求助】asp中怎样获得一个记录集(Recordset)的所有行数?

    你的位置: 问答吧 -> 网络编程 -> 问题详情 [求助]asp中怎样获得一个记录集(Recordset)的所有行数? 如标题所示 [ 本帖由 smilekiki 最后编辑于 2006- ...

  2. java创建单线程计时器_我们如何在Java中实现计时器线程?

    该定时器类计划任务一次或多次给定的时间运行.它也可以作为后台程序线程在后台运行.要将Timer与守护程序线程相关联,有一个带有布尔值的构造函数.计时器以固定的延迟和固定的速率安排任务.在固定的延迟中, ...

  3. python用api调用c语言_在C/C++程序中调用High Level的Python API

    Python API的类型 分为High Level API和Low Level API. Low Level API是API的基础,调用High Level API时,Python基础库会为C/C+ ...

  4. ibatis mysql存储过程_分步详解 如何在iBatis中调用存储过程

    通过iBatis我们可以在数据库表中执行内嵌的insert , delete, update SQL命令.本文中你将看到如何在iBatis中调用存储过程. 我们使用MySQL数据库,并且使用和上一个例 ...

  5. java构造函数未定义_错误:隐式超级构造函数Person()未定义.必须显式调用另一个构造函数....

    今天写程序的时候发现了一个错误: public class Test { public static void main(String[] args) { Student s1 = new Stude ...

  6. 一步一步学Silverlight 2系列(21):如何在Silverlight中调用JavaScriptjavascript

    一步一步学silverlight 2系列(21):如何在silverlight中调用javascript 概述 silverlight 2 beta 1版本发布了,无论从runtime还是tools都 ...

  7. excel调用python编程-如何在excel中调用python脚本

    如何在excel中调用python脚本 发布时间:2020-07-03 14:15:28 来源:亿速云 阅读:155 如何在excel中调用python脚本?针对这个问题,这篇文章详细介绍了相对应的分 ...

  8. 如何在MFC中调用CUDA

    如何在MFC中调用CUDA 有时候,我们需要在比较大的项目中调用CUDA,这就涉及到MFC+CUDA的环境配置问题,以矩阵相乘为例,在MFC中调用CUDA程序.我们参考罗振东iylzd@163.com ...

  9. 一步一步学Silverlight 2系列(21):如何在Silverlight中调用JavaScript

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

最新文章

  1. Java三大主流框架概述--(转载)
  2. Oracle管理拾遗(长期更新)
  3. 30 天学习 30 种新技术系列
  4. linux 内核 ntfs,Linux大脑 内核 内核编译(NTFS)
  5. 【Floyed】廉价最短路径
  6. 505B. Mr. Kitayuta‘s Colorful Graph
  7. hadoop2.7.3+spark2.1.0+scala2.12.1环境搭建(4)SPARK 安装
  8. TensorFlow2 实现神经风格迁移,DIY数字油画定制照片
  9. FISCO BCOS(三)——— 部署及调用HelloWorld合约
  10. 查看Anaconda内置的Python版本的方法
  11. 用CSS制作细线表格
  12. Windows命令之ping命令
  13. AXURE母版事件(Raised-events)
  14. linux网络服务配置说课,说课稿 LINUX.ppt
  15. 如何更换和删除微软雅黑字体
  16. freeswitch软电话配置、结合讯时网关,外线电话呼入、呼出配置
  17. 云南省计算机一级考试题7,计算机(一级B类)云南省计算机一级考试题库.doc
  18. 他们是公众号界的一股清流!
  19. Html+CSS+JS轮播图:手动轮播,自动轮播
  20. c++入门 有关《c++关键字》 《命名空间》《缺省参数》《函数重载》《引用》《内联函数》《outo关键字》

热门文章

  1. 在APPLE从创建ID到申请发布AppStore账户(四)登记前Apple ID 的双重认证(Two-factor authentication for Apple ID)
  2. centos安装anaconda教程
  3. Stating the Obvious
  4. LinQ综合应用实例
  5. 打破逆境,新型CRM怎样成为华为有赞们营销新增长的关键?
  6. 融合网络位置服务器,全IP网络融合
  7. 沐雪微信3.0功能说明
  8. altium 原理图reset unique id
  9. advance vocabulary
  10. 微信小程序开发引入icon图标出现小框的问题