JavaCompilerCore/resources/syntaxtreegenerator/javFiles/PatternMatching.jav

20 lines
646 B
Plaintext
Raw Normal View History

import java.lang.String;
record Point(int x, int y) {}
interface Shape {}
record ColoredPoint(Point pt, String color) {}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) implements Shape {}
2023-07-12 19:54:17 +00:00
sealed class Color permits Blue, Red {}
class Blue extends Color {}
class Red extends Color {}
class PatternMatching {
void printColorOfUpperLeftPoint(Shape shape)
{
switch (shape) {
case Rectangle(ColoredPoint(Point pt, String color), ColoredPoint lowerRight) -> System.out.println("x: " + pt.x() + " / color: " + color + " / lowerRight: " + lowerRight);
default -> System.out.println("not a rectangle");
2023-07-12 19:54:17 +00:00
};
}
}