簡體   English   中英

從另一個類調用方法

[英]Calling a method from another class

好吧,即時消息對編程來說還很新。 我為班級安排了作業,具體說明如下:
“ PolygonArtMaker.java主要代碼中的代碼,用於在tRef引用的ArtisticTurtle上調用您的五邊形方法,以使您的應用程序在運行時繪制一個五邊形。”

根據我的解釋,這意味着當我單擊“在Polygon artmaker上運行”時,它將同時運行Artistic Turtle並使用方法app進行繪制。

我已經嘗試了一段時間,無法弄清楚,任何幫助將不勝感激。

多邊形美術師

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的構造函數(已經擁有)來創建該實例。 然后,您需要做的就是通過對象調用函數。 在這種情況下,您需要:

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。 希望這可以幫助。 如果您需要更多指導,請告訴我:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM