面向对象编程中的委托示例:需要说明

Example of delegation in object-oriented programming: Clarification needed

取自https://en.wikipedia.org/wiki/Delegation_(programming):

class A {
  void foo() {
    // "this" also known under the names "current", "me" and "self" in other languages
    this.bar();
  }

  void bar() {
    print("a.bar");
  }
};

class B {
  private delegate A a; // delegation link

  public B(A a) {
    this.a = a;
  }

  void foo() {
    a.foo(); // call foo() on the a-instance
  }

  void bar() {
    print("b.bar");
  }
};

a = new A();
b = new B(a); // establish delegation between two objects

Calling b.foo() will result in b.bar being printed, since this refers to the original receiver object, b, within the context of a.

有人可以澄清上面的解释吗?我想我理解了基本原理,但我仍然不确定为什么它会从 class B.

调用 foo() 方法

维基百科文章 ( https://en.wikipedia.org/wiki/Delegation_(programming) ) 正在谈论一个名为 "delegation" 的功能,这是我没有经历过的。

引用您引用的维基百科文章中的#1: “调用 b.foo() 将导致打印 b.bar,因为它指的是原始接收者对象 b,在 a. 的上下文中”

它们基本上覆盖了 class A 中的 "this",因此它恰好指向 Class B 的实例。我怀疑覆盖是临时的,可能会因调用而改变调用 Class A 的方法。如果 class A 的给定实例作为 Class B、C 和 D 的 "delegate" 都具有不同的实现,祝您好运"bar()".

我不想成为负责编写 Class A 的程序员,如果 "this" 是任意的 class B 或 (class C ... class Z) 在运行时;感觉像是一个研究课题,对论文很有帮助,但对日常工作编程可能没有太大帮助。我有兴趣阅读 stackexchange 人群可以提供的任何他们发现这种行为有帮助的例子。

引用您引用的维基百科文章中的#2: “一般的编程语言不支持这种不寻常的委托形式作为语言概念,但也有一些例外[需要引用]”。 =15=]

我同意[需要引用],我很想听听任何实际支持这一点的语言。

样本Java输出

请注意,Java 与维基百科文章描述的方式完全不同。 在源代码中使用 "delegate" 这个词可能毫无意义,因为它具有误导性,至少在维基百科文章概念的意义上是这样。但我把它留在里面是为了更容易将维基百科示例 "code" 与以下内容进行比较。

$ java B
> B(), adding delegateA@2a139a55
< B()
----- a.foo() ---
> A.foo(), calling this.bar()
A.bar(): hello from a.bar
< A.foo(), back from this.bar()
----- a.bar() ---
A.bar(): hello from a.bar
----- b.foo() ---
> B.foo(), calling a.foo()
> A.foo(), calling this.bar()
A.bar(): hello from a.bar
< A.foo(), back from this.bar()
< B.foo(), back from  a.foo()
----- b.bar() ---
B.bar(): hello from b.bar
$ 

现在......作为一个思想实验,如果 java 实现了那种 'delegation' 输出的最后一部分看起来(我认为)更像这样:

//This isn't actual output from java at all,
//I just edited this trying to make it look like
//what I think the wikipedia-style "delegation" would do.
----- b.foo() ---
> B.foo(), calling a.foo()
> A.foo(), calling this.bar()
B.bar(): hello from b.bar  // Big difference here vs. actual java output above.
< A.foo(), back from this.bar()
< B.foo(), back from  a.foo()
----- b.bar() ---
B.bar(): hello from b.bar
$ 

class一个

class A {
  void foo() {
    System.out.println("> A.foo(), calling this.bar()");
    this.bar();
    System.out.println("< A.foo(), back from this.bar()");
  }

  void bar() {
    System.out.println("A.bar(): hello from a.bar");
  }
}

classB

class B {
  // private delegate A a; // delegation link
  // above line doesn't compile in Java, "delegate" not a key word.
  private A a; // delegation link, without "delegate" key word.

  public B(A a) {
    System.out.println("> B(), adding delegate"+a+"");
    this.a = a;
    System.out.println("< B()" );
  }

  void foo() {
    System.out.println("> B.foo(), calling a.foo()");
    a.foo(); // call foo() on the a-instance
    System.out.println("< B.foo(), back from  a.foo()");
  }

  void bar() {
    System.out.println("B.bar(): hello from b.bar");
  }

   public static void main( String args[] ) {
      A a = new A();
      B b = new B(a);
      System.out.println("----- a.foo() ---");
      a.foo();
      System.out.println("----- a.bar() ---");
      a.bar();
      System.out.println("----- b.foo() ---");
      b.foo();
      System.out.println("----- b.bar() ---");
      b.bar();
   }
}
  1. b.foo() 呼叫 this.a.foo()

因为 b.a 被声明为 delegated,所以 this.a.foo()b

的上下文中被调用
  1. a.foo() 呼叫 this.bar()

因为 a.foo() 是在 b 的上下文中调用的,a.foo() 中的 this 关键字引用 b,而不是 a。因此,this.bar() 引用 b.bar().