instanceof

This commit is contained in:
michaelin007
2022-11-25 12:13:41 +00:00
parent 2f35139e41
commit 7679caa8bc
31 changed files with 519 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
package com.baeldung.enumallt;
public class Anatotitan extends Dinosaur{
String behavior() {
return "very aggressive";
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.enumallt;
public class Euraptor extends Dinosaur{
String behavior() {
return "calm";
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.examplesetup;
public class Anatotitan extends Dinosaur{
public String behavior() {
return "very aggressive";
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.examplesetup;
public class Dinosaur {
}

View File

@@ -0,0 +1,12 @@
package com.baeldung.examplesetup;
public class Euraptor extends Dinosaur{
public String behavior() {
return "calm";
}
}

View File

@@ -0,0 +1,12 @@
package com.baeldung.getclassalt;
public class Anatotitan extends Dinosaur{
public String behavior() {
return "very aggressive";
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.getclassalt;
public class Dinosaur {
/*String behavior() {
return "default sound";
}*/
}

View File

@@ -0,0 +1,13 @@
package com.baeldung.getclassalt;
public class Euraptor extends Dinosaur{
public String behavior() {
return "calm";
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.mapalt;
public class Anatotitan extends Dinosaur {
public String behavior() {
return "very aggressive";
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.mapalt;
public class Dinosaur {
public String behavior() {
return "default sound";
}
}

View File

@@ -0,0 +1,12 @@
package com.baeldung.mapalt;
public class Euraptor extends Dinosaur{
public String behavior() {
return "calm";
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.patternmatching;
public class Anatotitan extends Dinosaur {
public String behavior() {
return "very aggressive";
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.patternmatching;
public class Dinosaur {
String behavior() {
return "default sound";
}
}

View File

@@ -0,0 +1,12 @@
package com.baeldung.patternmatching;
public class Euraptor extends Dinosaur{
public String behavior() {
return "calm";
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.polymorphism;
public class Anatotitan extends Dinosaur{
@Override
public String behavior() {
return "very aggressive";
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.polymorphism;
public class Dinosaur {
public String behavior() {
return "default sound";
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.polymorphism;
public class Euraptor extends Dinosaur{
@Override
public String behavior() {
return "calm";
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.visitorspattern;
public interface Dino {
String behavior2(Visitor dinobehave);
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.visitorspattern;
public interface Visitor {
String visit(Anatotitan anatotitan);
String visit(Euraptor euraptor);
}

View File

@@ -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());
}
}

View File

@@ -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 "";
}
}

View File

@@ -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));
}
}

View File

@@ -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);
}
}

View File

@@ -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 "";
}
}

View File

@@ -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 "";
}
}

View File

@@ -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();
}
}