【问题标题】:What is the use of creating a class inside interface and interface inside class在接口里面创建类和在类里面创建接口有什么用
【发布时间】:2011-08-21 09:49:13
【问题描述】:

我想知道在接口里面放一个类,在类里面放一个接口有什么需要?

class A {
   interface B {}
}

interface D {
   class E {}
} 

【问题讨论】:

标签: java class interface


【解决方案1】:

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

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);
    }

【讨论】:

    【解决方案2】:

    组织。

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

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

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

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

    【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 不,这不是真的,我们也可以在不实现的情况下访问内部类 - InterfaceName.ClassName a= new InterfaceName.ClassName ();a.print(10);
        猜你喜欢
        • 2015-03-10
        • 2016-10-29
        • 1970-01-01
        • 1970-01-01
        • 2016-12-30
        • 1970-01-01
        • 2020-05-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多