专用接口 [英] Private Interfaces

查看:143
本文介绍了专用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在代码中使用私有接口的方法?

How can we use the methods of a private interface in our code?

抽象类是无法实例化的东西。所以,如果我们需要使用抽象类的方法,我们可以继承它们并使用它们的方法。

Abstract classes are something which cannot be instantiated. So, if we need to use methods of abstract class, we can inherit them and use their methods.

但是,当我们讨论接口时,我们需要实现它们使用他们的方法。

But, when we talk about interfaces, we need to implement them to use their methods.

推荐答案

private 关键字的意思是同一类:

The private keyword means "anyone in the same class":

public class Foo {

   private interface X {...}
   private class X1 implements X {...}
}

这意味着所有类在 Foo 内声明可以使用接口 Foo.X

This means all classes declared inside of Foo can use the interface Foo.X.

命令模式的常见用例其中 Foo 接受字符串并将它们转换为内部命令对象,这些对象都实现相同的接口。

A common use case for this is the command pattern where Foo accepts, say, strings and converts them into internal command objects which all implement the same interface.

如果您将第二个类 Bar 添加到文件 Foo.java ,那么它无法看到 Foo.X

If you add a second class Bar to the file Foo.java, then it can't see Foo.X.

这篇关于专用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆