java构造函数

以下是为您整理出来关于【java构造函数】合集内容,如果觉得还不错,请帮忙转发推荐。

【java构造函数】技术教程文章

具有最终用户馈送字段的类的Java构造函数【代码】

我的问题是:使用通过stdin初始化的字段编写Java类构造函数的最佳方法是什么? 例如,假设我有一个类似于的Employee类:Public class Employee {private int empID;private String empName;private List<Role> empRoles;{....} }我可以为这堂课写下所有的二传手和门手.当然,Role类将拥有自己的文件. 还假设我为前两个字段创建了我的setter,如下所示,以便让最终用户初始化字段:public void setEmpID() {System.out.println("Please ...

抽象类的Java构造函数

据我所知(如果我错了请纠正我)一个抽象的类无法实例化.你可以给它一个构造函数,但不能在该类上调用new.如果在子类中调用super,超类构造函数将运行(从而创建该类的对象?)那么为什么你实际上可以在抽象类的子类中调用super?我确定这与我对构造函数制作对象的误解有关…解决方法:If you call super in a subclass, the superclass constructor will run (and thus create an object of that class??) then how come you can accuall...

Java“this”在构造函数中【代码】

嗯,这是一个非常基本的问题,我从来没有用java编写代码,但我正在为一个朋友写一个类……有类似的东西:class myClass{private string name;public string getName() {return this.name;} public void setName (int newValue) {this.name = newValue;}private int number;public int getNumber() {return this.number;} public void setNumber (int newValue) {this.number = newValue;} } 我想构建构造函数的方式是:public my...

java – 将构造函数方法拆分为部分 – 最终值的问题【代码】

我想把我班级的构造函数分成几部分.但我有一个问题…… 是否可以在构造函数内部的方法中初始化最终值?它必须直接在构造函数中初始化? 这个…import java.util.Scanner;public final class A {private final int L;private final int D;private final int N;public A(){Scanner scanner = new Scanner(System.in);this.getFirstLine(scanner);/* the rest of the constructor method */}private void getFirstLine(Scanner scanne...

java – 在构造函数中初始化public static final变量【代码】

我正在尝试为我的应用程序创建一个Version类,它将在加载时从清单中读取版本号,然后仅引用Version.MAJOR等我在其他地方需要的地方.但是,我遇到了这样的问题.这是我目前的代码:public class Version {public static final int APPCODE;public static final int MAJOR;public static final int MINOR;public static final char RELEASE;public static final int BUILD;static {try {Class clazz = Version.class;String className = ...

java – 构造函数必须在方法返回之前调用super()或this()【代码】

我收到此错误:Exception in thread "Thread-0" java.lang.VerifyError: Constructor must call super() or this() before return in method JGame.Util.KeyboardMap.<init>()V at offset 0at JGame.Room.Room.keyboardEventTests(Room.java:81)at JGame.Room.Room.run(Room.java:54)at java.lang.Thread.run(Thread.java:722)当我的应用程序加载时,它立即调用此方法(KeyboardMap.map是一个空的HashMap). 这是方法(第54行调用此方法...

java – 构造函数不能应用于给定的类型?【代码】

我有以下Java代码:public class WeirdList {/** The empty sequence of integers. *//*ERROR LINE */ public static final WeirdList EMPTY = new WeirdList.EmptyList();/** A new WeirdList whose head is HEAD and tail is TAIL. */public WeirdList(int head, WeirdList tail) {headActual = head;tailActual = tail;}/** Returns the number of elements in the sequence that* starts with THIS. */public int length() {r...

Java构造函数链接【代码】

参见英文答案 > How to avoid constructor code redundancy in Java? 4个嗨,我刚学习Java中的构造函数链接,并有一些问题…… >首先,有人可以解释我何时需要使用它?在我的头脑中,我真的想不到一个情况.>在这个例子中,在没有参数的构造函数中,我调用另一个构造函数.如何访问这个新的“James Bond”对象以备将来使用?import java.util.*;class Employee { private String name;private double...

java – 构造函数没有任何返回类型,但是如何创建对象?【代码】

根据构造函数的定义,它们没有任何返回类型,但在创建对象时我们经常做A a = new A();负责创建对象a.A a=new A();任何人都可以帮助我理解这个问题,在创建Object时构造函数实际发生了什么.解决方法:构造函数没有返回类型,正确.但表达式新的A()确实有一个结果:对新创建的对象的引用. 以下是新A()的情况: >创建一个对象>它给出了A型>调用相关的A构造函数,引用该新对象>初始化完成后,表达式完成>表达式的结果是对新对象的引用 此过程在...

java – 何时为构造函数抛出异常【代码】

public Neocortex(Region rootRegion, ConnectionInterface functor) { this.rootRegion = rootRegion; this.currentRegion = this.rootRegion; this.functor = functor; }嘿上面我有一个类的构造函数.我的问题是我应该在构造函数中添加空指针异常还是不必要?老实说,我只是不明白何时应该为我的代码添加例外.但在这种情况下,我应该使用哪个构造函数?public Neocortex(Region rootRegion, ConnectionInterface functor) {if (root...