BAEL-3854 - Pattern Matching for instanceof in Java 14

This commit is contained in:
Jonathan Cook 2020-03-01 08:36:51 +01:00
parent 08506092e8
commit f0e23cabda
1 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,26 @@
package com.baeldung.java14.patternmatchingforinstanceof;
public class PatternMatchingForInstanceOf {
public void performAnimalOperations(Animal animal) {
if (animal instanceof Cat cat) {
cat.meow();
} else if(animal instanceof Dog dog) {
dog.woof();
}
}
private abstract class Animal {
}
private final class Cat extends Animal {
private void meow() {
}
}
private final class Dog extends Animal {
private void woof() {
}
}
}