Tag: 构造函数

如何避免制作长构造函数

我有一个客户端库,我正在对我的rest服务进行http远程调用,然后我将List返回给正在调用我们的库的客户,我从REST服务获得的响应以及任何错误,如果有任何包裹DataResponse对象。 public class DataResponse { private final String response; private final boolean isLink; private final TypeOfId idType; private final long ctime; private final long lmd; private final String maskInfo; // below are for error stuff private final ErrorCode error; private final StatusCode status; // constructors and getters here } 这是我的ErrorCode枚举类: public enum ErrorCode { // […]

如何在java中使用varargs作为Constructor.getConstructor()的参数

我有一个像下面这样的java类,我想通过使用类名动态创建这个类的实例。 class Demo { public Demo(String… s) { //some initialization here. } } 我想使用以下代码创建一个对象 Class klass = Class.forName(“Demo”); Constructor con = klass.getConstructor(“**what should be here**”); Object obj = con.newInstance(param1, param2, …);

java中的构造函数是什么,如果它不是类的成员?

如果它不是Oracle doc中所述类的成员,我们称之为构造函数: http : //docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Javainheritance:为什么在父构造函数级别调用方法,调用重写的子方法?

在Fruit构造函数中调用fruitName方法,实际上是将调用委托给子类Apple类的方法! public class CallingParentMethodInInheritanceHierarchy { abstract class Fruit { String fruitName; public Fruit(String fruitName) { this.fruitName = fruitName; /* * o/p – Inside constructor – Child: Fruit name is – Apple */ System.out.println(“Inside constructor – ” + fruitName()); // doubt? } public String fruitName() { return “Parent: Fruit name is – ” + fruitName; } […]

java中的“无效声明方法”

public class Bugs{ private String bugType; private int legs; private int arms; private String nativeTo; public bug(String bt, int l, int a, String nt){ bt=bugType; l=legs; a=arms; nt=nativeTo; } } 为什么我在这里继续获得“无效的声明方法”? 它一直在说需要一种返回方法。

什么时候必须在Java中使用默认构造函数和参数化构造函数?

很多时候我有一个例外,说“缺少默认构造函数的实现”。 很多时候,参数化构造函数的定义单独执行所有操作。 我想知道这种情况发生在哪种情况下。

如何在Java中实例化generics方法参数的实例?

请考虑以下代码: // … public class BaseClass { public BaseClass (int theParam) { // …whatever… } } public class DerivedType { // …Content does not matter… } // …elsewhere: public boolean doIt (ArrayList target) { ElemType newElem=new ElemType (5) ; // “Cannot instantiate this type” // …other code does not matter… return true ; } // […]

JUnit:具有私有字段的测试构建器

我是初学者,我在类的构造函数中遇到了JUnit测试的问题。 我想测试的类名为IntSortedArray,如下所示: public class IntSortedArray { private int[] elements; private int size; public IntSortedArray() { this.elements = new int[16]; this.size = 0; } public IntSortedArray(int initialCapacity) throws IllegalArgumentException { if(initialCapacity < 0) { throw new IllegalArgumentException("Error – You can't create an array of negative length."); } else { elements = new int[initialCapacity]; size = 0; […]

Java – 来自构造函数的setter

package cen.col.course.demo; import java.io.Serializable; public class Course implements Serializable { private static final long serialVersionUID = 1L; protected String code; protected String title; protected Professor professor; public Course( String code) throws InvalidDataException { super(); setCode(code); } public Course(String code, String title ) throws InvalidDataException { this(code); setTitle(title); } public Course(String code, String title, Professor professor) […]

哪个新的首先执行 – 在构造函数中还是在外?

如果我定义一个类如下: 公共类myClass { private x = new anotherClass(); 私人y; public myClass(){ y = new anotherClass(); } } 哪个变量会更早获得实例? x还是y? 并且,在构造函数之外分配变量是不建议的吗?