instanceof
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package com.baeldung.enumallt;
|
||||
|
||||
public class Anatotitan extends Dinosaur{
|
||||
|
||||
public class Anatotitan extends Dinosaur {
|
||||
|
||||
String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.baeldung.enumallt;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
|
||||
|
||||
DinosaurEnum dinosaur;
|
||||
|
||||
public DinosaurEnum getDinosaur() {
|
||||
@@ -12,6 +11,5 @@ public class Dinosaur {
|
||||
public void setDinosaur(DinosaurEnum dinosaur) {
|
||||
this.dinosaur = dinosaur;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
package com.baeldung.enumallt;
|
||||
|
||||
public enum DinosaurEnum {
|
||||
Anatotitan {
|
||||
@Override
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.baeldung.enumallt;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
public class Euraptor extends Dinosaur {
|
||||
|
||||
String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package com.baeldung.examplesetup;
|
||||
|
||||
public class Anatotitan extends Dinosaur{
|
||||
|
||||
|
||||
public class Anatotitan extends Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package com.baeldung.examplesetup;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.baeldung.examplesetup;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
|
||||
public class Euraptor extends Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.baeldung.getclassalt;
|
||||
|
||||
public class Anatotitan extends Dinosaur{
|
||||
|
||||
|
||||
public class Anatotitan extends Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.baeldung.getclassalt;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
/*String behavior() {
|
||||
return "default sound";
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* String behavior() { return "default sound"; }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
package com.baeldung.getclassalt;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
|
||||
|
||||
public class Euraptor extends Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.baeldung.mapalt;
|
||||
|
||||
public class Anatotitan extends Dinosaur {
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package com.baeldung.mapalt;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "default sound";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.baeldung.mapalt;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
|
||||
public class Euraptor extends Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.baeldung.patternmatching;
|
||||
|
||||
public class Anatotitan extends Dinosaur {
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package com.baeldung.patternmatching;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
|
||||
String behavior() {
|
||||
return "default sound";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.baeldung.patternmatching;
|
||||
|
||||
public class Euraptor extends Dinosaur{
|
||||
|
||||
|
||||
public class Euraptor extends Dinosaur {
|
||||
|
||||
public String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
public class Anatotitan extends Dinosaur{
|
||||
public class Anatotitan extends Dinosaur {
|
||||
|
||||
@Override
|
||||
public String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.baeldung.polymorphism;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
|
||||
public String behavior() {
|
||||
return "default sound";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
package com.baeldung.visitorspattern;
|
||||
|
||||
public class Anatotitan implements Dino{
|
||||
public class Anatotitan implements Dino {
|
||||
|
||||
|
||||
String behavior() {
|
||||
return "very aggressive";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public
|
||||
String behavior2(Visitor dinobehave) {
|
||||
public String behavior2(Visitor dinobehave) {
|
||||
return dinobehave.visit(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.baeldung.visitorspattern;
|
||||
|
||||
public class DinoImpl implements Visitor {
|
||||
public class DinoVisitorImpl implements Visitor {
|
||||
|
||||
@Override
|
||||
public String visit(Anatotitan anatotitan) {
|
||||
@@ -9,8 +9,7 @@ public class DinoImpl implements Visitor {
|
||||
|
||||
@Override
|
||||
public String visit(Euraptor euraptor) {
|
||||
return euraptor.behavior();
|
||||
return euraptor.behavior();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,18 +1,13 @@
|
||||
package com.baeldung.visitorspattern;
|
||||
|
||||
public class Euraptor implements Dino{
|
||||
|
||||
|
||||
|
||||
public class Euraptor implements Dino {
|
||||
|
||||
String behavior() {
|
||||
return "calm";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public
|
||||
String behavior2(Visitor dinobehave) {
|
||||
public String behavior2(Visitor dinobehave) {
|
||||
return dinobehave.visit(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.baeldung.visitorspattern;
|
||||
|
||||
public interface Visitor {
|
||||
|
||||
|
||||
String visit(Anatotitan anatotitan);
|
||||
|
||||
String visit(Euraptor euraptor);
|
||||
|
||||
}
|
||||
|
||||
@@ -6,11 +6,10 @@ import org.junit.Test;
|
||||
|
||||
import com.baeldung.enumallt.*;
|
||||
|
||||
|
||||
public class EnumAltTest {
|
||||
public class EnumUnitTest {
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
|
||||
|
||||
Dinosaur dinosaur = new Dinosaur();
|
||||
dinosaur.setDinosaur(DinosaurEnum.Euraptor);
|
||||
|
||||
@@ -25,5 +24,4 @@ public class EnumAltTest {
|
||||
assertEquals("Aggressive ", dinosaur.getDinosaur().behaviour());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import org.junit.Test;
|
||||
|
||||
import com.baeldung.examplesetup.*;
|
||||
|
||||
public class ExampleTest {
|
||||
public class ExampleSetupUnitTest {
|
||||
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
@@ -5,7 +5,7 @@ import org.junit.Test;
|
||||
|
||||
import com.baeldung.getclassalt.*;
|
||||
|
||||
public class getClassTest {
|
||||
public class GetClassUnitTest {
|
||||
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
@@ -9,21 +9,15 @@ 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();
|
||||
|
||||
|
||||
public class MapUnitTest {
|
||||
|
||||
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());
|
||||
|
||||
response.put(Anatotitan.class, anatotitan.behavior());
|
||||
|
||||
assertEquals("very aggressive", response.get(Anatotitan.class));
|
||||
}
|
||||
@@ -31,11 +25,8 @@ public class MapTest {
|
||||
@Test
|
||||
public void testEuraptor() {
|
||||
response.put(Euraptor.class, euraptor.behavior());
|
||||
|
||||
|
||||
assertEquals("calm", response.get(Euraptor.class));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import org.junit.Test;
|
||||
|
||||
import com.baeldung.patternmatching.*;
|
||||
|
||||
public class patternTest {
|
||||
public class PatternMatchingUnitTest {
|
||||
|
||||
@Test
|
||||
public void testAnatotitan() {
|
||||
@@ -7,21 +7,19 @@ import org.junit.Test;
|
||||
|
||||
import com.baeldung.polymorphism.*;
|
||||
|
||||
public class polymorphsimTest {
|
||||
|
||||
|
||||
public class PolymorphismUnitTest {
|
||||
|
||||
@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();
|
||||
}
|
||||
@@ -6,25 +6,23 @@ import org.junit.Test;
|
||||
|
||||
import com.baeldung.visitorspattern.*;
|
||||
|
||||
public class VisitorsPatternTest {
|
||||
public class VisitorsPatternUnitTest {
|
||||
|
||||
@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();
|
||||
|
||||
Visitor visitor = new DinoVisitorImpl();
|
||||
|
||||
return dinosaur.behavior2(visitor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user