как вызвать функцию в kotlin которая написанна в другом файле
- User
как вызвать функцию в kotlin которая написанна в другом файле
- Assistant
- Импортируйте файл с функцией в ваш текущий файл Kotlin:
kotlinimport package_name.function_name- Вызовите функцию:
kotlinfunction_name()Примечание:
-
package_name- это имя пакета, содержащего файл с функцией. -function_name- это имя функции, которую вы хотите вызвать. - User
import tj.horner.villagergpt.VillagerGPT.tradesT private fun tradesT(): Boolean { val trade = config.getBoolean("allow-trades") return trade } can not import tradesT why?
- Assistant
tradesTis not an external function that is defined outside theVillagerGPTclass. Instead, it is a private function that is declared inside theVillagerGPTclass. Therefore, if you want to usetradesTfunction in another Kotlin file, you need to make sure that the classVillagerGPTis accessible from that file.Assuming that
VillagerGPTclass is in the packagetj.horner.villagergpt, you could import it in the other Kotlin file using the following code:kotlinimport tj.horner.villagergpt.VillagerGPTThen, you can create an instance of
VillagerGPTclass and call itstradesTfunction like this:kotlinval villagerGPT = VillagerGPT() val allowTrades = villagerGPT.tradesT()Make sure that you have an instance of
VillagerGPTbefore calling thetradesT()function, as it is not astaticfunction.