//: c07:Sandwich.java
// Order of constructor calls.
// package c07;
// import com.bruceeckel.simpletest.*;
import java.util.*;
class Meal {
Meal() { System.out.println("Meal()"); }
}
class Bread {
Bread() { System.out.println("Bread()"); }
}
class Cheese {
Cheese() { System.out.println("Cheese()"); }
}
class Lettuce {
Lettuce() { System.out.println("Lettuce()"); }
}
class Lunch extends Meal {
Lunch() { System.out.println("Lunch()"); }
}
class PortableLunch extends Lunch {
PortableLunch() { System.out.println("PortableLunch()");}
}
public class Sandwich extends PortableLunch {
// private static Test monitor = new Test(); …Run Code Online (Sandbox Code Playgroud) 这是代码,我定义了两个名为Father和Son的类,并在main函数中创建它们:
public class Test {
public static void main(String[] args) {
Father father = new Son();
}
}
class Father {
private String name = "father";
public Father() {
who();
tell(name);
}
public void who() {
System.out.println("this is father");
}
public void tell(String name) {
System.out.println("this is " + name);
}
}
class Son extends Father {
private String name = "son";
public Son() {
who();
tell(name);
}
public void who() {
System.out.println("this is son");
}
public void tell(String name) …Run Code Online (Sandbox Code Playgroud) 我有一个简单的问题。我有这样的类结构:基类和子类(关系是继承)。@BeforeAll 来自 JUnit5。
abstract class Base {
static{
System.out.println("A");
}
@BeforeAll
public static void setUp() {
System.out.println("B");
}
}
class Child extends Base {
static {
System.out.println("C");
}
@Test
public void test() {
System.out.println("D");
}
}
Run Code Online (Sandbox Code Playgroud)
所以,在我看来,执行顺序应该是:ACBD,但是,这看起来像:ABCD 我正在搜索信息,但我找不到任何有关它的信息。
在编译这个程序时,我得到错误 -
class Person {
Person(int a) { }
}
class Employee extends Person {
Employee(int b) { }
}
public class A1{
public static void main(String[] args){ }
}
Run Code Online (Sandbox Code Playgroud)
错误 - 找不到构造函数Person().为什么定义Person()是必要的?