Oop 面向对象编程中的委托示例:需要澄清

Oop 面向对象编程中的委托示例:需要澄清,oop,delegation,Oop,Delegation,摘自: 调用b.foo()将导致打印b.bar,因为此 指在上下文中的原始接收方对象b a 有人能澄清一下上面的解释吗?我想我理解基本原理,但我仍然不确定为什么它会从类B调用foo()方法。wikipedia的文章()正在讨论一个名为“delegation”的功能,这是我从未体验过的 引用您引用的维基百科文章#1: 调用b.foo()将导致打印b.bar,因为它引用了a上下文中的原始接收方对象b 它们基本上覆盖了类A中的“this”,因此它恰好指向类B的一个实例。我怀疑这种覆盖是临时的,可能会针

摘自:

调用
b.foo()
将导致打印
b.bar
,因为
指在上下文中的原始接收方对象
b
a

有人能澄清一下上面的解释吗?我想我理解基本原理,但我仍然不确定为什么它会从类
B

调用
foo()
方法。wikipedia的文章()正在讨论一个名为“delegation”的功能,这是我从未体验过的

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

它们基本上覆盖了类A中的“this”,因此它恰好指向类B的一个实例。我怀疑这种覆盖是临时的,可能会针对类A的方法从一个调用变为另一个调用。如果同一个实例作为类B、C和D的“委托”使用不同的“bar()”实现,那么很幸运地知道类a的给定实例将要做什么

如果“this”在运行时是任意的类B或(类C…类Z),我不想成为负责编写类A的程序员;这感觉像是一个研究主题,对于一篇论文来说很好,但对于每天工作的编程来说可能没什么帮助。我有兴趣阅读stackexchange人群提供的任何他们认为这种行为有用的例子

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

我同意[需要引证],我很想听到任何支持这一点的语言

Java输出示例 请注意,Java并不像Wikipedia文章所描述的那样工作。 在源代码中使用“delegate”一词可能毫无意义,因为它具有误导性,至少在维基百科文章概念的意义上是如此。但我保留了它,以便更容易地将维基百科示例“代码”与以下内容进行比较

$ 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
$ 
$javab
>B(),加入delegateA@2a139a55
A.foo(),调用此.bar()
A.bar():A.bar的您好
B.foo(),调用a.foo()
>A.foo(),调用此.bar()
A.bar():A.bar的您好
现在。。。作为一个思维实验,如果java实现了这种“委托”,那么输出的最后一部分(我认为)会更像这样:

//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
$ 
//这根本不是java的实际输出,
//我只是编辑了一下,让它看起来像
//我认为维基百科式的“授权”会起到什么作用。
-----b.foo()---
>B.foo(),调用a.foo()
>A.foo(),调用此.bar()
B.bar():hello from B.bar//这里与上面的实际java输出有很大的不同。
甲级
A类{
void foo(){
System.out.println(“>A.foo(),调用this.bar()”;
这个.bar();
System.out.println(“
B类
B类{
//私有委托A;//委托链接
//上面这一行不是用Java编译的,“委托”不是关键字。
private A;//委托链接,不带“delegate”关键字。
公共图书馆B(A){
System.out.println(“>B(),添加委托“+a+”);
这个a=a;
System.out.println(“B.foo(),调用a.foo()”;
a、 foo();//在a实例上调用foo()
System.out.println(“
  • b.foo()
    调用
    this.a.foo()
  • 由于
    b.a
    被声明为
    delegate
    因此在
    b
    的上下文中调用了
    this.a.foo()

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

    //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 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");
      }
    }
    
    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();
       }
    }