相关疑难解决方法(0)

java中多级继承中构造函数调用的顺序

//: 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)

java inheritance constructor

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

本Java代码中构造函数的顺序是什么?

这是代码,我定义了两个名为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)

java

6
推荐指数
2
解决办法
365
查看次数

JUnit BeforeAll 与静态块 - 执行顺序

我有一个简单的问题。我有这样的类结构:基类和子类(关系是继承)。@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 我正在搜索信息,但我找不到任何有关它的信息。

java junit

6
推荐指数
1
解决办法
1853
查看次数

Java中子类的构造函数

在编译这个程序时,我得到错误 -

 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()是必要的?

java constructor superclass

4
推荐指数
1
解决办法
4万
查看次数

标签 统计

java ×4

constructor ×2

inheritance ×1

junit ×1

superclass ×1