android– 如何在Kotlin中避免具有相同名称冲突的类型?

android– 如何在Kotlin中避免具有相同名称冲突的类型?,第1张

概述我对kotlin很新,而且我正在从一本书中构建的应用程序遇到一些麻烦.我在名为Forecast的单独包中有两个类,我试图在一个包含使用其中相同名称的包的函数中定义一些函数.该书说要将Forecast类导入为ModelForecast并且我做了但是现在我在跟踪这种类型不匹配错误的来源时遇到了问题.看来

我对kotlin很新,而且我正在从一本书中构建的应用程序遇到一些麻烦.我在名为Forecast的单独包中有两个类,我试图在一个包含使用其中相同名称的包的函数中定义一些函数.该书说要将Forecast类导入为ModelForecast并且我做了但是现在我在跟踪这种类型不匹配错误的来源时遇到了问题.看来我的convertForecastListTodomain()方法正在期待其他的东西?请帮我找到我犯的错误.如果它非常简单,我不会感到惊讶,但我仍然无法找到它.

MainActivity.kt:

package com.example.zacharymcdanIEl.weatherkotimport androID.support.v7.app.AppCompatActivityimport androID.os.Bundleimport androID.support.v7.Widget.linearlayoutmanagerimport androID.support.v7.Widget.RecyclerVIEwimport com.example.zacharymcdanIEl.weatherkot.domain.Forecastimport org.jetbrains.anko.findclass 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 pagedata 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包中的domain.kt:

package com.example.zacharymcdanIEl.weatherkot.domainimport com.example.zacharymcdanIEl.weatherkot.Forecastimport com.example.zacharymcdanIEl.weatherkot.forecastresultimport java.text.DateFormatimport java.util.*import com.example.zacharymcdanIEl.weatherkot.domain.Forecast as ModelForecastpublic 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而不是你自己的预测.

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

或者使用typealias重命名它,例如:

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

或者使用别名import语句重命名它,例如:

import com.example.zacharymcdanIEl.weatherkot.domain.Forecast as DomainForecast 
总结

以上是内存溢出为你收集整理的android – 如何在Kotlin中避免具有相同名称冲突的类型?全部内容,希望文章能够帮你解决android – 如何在Kotlin中避免具有相同名称冲突的类型?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1209289.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存