instanceof
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.enumallt;
|
||||
|
||||
public class Anatotitan extends Dinosaur{
|
||||
|
||||
String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.enumallt;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
|
||||
DinosaurEnum dinosaur;
|
||||
|
||||
public DinosaurEnum getDinosaur() {
|
||||
return dinosaur;
|
||||
}
|
||||
|
||||
public void setDinosaur(DinosaurEnum dinosaur) {
|
||||
this.dinosaur = dinosaur;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.enumallt;
|
||||
public enum DinosaurEnum {
|
||||
Anatotitan {
|
||||
@Override
|
||||
public String behaviour() {
|
||||
return "Aggressive";
|
||||
}
|
||||
},
|
||||
Euraptor {
|
||||
@Override
|
||||
public String behaviour() {
|
||||
return "Calm";
|
||||
}
|
||||
};
|
||||
|
||||
public abstract String behaviour();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.enumallt;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.examplesetup;
|
||||
|
||||
public class Anatotitan extends Dinosaur{
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.examplesetup;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.examplesetup;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.getclassalt;
|
||||
|
||||
public class Anatotitan extends Dinosaur{
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.getclassalt;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
/*String behavior() {
|
||||
return "default sound";
|
||||
}*/
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.getclassalt;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.mapalt;
|
||||
|
||||
public class Anatotitan extends Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.mapalt;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "default sound";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.mapalt;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.patternmatching;
|
||||
|
||||
public class Anatotitan extends Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.patternmatching;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
String behavior() {
|
||||
return "default sound";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.patternmatching;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
public class Anatotitan extends Dinosaur{
|
||||
|
||||
@Override
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "default sound";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
@Override
|
||||
public String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.visitorspattern;
|
||||
|
||||
public class Anatotitan implements Dino{
|
||||
|
||||
|
||||
String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
String behavior2(Visitor dinobehave) {
|
||||
return dinobehave.visit(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.visitorspattern;
|
||||
|
||||
public interface Dino {
|
||||
|
||||
|
||||
String behavior2(Visitor dinobehave);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.visitorspattern;
|
||||
|
||||
public class DinoImpl implements Visitor {
|
||||
|
||||
@Override
|
||||
public String visit(Anatotitan anatotitan) {
|
||||
return anatotitan.behavior();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String visit(Euraptor euraptor) {
|
||||
return euraptor.behavior();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.visitorspattern;
|
||||
|
||||
public class Euraptor implements Dino{
|
||||
|
||||
|
||||
|
||||
String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public
|
||||
String behavior2(Visitor dinobehave) {
|
||||
return dinobehave.visit(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.visitorspattern;
|
||||
|
||||
public interface Visitor {
|
||||
|
||||
String visit(Anatotitan anatotitan);
|
||||
String visit(Euraptor euraptor);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.instanceoftest;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.enumallt.*;
|
||||
|
||||
|
||||
public class EnumAltTest {
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
|
||||
Dinosaur dinosaur = new Dinosaur();
|
||||
dinosaur.setDinosaur(DinosaurEnum.Euraptor);
|
||||
|
||||
assertEquals("Calm", dinosaur.getDinosaur().behaviour());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEuraptor() {
|
||||
Dinosaur dinosaur = new Dinosaur();
|
||||
dinosaur.setDinosaur(DinosaurEnum.Anatotitan);
|
||||
|
||||
assertEquals("Aggressive", dinosaur.getDinosaur().behaviour());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.instanceoftest;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.examplesetup.*;
|
||||
|
||||
public class ExampleTest {
|
||||
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
|
||||
assertEquals("very aggressive", dinoBehavior(new Anatotitan()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEuraptor() {
|
||||
assertEquals("calm", dinoBehavior(new Euraptor()));
|
||||
}
|
||||
|
||||
public static String dinoBehavior(Dinosaur dinosaur) {
|
||||
|
||||
if (dinosaur instanceof Anatotitan) {
|
||||
|
||||
Anatotitan anatotitan = (Anatotitan) dinosaur;
|
||||
return anatotitan.behavior();
|
||||
} else if (dinosaur instanceof Euraptor) {
|
||||
Euraptor euraptor = (Euraptor) dinosaur;
|
||||
return euraptor.behavior();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.instanceoftest;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.mapalt.*;
|
||||
|
||||
public class MapTest {
|
||||
|
||||
|
||||
|
||||
Map<Class<? extends Dinosaur>, String> response =
|
||||
new HashMap<Class<? extends Dinosaur>, String>();
|
||||
Dinosaur anatotitan = new Anatotitan();
|
||||
Dinosaur euraptor = new Euraptor();
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
response.put(Anatotitan.class, anatotitan.behavior());
|
||||
|
||||
|
||||
assertEquals("very aggressive", response.get(Anatotitan.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEuraptor() {
|
||||
response.put(Euraptor.class, euraptor.behavior());
|
||||
|
||||
|
||||
assertEquals("calm", response.get(Euraptor.class));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.instanceoftest;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.visitorspattern.*;
|
||||
|
||||
public class VisitorsPatternTest {
|
||||
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
|
||||
|
||||
assertEquals("very aggressive", visitorsPatter((Dino) new Anatotitan()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEuraptor() {
|
||||
|
||||
|
||||
assertEquals("calm", visitorsPatter((Dino) new Euraptor()));
|
||||
}
|
||||
|
||||
public static String visitorsPatter(Dino dinosaur) {
|
||||
Visitor visitor = new DinoImpl();
|
||||
|
||||
return dinosaur.behavior2(visitor);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.instanceoftest;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.getclassalt.*;
|
||||
|
||||
public class getClassTest {
|
||||
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
|
||||
assertEquals("very aggressive", dinoBehavior(new Anatotitan()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEuraptor() {
|
||||
assertEquals("calm", dinoBehavior(new Euraptor()));
|
||||
}
|
||||
|
||||
public static String dinoBehavior(Dinosaur dinosaur) {
|
||||
|
||||
if (dinosaur.getClass().equals(Anatotitan.class)) {
|
||||
|
||||
Anatotitan anatotitan = (Anatotitan) dinosaur;
|
||||
return anatotitan.behavior();
|
||||
} else if (dinosaur.getClass().equals(Euraptor.class)) {
|
||||
Euraptor euraptor = (Euraptor) dinosaur;
|
||||
return euraptor.behavior();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.instanceoftest;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.patternmatching.*;
|
||||
|
||||
public class patternTest {
|
||||
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
|
||||
assertEquals("very aggressive", dinoBehavior(new Anatotitan()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEuraptor() {
|
||||
assertEquals("calm", dinoBehavior(new Euraptor()));
|
||||
}
|
||||
|
||||
public static String dinoBehavior(Dinosaur dinosaur) {
|
||||
|
||||
if (dinosaur instanceof Anatotitan anatotitan) {
|
||||
return anatotitan.behavior();
|
||||
} else if (dinosaur instanceof Euraptor euraptor) {
|
||||
|
||||
return euraptor.behavior();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.instanceoftest;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.polymorphism.*;
|
||||
|
||||
public class polymorphsimTest {
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
|
||||
assertEquals("very aggressive", dinoBehavior(new Anatotitan()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEuraptor() {
|
||||
assertEquals("calm", dinoBehavior(new Euraptor()));
|
||||
}
|
||||
|
||||
public static String dinoBehavior(Dinosaur dinosaur) {
|
||||
return dinosaur.behavior();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user