相关疑难解决方法(0)

Java默认构造函数

究竟什么是默认构造函数 - 你能告诉我以下哪一个是默认构造函数,以及它与其他构造函数的区别是什么?

public Module() {
   this.name = "";
   this.credits = 0;
   this.hours = 0;
}

public Module(String name, int credits, int hours) {
   this.name = name;
   this.credits = credits;
   this.hours = hours;
}
Run Code Online (Sandbox Code Playgroud)

java constructor default-constructor

147
推荐指数
7
解决办法
37万
查看次数

Java中未初始化的变量和成员

考虑一下:

public class TestClass {

    private String a;
    private String b;

    public TestClass()
    {
    a = "initialized";
    }

    public void doSomething()
    {
    String c;

        a.notify(); // This is fine
    b.notify(); // This is fine - but will end in an exception
    c.notify(); // "Local variable c may not have been initialised"
    }

}
Run Code Online (Sandbox Code Playgroud)

我不明白."b"永远不会被初始化,但会产生与"c"相同的运行时错误,这是一个编译时错误.为什么局部变量和成员之间存在差异?

编辑:让会员私密是我最初的意图,问题仍然存在......

java oop variables

28
推荐指数
3
解决办法
2万
查看次数

Java中的空构造函数

在 Java 中使用空构造函数可以吗?例如,从 MySQL 数据库加载数据时,我想执行以下操作:

ResultSet set = statement.executeQuery();
while (set.next()) {
    Faction faction = new Faction();
    faction.setId(UUID.fromString(set.getString("id")));
    faction.setName(set.getString("name"));
}
Run Code Online (Sandbox Code Playgroud)

因为我已经有一个派系类的构造函数,

public Faction(Player player, UUID uuid) {}
Run Code Online (Sandbox Code Playgroud)

我想知道我是否可以有一个普通的构造函数并且只设置值和时间。

否则,我可以使用与 mySQL 数据匹配的参数(公共派系(字符串名称、UUID uuid、字符串公告等)来创建一个构造函数来加载......不确定什么是最佳实践?

java constructor

0
推荐指数
1
解决办法
3477
查看次数

标签 统计

java ×3

constructor ×2

default-constructor ×1

oop ×1

variables ×1