Tag: 构造函数

子类构造函数是否需要超类构造函数的所有参数?

我有两个class, Staff和AdvancedStaff ,它扩展了前者。 Staff有这个构造函数: public Staff (String number, String title, String name, String role, char level) { staffNumber = number; staffTitle = title; staffName = name; staffRole = role; payScaleLevel = level; } 我会注意到所有实例变量都已设置为私有。 虽然, Advanced Staff有这个构造函数: public AdvancedStaff (String number, String title, String name) { super(number, title, name); role = “Entry level Advanced Staff”; […]

使用inheritance和重写方法在Java中复杂输出

我偶然发现了这段代码。 我试着猜测在实际操作之前运行它的结果是什么。 当我看到它们并需要一些解释时,我真的很困惑。 这是代码: public class A { String bar = “A.bar”; A() { foo(); } public void foo() { System.out.println(“A.foo(): bar = ” + bar); } } public class B extends A { String bar = “B.bar”; B() { foo(); } public void foo() { System.out.println(“B.foo(): bar = ” + bar); } } public […]

Java类“无法解析为类型”

这是我得到的错误: Exception in thread “main” java.lang.Error: Unresolved compilation problem: TeamLeader cannot be resolved to a type at TeamLeadDemo.main(TeamLeadDemo.java:26) 这是我的代码: import java.util.Scanner; public class Employee { public String empName, empNumber, hireDate; public class TeamLeadDemo {} public Employee(String empName, String empNumber, String hireDate) { this.setEmpName(empName); this.setEmpNumber(empNumber); this.setHireDate(hireDate); } public void setEmpName(String empName) { this.empName = empName; } […]

Java“空白的最终字段可能尚未初始化”Anonymous Interface vs Lambda Expression

我最近遇到了错误消息“空白的最终字段obj可能尚未初始化”。 通常情况下,如果您尝试引用可能尚未分配给值的字段。 示例类: public class Foo { private final Object obj; public Foo() { obj.toString(); // error (1) obj = new Object(); obj.toString(); // just fine (2) } } 我用Eclipse。 在第(1)我得到错误,在第(2)一切正常。 到目前为止这是有道理的。 接下来,我尝试在构造函数内创建的匿名接口中访问obj 。 public class Foo { private Object obj; public Foo() { Runnable run = new Runnable() { public void run() { obj.toString(); […]

Javainheritance错误:隐式超级构造函数未定义

我是Java的新手,只是学习OOP概念。 请查看我的代码。 我收到以下错误.- 隐式超级构造函数未定义。 class BoxSuper { int height; int length; int width; BoxSuper(BoxSuper obj) { height=obj.height; length=obj.length; width=obj.width; } BoxSuper(int a,int b,int c) { height=a; length=b; width=c; } BoxSuper(int val) { height=length=width=val; } int volume() { return height*length*width; } } class BoxSub extends BoxSuper { int weight; BoxSub(int a,int b,int c,int d) { height=a; […]

为什么我们在Java中需要一个默认的无参数构造函数?

为什么我们在许多Java相关API中需要一个默认的无参数构造函数? 像一般规则一样,所有java bean类或实体类(JPA等)或JAX-WS实现类都需要显式的无参数构造函数。 如果默认情况下Java提供了无参数构造函数,那么为什么这些标准中的大多数需要显式构造函数?

Java:构造函数如何返回值?

$ cat Const.java public class Const { String Const(String hello) { return hello; } public static void main(String[] args) { System.out.println(new Const(“Hello!”)); } } $ javac Const.java Const.java:7: cannot find symbol symbol : constructor Const(java.lang.String) location: class Const System.out.println(new Const(“Hello!”)); ^ 1 error

如何从匿名内部类调用特定的父构造函数?

好的,所以我知道匿名内部类要么隐式扩展父类,要么实现接口,因此需要调用超类的构造函数。 但是,我不确定如何为匿名类创建构造函数(如果这是可能的)并且没有定义构造函数我不知道如何调用super()! 这是我的练习代码: public class AnonymousConstructor { public static void main(String[] args) { //I’m not sure how to explicitly call one of the arg super constructors MyBob my = new MyBob() { //I would like to do something like this super(“String”); or //super(“String”, “String”); }; } } class MyBob extends Thread { MyBob() { System.out.println(“No […]

子类是否从它的超类inheritance构造函数?

在子类中,我们可以使用子类的构造函数初始化数据成员,该构造函数在内部调用超类的构造函数super() 。 如果子类不能从其超类inheritance构造函数,那么super()调用如何初始化超类?

使用reflection创建新对象?

给定类值: public class Value { private int xVal1; private int xVal2; private double pVal; // constructor of the Value class public Value(int _xVal1 ,int _xVal2 , double _pVal) { this.xVal1 = _xVal1; this.xVal2 = _xVal2; this.pVal = _pVal; } public int getX1val() { return this.xVal1; } … } 我正在尝试使用reflection创建该类的新实例: 来自Main: …. // some code …. […]