Tag: 构造函数

如何在我的自定义exception中设置我自己的消息,可以检索我的getMessage()但是没有使用构造函数,有什么办法吗?

我刚学习Java中的exception处理。 我想知道的不是尝试说: throw new Exception(“My Message”); 和 String message=ex.getMessage(); System.out.println(message); 看看下面的代码, class ExceptionTest { public static void main(String[] args) { ExceptionTest t1=new ExceptionTest(); try { t1.riskyMethod();//call the risky or exception throwing method } catch(MyException myex) { System.out.println(“Exception has been thrown”); String message=myex.getMessage();//get the String passed during exception call System.out.println(“The message retrieved is “+message); myex.printStackTrace();//prints name […]

Java:Class 和Class:从类的内部/外部使用时的差异

我有一个类需要使用Class参数(参见我之前的半相关问题 )。 它是: public class BaseTable { protected Class mClass; … public BaseTable(int rows, int cols, Class clasz) { … mClass = clasz; } public BaseTable(int rows, int cols) { this(rows, cols, StringTableEntry.class); //Does NOT compile: //expected [int, int, Class], but got //[int, int, Class] } … } 我想知道为什么构造函数(带有2个参数)不起作用,但是当我从外部类调用完全相同的东西时,如下所示: mSomeTable = new BaseTable(2, 2, StringTableEntry.class); […]

如何使用具有相同参数类型的多个构造函数创建类

我想做这样的事情: 公共类Arquivo {private File diretorio = null; public Arquivo(File dir){this.diretorio = dir; public Arquivo(String dir){this(new File(dir)); public Arquivo(String fileName){this(new File(“./ src / Data /”+ fileName)); }}

为什么这个类有两个构造函数?

我在幻灯片中看到了这一点,旨在说明构造函数。 我现在很困惑,因为它有两个具有相同作业的构造函数接受在第二个中将gpa设置为零。 为什么编码器需要重复this.id = id; this.name = name; this.id = id; this.name = name; 再次? 为什么这个类甚至需要两个构造函数? class Student{ private int id; private String name; private double gpa; public Student(int id, String name, double gpa){ this.id = id; this.name = name; this.gpa = gpa; } public Student(int id, String name){ this.id = id; this.name = name; […]

如果在构造函数中使用super调用重写方法会发生什么

有两个类Super1和Sub1 Super1.class public class Super1 { Super1 (){ this.printThree(); } public void printThree(){ System.out.println(“Print Three”); } } Sub1.class public class Sub1 extends Super1 { Sub1 (){ super.printThree(); } int three=(int) Math.PI; public void printThree(){ System.out.println(three); } public static void main(String …a){ new Sub1().printThree(); } } 当我调用类Sub1的方法printThree ,我期望输出为: 打印三 3 因为Sub1构造函数调用了super.printThree(); 。 但我真的得到了 0 打印三 3 […]

关于建设者

我有一个关于Constructors的问题。我认为构造函数只是我们的方便而不是setter方法,对吧? 因此,对于一个对象,您认为重要的属性(例如webform中的必需字段)将作为参数传递给构造函数。 是否有任何标准要将这么多参数传递给构造函数? 请详细说明这些要点以及有关构造函数的任何要点。 编辑 :抱歉我问问题的方式。是的,我们用构造函数创建一个对象,我们用setter赋值,但我的问题是默认构造函数与setter和构造函数与显式参数之间的比较。

Java:构造函数中的NULL

请问这个问题有点理论,但我想理解。 为什么我将null参数传递给构造函数我是否会得到NullPointerException ? 这是我的榜样 new AttendeeDetail(“Gus Goose”,”1151″,”15-01-2012″,(Integer) null,null) 这是class级: public class AttendeeDetail { private String ticketholder_name; private String user_id; private String date_of_birth; private int tickets_purchased; private ArrayList tickets; public AttendeeDetail(String ticketholder_name, String user_id, String date_of_birth, int tickets_purchased, ArrayList tickets) { this.ticketholder_name=ticketholder_name; this.user_id=user_id; this.date_of_birth=date_of_birth; this.tickets_purchased=tickets_purchased; this.tickets=tickets; } }

为什么Java Pattern类使用工厂方法而不是构造函数?

在一般情况下 ,对此进行了很好的讨论。 但是,我特别想知道为什么Pattern类使用compile静态方法来创建一个对象,而不是构造函数? 在我看来使用构造函数更直观。

Java:在各种其他类中使用相同的类实例

这可能是一个愚蠢的问题,但我不知道它的答案,我不知道在哪里搜索答案,所以如果有人可以帮助我会很好。 我有一个类(让我们命名为A)具有不同的成员和方法。 我在另一个类中使用此类的方法(让我们将其命名为B)。 对于每个创建的B-Object,我想使用A的SAME实例。这可能吗? 实际上我在B中有一个构造函数,我称之为A a = new A(); 当然,我总是得到这个类的不同实例。 我现在该怎么改变这个? 我知道可以用spring框架解决它(将相同的对象注入到B的实例中),但我无法使用它。 怎么还能解决这个问题? 非常感谢您的帮助! 🙂

Java – 应该通过getter和setter方法在构造函数中访问私有实例变量吗?

我知道私有实例变量是通过他们的公共getter和setter方法访问的。 但是当我在IDE的帮助下生成构造函数时,它直接初始化实例变量,而不是通过setter方法初始化它们。 Q1。 因此,我应该为构造函数更改IDE生成的代码,以通过其setter方法初始化这些实例变量。 Q2。 如果是,那么IDE为什么不以这种方式生成构造函数代码? ============================= EDITED ==================== =================== 我使用Eclipse和Netbeans IDE 这是一个普遍的问题。 但正如@Lords所要求的那样,答案取决于我们的构造函数是公共的还是受保护的,还是私有的还是私有的?