fork download
  1. public class Main {
  2. interface A {
  3. int a();
  4. }
  5.  
  6. static class B implements A {
  7. @Override
  8. public int a() {
  9. return 1;
  10. }
  11. }
  12.  
  13. static class C implements A {
  14. @Override
  15. public int a() {
  16. return 2;
  17. }
  18. }
  19.  
  20. static class D1 implements A {
  21. @Override
  22. public int a() {
  23. return (new B()).a() + (new C()).a();
  24. }
  25. }
  26.  
  27.  
  28. static class D2 implements A {
  29. private B b;
  30. private C c;
  31.  
  32. public D2(B b, C c) {
  33. this.b = b;
  34. this.c = c;
  35. }
  36.  
  37. @Override
  38. public int a() {
  39. return b.a() + c.a();
  40. }
  41. }
  42.  
  43. public static void main(String[] args) {
  44. B b = new B();
  45. C c = new C();
  46. D1 d1 = new D1();
  47. D2 d2 = new D2(b, c);
  48.  
  49. System.out.format("B = %d%n", b.a());
  50. System.out.format("C = %d%n", c.a());
  51. System.out.format("D1 = %d%n", d1.a());
  52. System.out.format("D2 = %d%n", d2.a());
  53. }
  54. }
Success #stdin #stdout 0.1s 28248KB
stdin
Standard input is empty
stdout
B = 1
C = 2
D1 = 3
D2 = 3