gpt4 book ai didi

android - 如何避免 Kotlin 中同名冲突的类型?

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:42 26 4
gpt4 key购买 nike

我是 kotlin 的新手,我在使用一本书构建的应用程序时遇到了一些问题。我在名为 Forecast 的单独包中有两个类,我试图在其中一个使用同名类的包中定义一些函数。这本书说要将 Forecast 类作为 ModelForecast 导入,我做到了,但现在我在追踪这种类型不匹配错误的来源时遇到了问题。似乎我的 convertForecastListToDomain() 方法期待其他的东西?请帮我找出我犯的错误。如果它是非常简单的东西,我不会感到惊讶,但我仍然找不到它。

主要 Activity .kt:

package com.example.zacharymcdaniel.weatherkot

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.example.zacharymcdaniel.weatherkot.domain.Forecast
import org.jetbrains.anko.find

class MainActivity : AppCompatActivity() {
private val url: String = "http://openweathermap.org/"

private val items = listOf(
"Mon 6/23 - Sunny - 31/17",
"Tue 6/24 - Foggy - 21/8",
"Wed 6/25 - Cloudy - 22/17",
"Thur 6/26 - Rainy - 18/11",
"Fri 6/27 - Foggy - 21/10",
"Sat 6/28 - TRAPPED IN WEATHER STATION - 23/18",
"Sun 6/29 - Sunny - 20/7"
)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val forecastList: RecyclerView = find(R.id.forecast_list)
forecastList.layoutManager = LinearLayoutManager(this)
forecastList.adapter = ForecastListAdapter(items)

}

}

data class ForecastResult(val city: City, val list: List<Forecast>)

data class City(val id: Long, val name: String, val coord: Coordinates, val country: String, val population: Int)

data class Coordinates(val ion: Float, val lat: Float)

data class Forecast(val dt: Long, val temp: Tempurature, val pressure: Float,
val humidity: Int, val weather: List<Weather>,
val speed: Float, val deg: Int, val clouds: Int,
val rain: Float) //<----this is supposed to be Forecast in second page

data class Tempurature(val day: Float, val min: Float, val max: Float,
val night: Float, val eve: Float, val morn: Float)

data class Weather(val id: Long, val main: String, val desciption: String, val icon: String)

域包中的domain.kt:

package com.example.zacharymcdaniel.weatherkot.domain

import com.example.zacharymcdaniel.weatherkot.Forecast
import com.example.zacharymcdaniel.weatherkot.ForecastResult
import java.text.DateFormat
import java.util.*
import com.example.zacharymcdaniel.weatherkot.domain.Forecast as
ModelForecast

public interface Command<T>{
fun execute(): T
}

data class ForecastList(val city: String, val country: String, val dailyForecast: List<ModelForecast>)

data class Forecast(val date: String, val description: String, val high: Int, val low: Int)

public class ForecastdataMapper{

private fun convertFromDataModel(forecast: ForecastResult): ForecastList {
return ForecastList(forecast.city.name, forecast.city.country, convertForecastListToDomain(forecast.list)) //<---wrong type found here (forecast.list is indicated)
}

private fun convertForecastListToDomain(list: List<Forecast>): List<ModelForecast>{
return list.map { convertForecastItemToDomain(it) }
}

private fun convertForecastItemToDomain(forecast: Forecast): ModelForecast{
return ModelForecast(convertDate(forecast.dt),
forecast.weather[0].desciption,
forecast.temp.max.toInt(),
forecast.temp.min.toInt())
}

private fun convertDate(date: Long): String{
val df = DateFormat.getDateInstance(DateFormat.MEDIUM,
Locale.getDefault())
return df.format(date * 1000)
}
}

感谢帮助

最佳答案

您应该从第一页中删除 import 语句,这是在第二页中使用 domain.Forecast 而不是您自己的 Forecast .

import com.example.zacharymcdaniel.weatherkot.domain.Forecast
// ^--- remove it from your source code

使用typealias重命名,例如:

typealias DomainForecast = com.example.zacharymcdaniel.weatherkot.domain.Forecast

使用alias import statement重命名,例如:

import com.example.zacharymcdaniel.weatherkot.domain.Forecast as DomainForecast 

关于android - 如何避免 Kotlin 中同名冲突的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44991554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com