package br.com.dhionata // Classe para armazenar estatísticas de cada classe data class…
- User
package br.com.dhionata
// Classe para armazenar estatísticas de cada classe data class Set( val name: String, val armor: Double, val bodyProtection: Double, // Body Protection% val headProtection: Double, // Head Protection% val armProtection: Double, // Arm Protection% val legProtection: Double, // Leg Protection% val absorption: Double, // Absorption (flat value) val resistance: Double, // Resistance (percentage) val weaponTypeResistance: Double, // Weapon Type Resistance (percentage) val cyborgDmgBuff: Double = 0.0, // Bonus contra Cyborgs val entityDmgMult: Double = 1.0, // Multiplicador contra entidades específicas val hp: Double = 125.0, // HP inicial ) {
companion object Fuzileiro { val sirocco: Set = Set( "Tempestade de Areia", 500.0, 0.15, 0.25, 0.25, 0.25, 15.0, 0.0, 0.0 ) val nord: Set = Set( "Nórdico", 340.0, 0.0, 0.6, 0.25, 0.25, 22.0, 0.0, 0.0 ) }}
como eu posso acessar na main() diferente de val setList: List = listOf(Set.sirocco, Set.nord) eu gostaria de recuperar todos os companions objects de Set para colocar em setList
- Assistant
To retrieve all the
Setinstances defined in the companion object without manually listing them, you can use Kotlin's reflection capabilities. Here's how you can do it:import kotlin.reflect.full.memberProperties fun main() { val companionInstance = Set::class.companionObjectInstance val setList = Set::class.companionObject?.memberProperties ?.filter { it.returnType.classifier == Set::class } ?.map { it.get(companionInstance) as Set } ?: emptyList() // Now you can use setList which contains all Set instances from the companion object println(setList) }Explanation:
-
Retrieve the Companion Object Instance:
Set::class.companionObjectInstancegets the instance of the companion object of theSetclass.Set::class.companionObjectgets theKClassreference to the companion object.
-
Access Companion Object Properties:
memberPropertiesretrieves all properties declared in the companion object.- We filter these properties to include only those whose return type is
Set.it.returnType.classifier == Set::classensures the property is of typeSet.
-
Retrieve Property Values:
it.get(companionInstance)gets the value of the property from the companion object instance.- We cast it to
Setsince we have ensured it's of that type.
-
Handle Nulls:
- The Elvis operator
?: emptyList()ensures thatsetListis an empty list if the companion object or properties are null (which shouldn't happen in this context).
- The Elvis operator
Note:
This approach relies on reflection, which can introduce overhead and should be used judiciously. If possible, consider maintaining a list of your
Setinstances manually within the companion object:data class Set(/* ... */) { companion object Fuzileiro { val sirocco: Set = Set(/* ... */) val nord: Set = Set(/* ... */) val allSets: List<Set> = listOf(sirocco, nord) } }Then, you can access
Set.allSetsdirectly without reflection:val setList = Set.allSetsThis manual method is more efficient and clearer in intent but requires you to update the list whenever you add new instances.
-