在java中使用接口的原因是什么?
今年夏天我刚上了一些java课程,一切都进行得很顺利,但我被困在了接口中。我知道该接口允许我们将方法与类(如文件)组合在一起,但我们什么时候才能真正在实际编程中使用它呢。我只是有点困惑。有定义的示例将不胜感激。从我的角度来看,界面表示某个特定时间要执行的操作 例如,按下硬件键时会调用在java中使用接口的原因是什么?,java,android,Java,Android,今年夏天我刚上了一些java课程,一切都进行得很顺利,但我被困在了接口中。我知道该接口允许我们将方法与类(如文件)组合在一起,但我们什么时候才能真正在实际编程中使用它呢。我只是有点困惑。有定义的示例将不胜感激。从我的角度来看,界面表示某个特定时间要执行的操作 例如,按下硬件键时会调用onKeyDown接口。这里“按下”是一个特定的时间,那么我们应该在那个时间做什么呢?只需调用onKeyDown即可 另一个优点是,通过接口,我们不需要知道真实的类实例是什么,只要知道有一个方法可以调用就足够了。在我
onKeyDown
接口。这里“按下”是一个特定的时间,那么我们应该在那个时间做什么呢?只需调用onKeyDown
即可
另一个优点是,通过接口,我们不需要知道真实的类实例是什么,只要知道有一个方法可以调用就足够了。在我看来,接口表示在某个特定时间执行的操作 例如,按下硬件键时会调用
onKeyDown
接口。这里“按下”是一个特定的时间,那么我们应该在那个时间做什么呢?只需调用onKeyDown
即可
另一个优点是,通过接口,我们不需要知道任何关于真实类实例的信息,只要知道有一个方法可以调用就足够了
接口允许我们使用类(如文件)对方法进行分组
接口就是合同。它缺乏执行力,缺乏方法。但它规定了什么
这样做的好处是,它使我们能够交换不同的实现(或解决方案),而无需将更改级联到接口使用的所有位置
例如,看一下JDK库。它们提供了一个列表接口,以及一些可选的实现,这些实现提供了在不同情况下更好的不同实现。例如,LinkedList和ArrayList都实现了List,但它们的性能非常不同。有时候LinkedList比ArrayList好,有时候正好相反
接口允许我们使用类(如文件)对方法进行分组
接口就是合同。它缺乏执行力,缺乏方法。但它规定了什么
这样做的好处是,它使我们能够交换不同的实现(或解决方案),而无需将更改级联到接口使用的所有位置
例如,看一下JDK库。它们提供了一个列表接口,以及一些可选的实现,这些实现提供了在不同情况下更好的不同实现。例如,LinkedList和ArrayList都实现了List,但它们的性能非常不同。有时候LinkedList比ArrayList好,有时候正好相反
但我们什么时候才能真正在真正的编程中使用它呢
对我来说,我一直使用的最简单和最好的方法是回调模式
,在大多数情况下它都在侦听器中使用
下面是一个回调模式的简单示例
interface CallBack {
void methodToCallBack();
}
class CallBackImpl implements CallBack {
public void methodToCallBack() {
System.out.println("I've been called back");
}
}
class Caller {
public void register(CallBack callback) {
callback.methodToCallBack();
}
public static void main(String[] args) {
Caller caller = new Caller();
CallBack callBack = new CallBackImpl();
caller.register(callBack);
}
}
以更具体的方式:
// Reverse geocoding may take a long time to return so we put it in AsyncTask.
public class ReverseGeocoderTask extends AsyncTask<Void, Void, List<Address>> {
private static final String TAG = "ReverseGeocoder";
private Geocoder mGeocoder;
private float mLat;
private float mLng;
private Callback mCallback;
public ReverseGeocoderTask(Geocoder geocoder, float[] latlng,
Callback callback) {
mGeocoder = geocoder;
mLat = latlng[0];
mLng = latlng[1];
mCallback = callback;
}
@Override
protected List<Address> doInBackground(Void... params) {
List<Address> address = null;
try {
List<Address> address =
mGeocoder.getFromLocation(mLat, mLng, 1);
} catch (Exception ex) {
// ignore
}
return address;
}
@Override
protected void onPostExecute(List<Address> address) {
if(address != null)
mCallback.onComplete(address);
}
}
// the interface
public interface Callback {
public void onComplete(List<Address> address);
}
// example fragment
public class MyCallbackFragment extends Fragment implements Callback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
// params the geocoder, float[] latlng, and the class that implements Callback
new ReverseGeocoderTask(new Geocoder(getActivity(), Locale.getDefault()) , latlng, this).execute(null,null,null);
}
...
@Override
public void onComplete(List<Address> address){
// now we can get the address not interupting the main thread
// and also let the Task call us back to retrieve the information
// and use it.
// you can then use the address to display it anywhere you want in your UI
}
}
//反向地理编码可能需要很长时间才能返回,因此我们将其放在AsyncTask中。
公共类ReverseGeoCodeTask扩展了AsyncTask
但我们什么时候才能真正在真正的编程中使用它呢
对我来说,我一直使用的最简单和最好的方法是回调模式
,在大多数情况下它都在侦听器中使用
下面是一个回调模式的简单示例
interface CallBack {
void methodToCallBack();
}
class CallBackImpl implements CallBack {
public void methodToCallBack() {
System.out.println("I've been called back");
}
}
class Caller {
public void register(CallBack callback) {
callback.methodToCallBack();
}
public static void main(String[] args) {
Caller caller = new Caller();
CallBack callBack = new CallBackImpl();
caller.register(callBack);
}
}
以更具体的方式:
// Reverse geocoding may take a long time to return so we put it in AsyncTask.
public class ReverseGeocoderTask extends AsyncTask<Void, Void, List<Address>> {
private static final String TAG = "ReverseGeocoder";
private Geocoder mGeocoder;
private float mLat;
private float mLng;
private Callback mCallback;
public ReverseGeocoderTask(Geocoder geocoder, float[] latlng,
Callback callback) {
mGeocoder = geocoder;
mLat = latlng[0];
mLng = latlng[1];
mCallback = callback;
}
@Override
protected List<Address> doInBackground(Void... params) {
List<Address> address = null;
try {
List<Address> address =
mGeocoder.getFromLocation(mLat, mLng, 1);
} catch (Exception ex) {
// ignore
}
return address;
}
@Override
protected void onPostExecute(List<Address> address) {
if(address != null)
mCallback.onComplete(address);
}
}
// the interface
public interface Callback {
public void onComplete(List<Address> address);
}
// example fragment
public class MyCallbackFragment extends Fragment implements Callback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
// params the geocoder, float[] latlng, and the class that implements Callback
new ReverseGeocoderTask(new Geocoder(getActivity(), Locale.getDefault()) , latlng, this).execute(null,null,null);
}
...
@Override
public void onComplete(List<Address> address){
// now we can get the address not interupting the main thread
// and also let the Task call us back to retrieve the information
// and use it.
// you can then use the address to display it anywhere you want in your UI
}
}
//反向地理编码可能需要很长时间才能返回,因此我们将其放在AsyncTask中。
公共类ReverseGeoCodeTask扩展了AsyncTask接口
也可以用于应用程序编程接口(API)。接口
也可以用于应用程序编程接口(API)。