在接口内创建类和在类内创建接口有什么用

发布于 2024-11-06 04:43:26 字数 121 浏览 0 评论 0原文

我想知道将类放置在接口中和将接口放置在类中需要什么?

class A {
   interface B {}
}

interface D {
   class E {}
} 

I want to know what is the need of placing a class inside interface and an interface inside class?

class A {
   interface B {}
}

interface D {
   class E {}
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷情妓 2024-11-13 04:43:26

这是我从某个链接复制并粘贴的(我之前做过并与您分享)
也许这可以帮助你一点。

1)

interface employee{
class Role{
      public String rolename;
      public int roleId;
 }
Role getRole();
// other methods
 }

在上面的接口中,您将角色类型强绑定到员工接口(employee.Role)。
2)使用接口内的静态类,您可以缩短公共编程片段:检查对象是否是接口的实例,如果是,则调用该接口的方法。看这个例子:

  public interface Printable {
    void print();

    public static class Caller {
        public static void print(Object mightBePrintable) {
                if (mightBePrintable instanceof Printable) {
                        ((Printable) mightBePrintable).print();
                }
        }
    }
}

现在

  void genericPrintMethod(Object obj) {
    if (obj instanceof Printable) {
        ((Printable) obj).print();
    }
}

你可以写:

   void genericPrintMethod(Object obj) {
         Printable.Caller.print(obj);
    }

This i am copying and pasting from some link (i earlier did and sharing with you)
May be that can help you a bit.

1)

interface employee{
class Role{
      public String rolename;
      public int roleId;
 }
Role getRole();
// other methods
 }

In the above interface you are binding the Role type strongly to the employee interface(employee.Role).
2) With a static class inside an interface you have the possibility to shorten a common programming fragment: Checking if an object is an instance of an interface, and if so calling a method of this interface. Look at this example:

  public interface Printable {
    void print();

    public static class Caller {
        public static void print(Object mightBePrintable) {
                if (mightBePrintable instanceof Printable) {
                        ((Printable) mightBePrintable).print();
                }
        }
    }
}

Now instead of doing this:

  void genericPrintMethod(Object obj) {
    if (obj instanceof Printable) {
        ((Printable) obj).print();
    }
}

You can write:

   void genericPrintMethod(Object obj) {
         Printable.Caller.print(obj);
    }
痴情 2024-11-13 04:43:26

组织。

在接口内指定一个类将该类直接与该接口联系起来 - 使用该接口的客户端将有权访问该类及其提供的所有功能。

我只在 Java 1.4 及更低版本中看到过接口内类的模式,以提供与接口一起使用的枚举类型 - 因为接口只能使用该类,并且该类可以受到保护,使用的客户端接口只能接受接口中定义的类的实例作为枚举值。这只是我能想到的唯一例子 - 我确信还存在其他例子,但我很少看到使用接口内部的类。

对于翻转的情况,它仍然是组织。在类内部指定接口意味着只有该类应该使用该接口。其他类和接口仍然可以使用该接口,具体取决于其访问级别,这并不是重点 - 组织记录了接口的意图 - 仅在包含该接口的类中使用。

如果它在该类之外有用,则应将其适当地移至其自己的类型。因此,这两种用途都很罕见,但它们的用途主要是通过 Java 语法直接组织代码并记录其意图。

Organization.

Specifying a class inside an interface ties that class directly to that interface - clients which use that interface will have access to that class and all the functionality that it provides.

I've seen the pattern of a class inside an interface really only in Java 1.4 and lower to provide an enumerated type to go along with the interface - since the interface could only use the class, and the class can be protected, clients that use the interface could only accept the instances of the class defined in the interface as the enumerated values. That's just the only example I can come up with - I'm sure others exist, but it's rare that I see a class inside of an interface used.

For the flipped case, it's still organization. Specifying an interface inside of a class signifies that only that class should use the interface. That other classes and interfaces can still use that interface, depending on its access level, is not the point - the organization documents the intent of the interface - to be used only in the class that contains it.

If it is useful outside of that class, it should be moved appropriately to its own type. So, both of these uses are rare, but their use is primarily to organize the code and document its intent directly via Java syntax.

一口甜 2024-11-13 04:43:26

如果类的功能与接口密切相关,并且我们不会在任何地方使用该类,那么我们可以在接口中定义类。

package PracticeTest;

public interface VehicleService {
   public void repair(Vehicle v);
   public class Vehicle{
      String vehicleModel;
      String vehicleNumber;
      public Vehicle(String vehicleModel, String vehicleNumber) {
      super();
      this.vehicleModel = vehicleModel;
      this.vehicleNumber = vehicleNumber;
      }

   }
}

在上述情况下,车辆类可用于 VehicleService,但我们不会在其他地方使用它。

  1. 为了提供接口的默认实现,我们可以在接口内定义一个类

接口

package PracticeTest;

public interface VehicleService {
public void repair();
public class DefaultVehicle implements VehicleService{

    @Override
    public void repair() {          
        System.out.println(" Default Repair");
    }
}

实现类

 package PracticeTest;
 public class busRepair implements VehicleService{

   @Override
   public void repair() {
       System.out.println(" Bus Repair");
   }
   public static void main(String args[]){
        busRepair b = new busRepair();
        b.repair();
        DefaultVehicle d = new DefaultVehicle();
        d.repair();
    }
 }

If functionality of a class is closely associated with an interface and we are not going to use that class anywhere then we can define a class inside an interface.

package PracticeTest;

public interface VehicleService {
   public void repair(Vehicle v);
   public class Vehicle{
      String vehicleModel;
      String vehicleNumber;
      public Vehicle(String vehicleModel, String vehicleNumber) {
      super();
      this.vehicleModel = vehicleModel;
      this.vehicleNumber = vehicleNumber;
      }

   }
}

In above case vehicle class is available for VehicleService and we are not using it anywhere else.

  1. To provide default implementation of an interface we can define a class inside an interface

Interface

package PracticeTest;

public interface VehicleService {
public void repair();
public class DefaultVehicle implements VehicleService{

    @Override
    public void repair() {          
        System.out.println(" Default Repair");
    }
}

Implementation Class

 package PracticeTest;
 public class busRepair implements VehicleService{

   @Override
   public void repair() {
       System.out.println(" Bus Repair");
   }
   public static void main(String args[]){
        busRepair b = new busRepair();
        b.repair();
        DefaultVehicle d = new DefaultVehicle();
        d.repair();
    }
 }
未蓝澄海的烟 2024-11-13 04:43:26

在这种情况下,如果任何类功能与任何接口密切相关,我们就在接口内声明一个类。

有时,在接口内部提供抽象方法的默认实现也很有用。

If any class functionality is closely associate with any interface in that situation we declare a class inside an interface.

Some time it is also useful to provide default implementation of abstract method inside interface.

绅刃 2024-11-13 04:43:26

在接口内部创建类允许程序员对类进行限制,即只有当我们实现该接口时才能访问该内部类。

creating class inside interface allow programmer to have restrictions on class i.e., that inner class is accessible only if we implement that interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文