Last active
November 29, 2018 21:33
-
-
Save beigirad/c8b3eae6be46ba8704f562691b33bd8a to your computer and use it in GitHub Desktop.
kotlin vs. java Generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.Metadata; | |
import kotlin.jvm.internal.Intrinsics; | |
import org.jetbrains.annotations.NotNull; | |
public final class Test { | |
public final void s() { | |
RequestProvider.Production production1 = new FoodStore(); | |
RequestProvider.Production production2 = new FastFoodStore(); | |
RequestProvider.Production production3 = new InOutBurger(); | |
} | |
public final void s2() { | |
RequestProvider.Consumer consumer1 = new Everybody(); | |
RequestProvider.Consumer consumer2 = new ModernPeople(); | |
RequestProvider.Consumer consumer3 = new American(); | |
} | |
public static class Food { } | |
public static class FastFood extends RequestProvider.Food { } | |
public static final class Burger extends RequestProvider.FastFood { } | |
public interface Production { | |
Object produce(); | |
} | |
public static final class FoodStore implements RequestProvider.Production { | |
@NotNull | |
public RequestProvider.Food produce() { | |
String var1 = "Produce food"; | |
System.out.println(var1); | |
return new RequestProvider.Food(); | |
} | |
public Object produce() { | |
return this.produce(); | |
} | |
} | |
public static final class FastFoodStore implements RequestProvider.Production { | |
@NotNull | |
public RequestProvider.FastFood produce() { | |
String var1 = "Produce fast food"; | |
System.out.println(var1); | |
return new RequestProvider.FastFood(); | |
} | |
public Object produce() { | |
return this.produce(); | |
} | |
} | |
public static final class InOutBurger implements RequestProvider.Production { | |
@NotNull | |
public RequestProvider.Burger produce() { | |
String var1 = "Produce burger"; | |
System.out.println(var1); | |
return new RequestProvider.Burger(); | |
} | |
public Object produce() { | |
return this.produce(); | |
} | |
} | |
public interface Consumer { | |
void consume(Object var1); | |
} | |
public static final class Everybody implements RequestProvider.Consumer { | |
public void consume(@NotNull RequestProvider.Food item) { | |
Intrinsics.checkParameterIsNotNull(item, "item"); | |
String var2 = "Eat food"; | |
System.out.println(var2); | |
} | |
public void consume(Object var1) { | |
this.consume((RequestProvider.Food)var1); | |
} | |
} | |
public static final class ModernPeople implements RequestProvider.Consumer { | |
public void consume(@NotNull RequestProvider.FastFood item) { | |
Intrinsics.checkParameterIsNotNull(item, "item"); | |
String var2 = "Eat fast food"; | |
System.out.println(var2); | |
} | |
public void consume(Object var1) { | |
this.consume((RequestProvider.FastFood)var1); | |
} | |
} | |
public static final class American implements RequestProvider.Consumer { | |
public void consume(@NotNull RequestProvider.Burger item) { | |
Intrinsics.checkParameterIsNotNull(item, "item"); | |
String var2 = "Eat burger"; | |
System.out.println(var2); | |
} | |
public void consume(Object var1) { | |
this.consume((RequestProvider.Burger)var1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test{ | |
open class Food | |
open class FastFood : Food() | |
class Burger : FastFood() | |
class FoodStore : Production<Food> { | |
override fun produce(): Food { | |
println("Produce food") | |
return Food() | |
} | |
} | |
interface Production<out T> { | |
fun produce(): T | |
} | |
class FastFoodStore : Production<FastFood> { | |
override fun produce(): FastFood { | |
println("Produce fast food") | |
return FastFood() | |
} | |
} | |
class InOutBurger : Production<Burger> { | |
override fun produce(): Burger { | |
println("Produce burger") | |
return Burger() | |
} | |
} | |
fun s(){ | |
val production1 : Production<Food> = FoodStore() | |
val production2 : Production<Food> = FastFoodStore() | |
val production3 : Production<Food> = InOutBurger() | |
} | |
interface Consumer<in T> { | |
fun consume(item: T) | |
} | |
class Everybody : Consumer<Food> { | |
override fun consume(item: Food) { | |
println("Eat food") | |
} | |
} | |
class ModernPeople : Consumer<FastFood> { | |
override fun consume(item: FastFood) { | |
println("Eat fast food") | |
} | |
} | |
class American : Consumer<Burger> { | |
override fun consume(item: Burger) { | |
println("Eat burger") | |
} | |
} | |
fun s2(){ | |
val consumer1 : Consumer<Burger> = Everybody() | |
val consumer2 : Consumer<Burger> = ModernPeople() | |
val consumer3 : Consumer<Burger> = American() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment