Tôi đã đi loanh quanh với Kotlin gần đây, và thật tuyệt vời!Làm thế nào để làm cho Kotlin dừng lập luận đối với lớp sai (Giao diện)
Tôi đã làm việc với thư viện Twitter4j để thử một số nội dung với API Twitter. Tôi đã viết mã này trong Kotlin
object Demo {
private val twitterStream = TwitterStreamFactory().instance
@JvmStatic
fun main(args: Array<String>) {
val listener = object : StatusListener {
override fun onStallWarning(warning: StallWarning?) {
println("Got stall warning:" + warning)
}
override fun onScrubGeo(userId: Long, upToStatusId: Long) {
println("Got scrub_geo event userId:$userId upToStatusId:$upToStatusId")
}
override fun onStatus(status: Status) {
println("@" + status.user.screenName + " - " + status.text)
}
override fun onDeletionNotice(statusDeletionNotice: StatusDeletionNotice) {
println("Got a status deletion notice id:" + statusDeletionNotice.statusId)
}
override fun onTrackLimitationNotice(numberOfLimitedStatuses: Int) {
println("Got track limitation notice:" + numberOfLimitedStatuses)
}
override fun onException(ex: Exception) {
ex.printStackTrace()
}
}
twitterStream.addListener(listener)
twitterStream.sample()
}
}
nhưng mỗi lần tôi chạy nó, tôi nhận exception in thread "main" java.lang.IllegalAccessError: tried to access class twitter4j.StreamListener from class co.enoobong.eno.twitter.bot.Demo at co.enoobong.eno.twitter.bot.Demo.main(Demo.kt:63)
Sau khi điều tra thêm, Tools-> Kotlin-> Hiển thị Kotlin Bytecode. Tôi dịch ngược sang Java, chỉ để khám phá ra rằng đây là những gì đã được tạo ra.
@JvmStatic
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
<undefinedtype> listener = new StatusListener() {
public void onStallWarning(@Nullable StallWarning warning) {
String var2 = "Got stall warning:" + warning;
System.out.println(var2);
}
public void onScrubGeo(long userId, long upToStatusId) {
String var5 = "Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId;
System.out.println(var5);
}
public void onStatus(@NotNull Status status) {
Intrinsics.checkParameterIsNotNull(status, "status");
String var2 = "@" + status.getUser().getScreenName() + " - " + status.getText();
System.out.println(var2);
}
public void onDeletionNotice(@NotNull StatusDeletionNotice statusDeletionNotice) {
Intrinsics.checkParameterIsNotNull(statusDeletionNotice, "statusDeletionNotice");
String var2 = "Got a status deletion notice id:" + statusDeletionNotice.getStatusId();
System.out.println(var2);
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
String var2 = "Got track limitation notice:" + numberOfLimitedStatuses;
System.out.println(var2);
}
public void onException(@NotNull Exception ex) {
Intrinsics.checkParameterIsNotNull(ex, "ex");
ex.printStackTrace();
}
};
twitterStream.addListener((StreamListener)listener);
twitterStream.sample();
}
Kotlin đã cố gắng để cast nghe để StreamListener
đó là tin trong thư viện (StatusListener mở rộng nó) do đó IllegalAccessError
Bất kỳ ý tưởng làm thế nào để giải quyết được không?
PS: Đây là một phiên bản Java làm việc của mã - https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/stream/PrintSampleStream.java
PPS: Tôi đang sử dụng Kotlin 1.1.4, trên IntelliJ IDEA 2017.2.2
Cảm ơn bạn đã phản hồi @Kiskae. Tôi sẽ thử cái này. Trên thực tế nó hoạt động hoàn hảo trong Java vì vậy không cần cho một fixer – Enoobong
Làm việc như quyến rũ tốt quá. Rất cám ơn – Enoobong