I can't seem to figure this out: I want to have an attribute of a class to be settable and gettable as an enumeration. The attribute in question is an objects shape, which can be either CONCAVE, CONVEX, or UNDEFINED. So I thought an enum would be good to use:
private enum Shape {CONCAVE, CONVEX, UNDEFINED}
this is my constructor:
Vertex()
{
point = new int[] {0, 0};
Shape shape = Shape.UNDEFINED;
}
and here is its setter:
public void setShape(Shape shape)
{
Shape = shape;
}
and the corresponding accessor:
public Shape getShape()
{
return Shape;
}
and here is the compiler errors:
./Vertex.java:27: cannot find symbol symbol : variable Shape location: class Vertex Shape = shape; ^ ./Vertex.java:56: cannot find symbol symbol : variable Shape location: class Vertex return Shape; ^
I've tried all sorts of syntactical combinations and this is the closest I've come to. I also need to know how to properly pass the enumeration value via the setter method in the 'calling' class...
I don't quite understand what you have now, but I think you need:
public class Vertex {
private enum Shape {CONCAVE, CONVEX, UNDEFINED}
private Shape shape;
private int[] point;
public Vertex() {
point = new int[] {0, 0};
shape = Shape.UNDEFINED;
}
public Shape getShape() {
return shape;
}
public void setShape(Shape shape) {
this.shape = shape;
}
}
Your getter and setter methods are wrong because you've made a typo: you are referencing enumeration instead of class fields. It should be:
public void setShape(Shape shape)
{
this.shape = shape;
}
public Shape getShape()
{
return shape;
}
shape
should be a class field:
Shape shape;
Seems like you're missing an instance variable for the shape. Also, when allowing the Shape to be read and set, the enum type must be accessible (public, or package protected):
public class Vertex {
public static enum Shape {
CONCAVE, CONVEX, UNDEFINED
}
private Shape shape; // the instance variable
private int[] point;
public Vertex () {
point = new int[] {0, 0};
shape = Shape.UNDEFINED;
}
public Shape getShape () {
return shape;
}
public void setShape (Shape shape) {
this.shape = shape;
}
}
public class Vertex {
private enum Shape {CONCAVE, CONVEX, UNDEFINED};
Shape shape;
Vertex(){
this.shape = Shape.UNDEFINED;}
public void setShape(Shape shape){
this.shape = shape;}
public Shape getShape(){
return this.shape;}
You have no variable for Shape. A variable would resolve errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.