本文介绍了java中的继承和委托有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继承和继承之间有什么区别java中的委托?

What is the difference between inheritance and delegation in java?

如何在我的项目中使用以下示例?请你帮我指导一下。我知道继承但对代表团知之甚少。所以,请给出正确的理由。我为什么要用它?

How to use the following example in my project? Please can you guide me with delegation. I know about inheritance but do not have much knowledge about delegation. So, please give a proper reason. Why should I use this?

 package com.m;

 class RealPrinter { // the "delegate"
     void print() { 
      System.out.println("something"); 
    }
 }

 class Printer { // the "delegator"
     RealPrinter p = new RealPrinter(); // create the delegate 
     void print() { 
     p.print(); // delegation
     } 
 }

 public class Tester {

// to the outside world it looks like Printer actually prints.
     public static void main(String[] args) {
        Printer printer = new Printer();
        printer.print();
     }

   }


推荐答案

当你委托时,你只是在调用一些知道必须做什么的课程。你并不真正关心 它是如何做到的,所有你关心的关于你所调用的类知道需要做什么。

When you delegate, you are simply calling up some class which knows what must be done. You do not really care how it does it, all you care about is that the class you are calling knows what needs doing.

如果我是你,虽然我会创建一个界面,并将其命名为 IPrinter (或沿着这些界限)有一个名为 print 的方法。然后我将 RealPrinter 实现此接口。最后,我会改变这一行: RealPrinter p = new RealPrinter(); to this: IPrinter p = new RealPrinter()

If I were you though I would make an interface and name it IPrinter (or something along those lines) which has one method named print. I would then make RealPrinter implement this interface. Finally, I would change this line: RealPrinter p = new RealPrinter(); to this: IPrinter p = new RealPrinter().

由于 RealPrinter 实现 IPrinter ,我确定它有一个 print 方法。然后,我可以使用此方法通过将其委派给适当的类来更改应用程序的打印行为。

Since RealPrinter implements IPrinter, then I know for sure that it has a print method. I can then use this method to change the printing behaviour of my application by delegating it to the appropriate class.

这通常允许更大的灵活性,因为您没有将行为嵌入到特定的类中,而是将其留给另一个类。

This usually allows for more flexibility since you do not embed the behaviour in your specific class, but rather leave it to another class.

在这种情况下,要更改应用程序的打印行为,您只需要创建另一个实现 IPrinter 然后改变这一行: IPrinter p = new RealPrinter(); to IPrinter p = new MyOtherPrinter();

In this case, to change the behaviour of your application with regards to printing, you just need to create another class which implements IPrinter and then change this line: IPrinter p = new RealPrinter(); to IPrinter p = new MyOtherPrinter();.

public interface IPrinter {
    void print();
}

public class RealPrinter implements IPrinter {

    @Override
    public void print() {
        System.out.println("This is RealPrinter");
    }
}

public class RealPrinter2 implements IPrinter {

    @Override
    public void print() {
        System.out.println("This is RealPrinter2");
    }
}

public class Main {

    public static void main(String...arg){
        IPrinter printer  = new RealPrinter();
        printer.print();

        printer = new RealPrinter2();
        printer.print();
    }
}

这篇关于java中的继承和委托有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 22:28