为什么我们在不允许的情况下创建了抽象类的实例?

时间:2016-01-18 12:53:40

标签: android abstract-class google-api-client

此人已完成:"受保护的GoogleApiClient mGoogleApiClient;"在以下代码中:

Android Maps Get Longitude and Latitude

但GoogleApiClient是一个抽象类。

如何创建它的实例?

这里提到的抽象类: https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient

是否应该扩展它,因为抽象类只能扩展?

1 个答案:

答案 0 :(得分:2)

它没有直接实例化。如果您查看您提供的链接中的代码,就会生成GoogleApiClient子类的实例:

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

我们不在此处使用new关键字,而是使用静态类build中的Builder方法

相关问题