尝试用 Java 模拟其他 class 中的线程行为

Trying to simulate Thread behavior in orher class with Java

我有这个class:

public class MyThread {
    public static void main(String[] args) {
        Thread thread = new Thread() {
            public void run() {
                System.out.print("Test from Anonymous Class!");
            }
        };
        Thread newThread = new Thread(thread);
        newThread.run();
    }
}

当我运行这个程序时,我得到Test from Anonymous Class!

现在,我正在尝试用另一个 class 模拟此行为,如下所示:

interface MyInterface {
    public void doTest();
}

class MyClass implements MyInterface {
    public MyClass() {}

    public MyClass(MyInterface myInterface) {}

    public void doTest() {
        System.out.println("Test from MyClass!");
    }
}

public class Test {
    public static void main(String[] args) {
        MyClass myClass1 = new MyClass() {
            public void doTest() {
                System.out.println("Test from Anonymous Class!");
            }
        };

        MyClass myClass2 = new MyClass(myClass1);
        myClass2.doTest();
    }
}

当我运行这个程序时,我得到Test from MyClass!。为什么在第一个示例中打印出 Test from Anonymous Class!?我怎样才能得到与 MyClass class 相同的行为?

提前致谢!

那是因为你没有对你的参数做任何事情 myClass1

public MyClass(MyInterface myInterface) {}

你得到了参数,那又怎样?如果你想做参数做的事情,你必须调用方法:

myClass1.doTest()


>"Test from Anonymous Class!"

你所做的很少见,但如果你从正确的对象调用方法,你会得到你想要的:)

另一种罕见但有效的方法是拥有一个实例变量,并调用它:

class MyClass implements MyInterface {
    MyClass myOtherClass;

    public MyClass() {}

    public MyClass(MyInterface myInterface) {
       this.myOtherClass = myInterface;
    }

    public void doTest() {
        System.out.println("Test from MyClass!");
    }
}

然后,你调用里面的方法:

public class Test {
    public static void main(String[] args) {
        MyClass myClass1 = new MyClass() {
            public void doTest() {
                System.out.println("Test from Anonymous Class!");
            }
        };

        MyClass myClass2 = new MyClass(myClass1);
        myClass2.myOtherClass.doTest(); // calling method from myClass1
    }
}

您需要初始化您的目标,因此它将调用您 class

中的方法
class MyClass implements MyInterface {
        MyInterface myInterface;
        public MyClass() {}

        public MyClass(MyInterface myInterface) {
            this.myInterface=myInterface;
        }

        public void doTest() {
            if(myInterface !=null){
                myInterface.doTest();
                return;
            }
            System.out.println("Test from MyClass!");
        }
    }

您似乎想实现一个 class 的委托,该委托将接口作为构造函数的参数。

当您调用 Thread#start() 而您的自定义 class 不模仿此行为时,Thread 构造函数使用作为参数提供的 Runnable 实例作为执行目标.你确实对传递的 MyInterface 参数没有做任何事情:

public MyClass(MyInterface myInterface) {}

要实现委托,你应该在MyClass中添加一个MyInterface字段,并在构造函数中对其赋值。
然后在doTest()方法中使用它。

public MyClass(MyInterface myInterface) {}

  private MyInterface myInterface;
  ...
  public MyClass(MyInterface myInterface) {
    this.myInterface = myInterface;
  }

   public void doTest() {
       // do some processing
         ..
       // then delegate
      myInterface.doTest();
   }
}

答案很简单。 Thread 中的 运行() 方法被覆盖,它将始终是 运行.

的方法

在第二个示例中,myClass2 使用实例 doTest() 方法,而 myClass1 从未使用过,除了在构造函数中。