好吧,我对编程还很陌生。我的类(class)有一项作业,具体说明是:
“在 PolygonArtMaker.java 的主代码中调用 tRef 引用的 ArtisticTurtle 上的五边形方法,使您的应用程序在运行时绘制五边形。”
根据我的解释,这意味着当我单击 Polygon artmaker 上的“运行”时,它还将运行 Artistic Turtle 并使用方法应用程序进行绘制...
我已经尝试了一段时间,但无法弄清楚,任何帮助将不胜感激。
多边形美术师
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!");
}
}
最佳答案
因此,据我了解,您需要从 PolygonArtMaker 主函数中调用五边形函数。为此,您需要首先通过调用 ArtisticTurtle 的构造函数(您已经有了)来创建 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。 希望这可以帮助。如果您需要更多指导,请告诉我:)
关于java - 从另一个类调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19016537/