Skip to content

Instantly share code, notes, and snippets.

@dandvl
Last active November 26, 2019 03:54
Show Gist options
  • Save dandvl/b485cb4be7ff42ae5797ffa3bf2fd259 to your computer and use it in GitHub Desktop.
Save dandvl/b485cb4be7ff42ae5797ffa3bf2fd259 to your computer and use it in GitHub Desktop.
SealClass
//All these 4 classes MUST be in the same file
sealed class Product
data class AlphaProduct (var name: String, var description:String) : Product()
data class BetaProduct (var name: String, var description:String) : Product()
data class GameProduct (var name: String, var description:String) : Product()
var one = AlphaProduct("alpha", "one")
var two = BetaProduct("beta", "two")
var three = GameProduct("game", "three")
var listOfProducts = mutableListOf<Product>()
listOfProducts.add(one)
listOfProducts.add(two)
listOfProducts.add(three)
listOfProducts.forEach { product ->
if(product is AlphaProduct){
Log.i("test", "${product.name}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment