Почему если, например в последнем классе (DecoratorMyRunnableImpl) закомментировать строку component.run() - то строка "RunnableImpl body" не будет выводиться?
public class Solution {
public static void main(String[] args) {
new Thread(new DecoratorRunnableImpl(new DecoratorMyRunnableImpl(new RunnableImpl()))).start();
}
public static class RunnableImpl implements Runnable {
@Override
public void run() {
System.out.println("RunnableImpl body");
}
}
public static class DecoratorRunnableImpl implements Runnable {
private Runnable component;
public DecoratorRunnableImpl(Runnable component) {
this.component = component;
}
@Override
public void run() {
System.out.print("DecoratorRunnableImpl body ");
component.run();
}
}
public static class DecoratorMyRunnableImpl implements Runnable {
private Runnable component;
public DecoratorMyRunnableImpl(Runnable component) {
this.component = component;
}
@Override
public void run() {
System.out.print("DecoratorMyRunnableImpl body ");
component.run();
}
}
}
При выполнении component.run(); происходит запуск метода поля component класса RunnableImpl:
public void run() {
System.out.println("RunnableImpl body");
}
Соответственно если его коментить, то вывода текста не произойдет.
Сборка персонального компьютера от Artline: умный выбор для современных пользователей