语言界面 [英] interfaces in languages

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

问题描述

嗨.....

谁能以一种非常简单和描述性的方式向我解释有关接口的信息.我知道接口可以替代多重继承.但是接口如何避免出现钻石问题和歧义错误.请在理论上和实践上都进行解释.

Hi.....

Can anybody explain me about interfaces in a very simple and descriptive manner. I know that interfaces is a substitute for multiple inheritence. But how interfaces saved from diamond problem and ambiguity error..Please explian in theory and practical manner both.

推荐答案

^ ]

完成这一点:它是对象的表示.

例如:

您想提供向系统输入信息的可能性-您可以授予完全访问权限:

What is an Interface? @ oracle.com[^]

to complete that: it''s a representation of an object.

So for example:

You want to provide the possibility of making input to your system - you could grant complete access:

public class MyObject(){
  public myobject(){
  }

  public SecretData returnSecret(){
    return oSecretData;
  }

}



但这不是最好的主意.您最好创建一个仅表示另一端所需功能的接口:



But that''s not the best idea. You better create an interface just representing the functions the other side needs:

public Interface IMyObject(){
  
 public OfficalValue getOfficalValue();

} 


public class MyObject() implements IMyObject{
  IMyValue oMyValue;
  public myobject(){
    oMyValue = this;
  }

  public IMyObject grantAccess(){
    return oMyValue;// interface
  }

  private SecretData returnSecret(){
    return oSecretData;
  }

  @Override
  public OfficalValue getOfficalValue(){
    return oOfficalValue;
  }
}




接口也可以是各种对象的通用基础.所有对象都在实现的接口是对所有对象具有相同访问权限的保证.
在此示例中,所有车辆都能够停车和驾驶-只有拖拉机能够拉拖车:




An interface can also be a common base for a variation of objects. The interface, which all objects are implementing is the guarantee to have the same access to all of the objects.
In this example all Vehicles are able to park and drive - only the tractor is able to pull a trailer:

public class RoadTraffic{

   public void moveVehicles(Object oParticipatingObject){
     if(oParticipatingObject instanceof IVehicle){ // making sure it's a Vehicle
      IVehicle oVehicle = (IVehicle) oParticipatingObject;
      oVehicle.drive();
      if(oVehicle instanceof Tractor){
        ((Tractor)oVehicle).pullTrailer();
       }
     }
   }
}



public Interface IVehicle{

  public void park();
  public void drive();

}

public class Car implements IVehicle{

  public void park(){ }
  public void drive(){ }
}

public class Tractor implements IVehicle{
  public void park(){ }
  public void drive(){ }

  // addition function
  public void pullTrailer(){

  }
}





其他好的用途是在其中存储静态值-例如ID和在不同对象中使用的常用值:





Other good uses are to store static values in there - like ID''s and common used values that are used in different objects:

public interface IConstants{
  public static final String 
     GEAR = "",
     LIGHT = "",
     ...
}

public class MyObject{

  public void drive(){
    if(IConstants.LIGHT.equals(...){
      // dofunnythings();
    }
  }


}



我希望我在这里不会错过太多,也不会在这里写下很多失败.



I hope I didn''t miss to much and didn''t write to much fails in here.


这是一个面试问题吗?
在google中搜索,您会发现许多有用的链接,这些链接解释了界面.
Is this an interview question?
Search google, you will find many helpful links explaining about interface.


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

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