K
kvnsmnsn
Below I'm including the source code for a Java application that con-
tains an interface <If> and two classes that implement it, <Abc> and
<Def>. <Rndm.open> flips a coin and returns an object of class <Abc>
half the time and an object of class <Def> the other half of the time.
Then in my <main> method I call <Rndm.open> and assign its value to
object <xyz>. How can I tell which class <xyz> is, <Abc> or <Def>? I
wrote the code below to help me tell, but when I try to compile it I
get the error message:
Gc.java:36: cannot find symbol
symbol : variable Abc
location: class Gc
if (xyz.getClass() == Abc)
^
1 error
Can anyone see what I'm doing wrong? Any feedback on this would be
greatly appreciated. My code follows:
import java.util.Random;
interface If
{
}
class Abc implements If
{
}
class Def implements If
{
}
class Rndm
{
static If open ()
{
Random rndm = new Random();
if (rndm.nextInt( 2) == 0)
{ return new Abc();
}
else
{ return new Def();
}
}
}
public class Gc
{
public static void main ( String[] arguments)
{
If xyz = Rndm.open();
if (xyz.getClass() == Abc)
{ System.out.println( "Method <open> generated an <Abc>.");
}
else
{ System.out.println( "Method <open> generated an <Def>.");
}
}
}
---Kevin Simonson
"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
tains an interface <If> and two classes that implement it, <Abc> and
<Def>. <Rndm.open> flips a coin and returns an object of class <Abc>
half the time and an object of class <Def> the other half of the time.
Then in my <main> method I call <Rndm.open> and assign its value to
object <xyz>. How can I tell which class <xyz> is, <Abc> or <Def>? I
wrote the code below to help me tell, but when I try to compile it I
get the error message:
Gc.java:36: cannot find symbol
symbol : variable Abc
location: class Gc
if (xyz.getClass() == Abc)
^
1 error
Can anyone see what I'm doing wrong? Any feedback on this would be
greatly appreciated. My code follows:
import java.util.Random;
interface If
{
}
class Abc implements If
{
}
class Def implements If
{
}
class Rndm
{
static If open ()
{
Random rndm = new Random();
if (rndm.nextInt( 2) == 0)
{ return new Abc();
}
else
{ return new Def();
}
}
}
public class Gc
{
public static void main ( String[] arguments)
{
If xyz = Rndm.open();
if (xyz.getClass() == Abc)
{ System.out.println( "Method <open> generated an <Abc>.");
}
else
{ System.out.println( "Method <open> generated an <Def>.");
}
}
}
---Kevin Simonson
"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_