好的,所以我对编程很新。我有一个班级作业,具体说明如下:
“PolygonArtMaker.java的主要代码中的代码,用于调用tRef引用的ArtisticTurtle上的五边形方法,以使应用程序在运行时绘制五边形。”
从我的解释来看,这意味着当我点击Run on Polygon artmaker时,它还会运行Artistic Turtle并使用方法app绘制....
我已经尝试了一段时间而且无法弄明白,任何帮助都会受到赞赏。
Polygon artmaker
public class PolygonArtMaker
{
public static void main(String [] a)
{
World wRef = new World( );
ArtisticTurtle tRef = new ArtisticTurtle ( wRef );
tref.forward( 50 );
}
}
使用方法应用
public class DrawWithMethodsApp
{
public static void main(String[] a)
{
System.out.println("Hello from main!");
World wref = new World();
ArtisticTurtle tref = new ArtisticTurtle( wref );
tref.setPenColor( java.awt.Color.RED );
tref.pentagon( 2 );
tref.penUp();
tref.moveTo( 50,463);
tref.penDown();
tref.pentagon( 5 );
tref.penUp();
tref.moveTo(350,89);
tref.penDown();
tref.pentagon( 8 );
tref.penUp();
tref.moveTo( 100, 260);
tref.penDown();
tref.hexagon( 5 );
tref.penUp();
tref.moveTo( 500, 110 );
tref.penDown();
tref.hexagon( 7 );
tref.penUp();
tref.moveTo( 360, 310 );
tref.penDown();
tref.hexagon( 9 );
**Artistic Turtle **
public class ArtisticTurtle extends Turtle
{
public void hook( int numberParam )
{
System.out.println( "The hook method has been called on ArtisticTurtle "
+
this
+
"with parameter value equal to "
+
numberParam
);
}
public void pentagon( int numberParam )
{
System.out.println
("pentagon called with param "
+ numberParam);
int curLen;
curLen = (numberParam) ;
this.forward ( 10*curLen );
this.turn( 360.0/5 );
this.forward( 10*curLen );
this.turn( 360.0/5 );
this.forward( 10*curLen);
this.turn( 360.0/5 );
this.forward( 10*curLen );
this.turn( 360.0/5 );
this.forward( 10*curLen );
}
public void hexagon( int numberParam)
{
System.out.println
("hexagon called with param "
+ numberParam);
int curLen;
curLen = (numberParam) ;
this.forward( 10*curLen );
this.turn( 60 );
this.forward( 10*curLen);
this.turn( 60 );
this.forward( 10*curLen);
this.turn( 60 );
this.forward( 10*curLen);
this.turn( 60 );
this.forward( 10*curLen);
this.turn( 60 );
this.forward( 10*curLen );
}
public ArtisticTurtle( World wrefParam )
{
super( wrefParam );
}
public static void main(String[] a)
{
System.out.println("DONT RUN ArtisticTurtle!!");
System.out.println("Select DrawWithMethodsApp");
System.out.println("and RUN that!");
}
}
答案 0 :(得分:3)
因此,根据我的理解,您需要从PolygonArtMaker主函数调用五边形函数。要做到这一点,你需要首先通过调用它的构造函数(你已经)来创建一个ArtisticTurtle的实例。然后你需要做的就是通过对象调用函数。在这种情况下,您需要:
public class PolygonArtMaker{
public static void main(String [] args){
World wRef = new World( );
ArtisticTurtle tRef = new ArtisticTurtle ( wRef );
// this is just some random number I chose, not sure where you are supposed to get this value.
int num = 7;
// here is where the magic happens - call the function in the other class
tRef.pentagon(num);
}
}
你有一条tref线。我认为你的意思是tRef。 希望这可以帮助。如果您需要更多指导,请告诉我:)