如何显示方法从一个类到另一个类的主要方法

问题描述:

我正在为我的计算机科学类与Java(我正在使用BlueJ)的动画工作。我现在面临的问题是,我无法将我的第一课的方法显示到第二课的主要方法中。这是我正在处理的代码。这不是最有序的代码,但这是我迄今为止。如何显示方法从一个类到另一个类的主要方法

/** 
* Write a description of class Animation here. 
* 
* @author 
* @version 
*/ 

import java.util.Scanner; 
import java.awt.*; 

class TurtleGraphics 
{ 
    //set conditions for turtle to start drawing 
    public void prepareTurtleToDraw(Turtle myrtle, Color color, int x, int y) 
    { 
     myrtle.hide(); 
     myrtle.penUp();    //pick up the pen to avoid leaving a trail when moving the turtle 
     myrtle.setColor(color);  //set myrtle's color 
     myrtle.moveTo(x, y);   //move to coordinates 
     myrtle.penDown();    //put the pen down to start drawing 
    }//end of prepareTurtleToDraw method 

    //draw a line 
    public void drawLine(Turtle myrtle, int x1, int y1, int x2, int y2)//, int width) 
    { 
     myrtle.moveTo(x1, y1);  //moves to this coordinate first 
     myrtle.moveTo(x2, y2);  //then moves to this coordinate 
     //myrtle.setPenWidth(width); //this adjusts the size of the lines 
    }//end of drawLine method 
} 
class AnimationGraphics 
{  
    public static void pressC() 
{ 
    //Animation animate = new Animation(); 

    World worldObj = new World(); 
    Animation turt = new Animation(); 

    Turtle surtle = new Turtle(-50, 120, worldObj); 

    turt.prepareTurtleToDraw(surtle, Color.RED, -50, 120); 
    turt.drawLine(surtle, -50, 120, 60, 75); 
    turt.drawLine(surtle, 60, 75, 135, 150); 
    turt.drawLine(surtle, 135, 150, 25, 225); 
    turt.drawLine(surtle, 25, 225, -50, 120); 

    System.out.println(worldObj); 
    String userInput = "";         //declare and initialize a String variable 
    char key = ' ';           //declare and initialize a char variable 
    Scanner in = new Scanner(System.in);     //construct a Scanner object 

    System.out.println("Please press the c key multiple times to watch the animation."); 

    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable   

     Turtle kurtle = new Turtle(-50, 120, worldObj); 

     turt.prepareTurtleToDraw(kurtle, Color.WHITE, -50, 120); 
     turt.drawLine(kurtle, -50, 120, 60, 75); 
     turt.drawLine(kurtle, 60, 75, 135, 150); 
     turt.drawLine(kurtle, 135, 150, 25, 225); 
     turt.drawLine(kurtle, 25, 225, -50, 120); 


     Turtle turtle = new Turtle(100, 150, worldObj); 

     turt.prepareTurtleToDraw(turtle, Color.BLACK, 100, 150); 
     turt.drawLine(turtle, 50, 150, 200, 150); 
     turt.drawLine(turtle, 200, 150, 200, 250); 
     turt.drawLine(turtle, 200, 250, 50, 250); 
     turt.drawLine(turtle, 50, 250, 50, 150); 
     } 
     while(key != 'c');          //do-while condition statement 


    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable   


     Turtle burtle = new Turtle(100, 150, worldObj); 

     turt.prepareTurtleToDraw(burtle, Color.WHITE, 100, 150); 
     turt.drawLine(burtle, 50, 150, 200, 150); 
     turt.drawLine(burtle, 200, 150, 200, 250); 
     turt.drawLine(burtle, 200, 250, 50, 250); 
     turt.drawLine(burtle, 50, 250, 50, 150); 

     Turtle dyrtle = new Turtle(150, 150, worldObj); 

     turt.prepareTurtleToDraw(dyrtle, Color.RED, 150, 150); 
     turt.drawLine(dyrtle, 150, 150, 260, 75); 
     turt.drawLine(dyrtle, 260, 75, 335, 150); 
     turt.drawLine(dyrtle, 335, 150, 225, 225); 
     turt.drawLine(dyrtle, 225, 225, 150, 150); 

     } 
     while(key != 'c');          //do-while condition statement 



    //do-while loop to wait for the user to enter the letter c 
    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable   


     Turtle vyrtle = new Turtle(150, 150, worldObj); 

     turt.prepareTurtleToDraw(vyrtle, Color.WHITE, 150, 150); 
     turt.drawLine(vyrtle, 150, 150, 260, 75); 
     turt.drawLine(vyrtle, 260, 75, 335, 150); 
     turt.drawLine(vyrtle, 335, 150, 225, 225); 
     turt.drawLine(vyrtle, 225, 225, 150, 150); 

     Turtle mertle = new Turtle(300, 150, worldObj);  

     turt.prepareTurtleToDraw(mertle, Color.BLACK, 250, 150); 
     turt.drawLine(mertle, 250, 150, 400, 150); 
     turt.drawLine(mertle, 400, 150, 400, 250); 
     turt.drawLine(mertle, 400, 250, 250, 250); 
     turt.drawLine(mertle, 250, 250, 250, 150); 



     } 
     while(key != 'c');          //do-while condition statement 

    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable 


     Turtle dertle = new Turtle(250, 150, worldObj); 

     turt.prepareTurtleToDraw(dertle, Color.WHITE, 250, 150); 
     turt.drawLine(dertle, 250, 150, 400, 150); 
     turt.drawLine(dertle, 400, 150, 400, 250); 
     turt.drawLine(dertle, 400, 250, 250, 250); 
     turt.drawLine(dertle, 250, 250, 250, 150); 

     Turtle qyrtle = new Turtle(150, 150, worldObj); 

     turt.prepareTurtleToDraw(qyrtle, Color.RED, 325, 150); 
     turt.drawLine(qyrtle, 325, 150, 435, 75); 
     turt.drawLine(qyrtle, 435, 75, 510, 150); 
     turt.drawLine(qyrtle, 510, 150, 400, 225); 
     turt.drawLine(qyrtle, 400, 225, 325, 150); 


    } 
    while(key != 'c');          //do-while condition statement 

    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable 

     Turtle myrtle = new Turtle(325, 150, worldObj); 

     turt.prepareTurtleToDraw(myrtle, Color.WHITE, 325, 150); 
     turt.drawLine(myrtle, 325, 150, 435, 75); 
     turt.drawLine(myrtle, 435, 75, 510, 150); 
     turt.drawLine(myrtle, 510, 150, 400, 225); 
     turt.drawLine(myrtle, 400, 225, 325, 150); 

     Turtle bertle = new Turtle(500, 150, worldObj); 

     turt.prepareTurtleToDraw(bertle, Color.BLACK, 500, 150); 
     turt.drawLine(bertle, 425, 150, 575, 150); 
     turt.drawLine(bertle, 575, 150, 575, 250); 
     turt.drawLine(bertle, 575, 250, 425, 250); 
     turt.drawLine(bertle, 425, 250, 425, 150); 


    } 
    while(key != 'c');          //do-while condition statement 

    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable    


     Turtle mertle = new Turtle(500, 150, worldObj); 

     turt.prepareTurtleToDraw(mertle, Color.WHITE, 500, 150); 
     turt.drawLine(mertle, 425, 150, 575, 150); 
     turt.drawLine(mertle, 575, 150, 575, 250); 
     turt.drawLine(mertle, 575, 250, 425, 250); 
     turt.drawLine(mertle, 425, 250, 425, 150); 

    } 
    while(key != 'c');          //do-while condition statement 
    System.out.println(worldObj); 
}//end of main method 
} 

public class AnimationTester1 
{ 
public static void main(String[] args) 
{   
    AnimationGraphics animate = new AnimationGraphics(); 

    //Animation.main(); 
    //public static void Animation(); 

    //System.out.println(press); 

}  
} 

这是可行的版本,但它不是最有序的方式。对于这一个,所有的代码都是主要的方法,这不是我想改变的最佳格式。

/** 
* Write a description of class Animation here. 
* 
* @author 
* @version 
*/ 

import java.util.Scanner; 
import java.awt.*; 
class Animation 
{  
//set conditions for turtle to start drawing 
public void prepareTurtleToDraw(Turtle myrtle, Color color, int x, int y) 
{ 
    myrtle.hide(); 
    myrtle.penUp();    //pick up the pen to avoid leaving a trail when moving the turtle 
    myrtle.setColor(color);  //set myrtle's color 
    myrtle.moveTo(x, y);   //move to coordinates 
    myrtle.penDown();    //put the pen down to start drawing 
}//end of prepareTurtleToDraw method 

//draw a line 
public void drawLine(Turtle myrtle, int x1, int y1, int x2, int y2)//, int width) 
{ 
    myrtle.moveTo(x1, y1);  //moves to this coordinate first 
    myrtle.moveTo(x2, y2);  //then moves to this coordinate 
    //myrtle.setPenWidth(width); //this adjusts the size of the lines 
}//end of drawLine method 

public static void animation() 
{ 
    World worldObj = new World(); 
    Turtle lertle = new Turtle(300, 150, worldObj);    //create a Turtle object to do the drawing 
    Animation turt = new Animation(); 

    Turtle dyrtle = new Turtle(150, 150, worldObj);  

    turt.prepareTurtleToDraw(lertle, Color.BLACK, 250, 150); 
    turt.drawLine(lertle, 250, 150, 400, 150); 
    turt.drawLine(lertle, 400, 150, 400, 250); 
    turt.drawLine(lertle, 400, 250, 250, 250); 
    turt.drawLine(lertle, 250, 250, 250, 150); 

    turt.prepareTurtleToDraw(dyrtle, Color.RED, 150, 150); 
    turt.drawLine(dyrtle, 150, 150, 260, 75); 
    turt.drawLine(dyrtle, 260, 75, 335, 150); 
    turt.drawLine(dyrtle, 335, 150, 225, 225); 
    turt.drawLine(dyrtle, 225, 225, 150, 150); 

    System.out.println(worldObj); 
} 

public static void pressC() 
{ 
    String userInput = "";         //declare and initialize a String variable 
    char key = ' ';           //declare and initialize a char variable 
    Scanner in = new Scanner(System.in);     //construct a Scanner object 

    System.out.println("Please press the c key to watch the animation."); 

    //do-while loop to wait for the user to enter the letter c 
    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable 
     World worldObj = new World(); 
     Animation turt = new Animation(); 


     Turtle dertle = new Turtle(250, 150, worldObj); 
     Turtle vyrtle = new Turtle(150, 150, worldObj); 

     turt.prepareTurtleToDraw(dertle, Color.WHITE, 250, 150); 
     turt.drawLine(dertle, 250, 150, 400, 150); 
     turt.drawLine(dertle, 400, 150, 400, 250); 
     turt.drawLine(dertle, 400, 250, 250, 250); 
     turt.drawLine(dertle, 250, 250, 250, 150); 

     turt.prepareTurtleToDraw(vyrtle, Color.WHITE, 150, 150); 
     turt.drawLine(vyrtle, 150, 150, 260, 75); 
     turt.drawLine(vyrtle, 260, 75, 335, 150); 
     turt.drawLine(vyrtle, 335, 150, 225, 225); 
     turt.drawLine(vyrtle, 225, 225, 150, 150); 
    } 
    while(key != 'c');          //do-while condition statement 

    System.out.println("Thank you. You may continue"); 

}//end of main method 

} 

public class AnimationTester 
{ 
public static void main(String[] args) 
{   
    Animation animate = new Animation(); 

    World worldObj = new World(); 
    Animation turt = new Animation(); 

    Turtle surtle = new Turtle(-50, 120, worldObj); 

    turt.prepareTurtleToDraw(surtle, Color.RED, -50, 120); 
    turt.drawLine(surtle, -50, 120, 60, 75); 
    turt.drawLine(surtle, 60, 75, 135, 150); 
    turt.drawLine(surtle, 135, 150, 25, 225); 
    turt.drawLine(surtle, 25, 225, -50, 120); 

    System.out.println(worldObj); 
    String userInput = "";         //declare and initialize a String variable 
    char key = ' ';           //declare and initialize a char variable 
    Scanner in = new Scanner(System.in);     //construct a Scanner object 

    System.out.println("Please press the c key multiple times to watch the animation."); 

    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable   

     Turtle kurtle = new Turtle(-50, 120, worldObj); 

     turt.prepareTurtleToDraw(kurtle, Color.WHITE, -50, 120); 
     turt.drawLine(kurtle, -50, 120, 60, 75); 
     turt.drawLine(kurtle, 60, 75, 135, 150); 
     turt.drawLine(kurtle, 135, 150, 25, 225); 
     turt.drawLine(kurtle, 25, 225, -50, 120); 


     Turtle turtle = new Turtle(100, 150, worldObj); 

     turt.prepareTurtleToDraw(turtle, Color.BLACK, 100, 150); 
     turt.drawLine(turtle, 50, 150, 200, 150); 
     turt.drawLine(turtle, 200, 150, 200, 250); 
     turt.drawLine(turtle, 200, 250, 50, 250); 
     turt.drawLine(turtle, 50, 250, 50, 150); 
     } 
     while(key != 'c');          //do-while condition statement 


    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable   


     Turtle burtle = new Turtle(100, 150, worldObj); 

     turt.prepareTurtleToDraw(burtle, Color.WHITE, 100, 150); 
     turt.drawLine(burtle, 50, 150, 200, 150); 
     turt.drawLine(burtle, 200, 150, 200, 250); 
     turt.drawLine(burtle, 200, 250, 50, 250); 
     turt.drawLine(burtle, 50, 250, 50, 150); 

     Turtle dyrtle = new Turtle(150, 150, worldObj); 

     turt.prepareTurtleToDraw(dyrtle, Color.RED, 150, 150); 
     turt.drawLine(dyrtle, 150, 150, 260, 75); 
     turt.drawLine(dyrtle, 260, 75, 335, 150); 
     turt.drawLine(dyrtle, 335, 150, 225, 225); 
     turt.drawLine(dyrtle, 225, 225, 150, 150); 

     } 
     while(key != 'c');          //do-while condition statement 



    //do-while loop to wait for the user to enter the letter c 
    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable   


     Turtle vyrtle = new Turtle(150, 150, worldObj); 

     turt.prepareTurtleToDraw(vyrtle, Color.WHITE, 150, 150); 
     turt.drawLine(vyrtle, 150, 150, 260, 75); 
     turt.drawLine(vyrtle, 260, 75, 335, 150); 
     turt.drawLine(vyrtle, 335, 150, 225, 225); 
     turt.drawLine(vyrtle, 225, 225, 150, 150); 

     Turtle mertle = new Turtle(300, 150, worldObj);  

     turt.prepareTurtleToDraw(mertle, Color.BLACK, 250, 150); 
     turt.drawLine(mertle, 250, 150, 400, 150); 
     turt.drawLine(mertle, 400, 150, 400, 250); 
     turt.drawLine(mertle, 400, 250, 250, 250); 
     turt.drawLine(mertle, 250, 250, 250, 150); 



     } 
     while(key != 'c');          //do-while condition statement 

    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable 


     Turtle dertle = new Turtle(250, 150, worldObj); 

     turt.prepareTurtleToDraw(dertle, Color.WHITE, 250, 150); 
     turt.drawLine(dertle, 250, 150, 400, 150); 
     turt.drawLine(dertle, 400, 150, 400, 250); 
     turt.drawLine(dertle, 400, 250, 250, 250); 
     turt.drawLine(dertle, 250, 250, 250, 150); 

     Turtle qyrtle = new Turtle(150, 150, worldObj); 

     turt.prepareTurtleToDraw(qyrtle, Color.RED, 325, 150); 
     turt.drawLine(qyrtle, 325, 150, 435, 75); 
     turt.drawLine(qyrtle, 435, 75, 510, 150); 
     turt.drawLine(qyrtle, 510, 150, 400, 225); 
     turt.drawLine(qyrtle, 400, 225, 325, 150); 


    } 
    while(key != 'c');          //do-while condition statement 

    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable 

     Turtle myrtle = new Turtle(325, 150, worldObj); 

     turt.prepareTurtleToDraw(myrtle, Color.WHITE, 325, 150); 
     turt.drawLine(myrtle, 325, 150, 435, 75); 
     turt.drawLine(myrtle, 435, 75, 510, 150); 
     turt.drawLine(myrtle, 510, 150, 400, 225); 
     turt.drawLine(myrtle, 400, 225, 325, 150); 

     Turtle bertle = new Turtle(500, 150, worldObj); 

     turt.prepareTurtleToDraw(bertle, Color.BLACK, 500, 150); 
     turt.drawLine(bertle, 425, 150, 575, 150); 
     turt.drawLine(bertle, 575, 150, 575, 250); 
     turt.drawLine(bertle, 575, 250, 425, 250); 
     turt.drawLine(bertle, 425, 250, 425, 150); 


    } 
    while(key != 'c');          //do-while condition statement 

    do 
    { 
     userInput = in.next();         //accept one token from the keyboard 
     in.nextLine();           //flush the buffer 
     key = userInput.charAt(0);        //picks off the first character from the userInput String variable    


     Turtle mertle = new Turtle(500, 150, worldObj); 

     turt.prepareTurtleToDraw(mertle, Color.WHITE, 500, 150); 
     turt.drawLine(mertle, 425, 150, 575, 150); 
     turt.drawLine(mertle, 575, 150, 575, 250); 
     turt.drawLine(mertle, 575, 250, 425, 250); 
     turt.drawLine(mertle, 425, 250, 425, 150); 

    } 
    while(key != 'c');          //do-while condition statement 
    System.out.println(worldObj); 
}  
} 

Java类只是一个对象的蓝图。所以你必须先创建对象或者实例化类。实例化一个对象是通过调用类名称,赋值给它一个变量,然后赋值给对象构造函数来完成的。

Animation animation = new Animation(); 

创建对象后,您可以使用“。”或点运算符来调用在类文件中创建的方法。

animation.drawLine(myrtle, x1, y1, x2, y2); 
animation.prepareTurtleToDraw(myrtle, color, x, y); 
animation.pressC(); 

从类文件中调用静态方法。你不需要实例化对象。只需使用类名后跟“。”。或点运算符。

Animation.animation(); 

所以

public class AnimationTester1 
{ 
    public static void main(String[] args) 
    {   
     AnimationGraphics animate = new AnimationGraphics(); 

     Animation animation = new Animation(); 
     animation.drawLine(myrtle, x1, y1, x2, y2); 
     animation.prepareTurtleToDraw(myrtle, color, x, y); 
     animation.pressC(); 
     Animation.animation(); 

    }  
} 
+0

这解决了我的问题,但事情是我没有需要包括:animation.drawLine(番石榴,X1,Y1,X2,Y2); animation.prepareTurtleToDraw(myrtle,color,x,y);为了它的工作。我还把我的前两节课结合在一起。 –

通过看你的问题:“我无法显示从我第一类中的方法为我的第二类的主要方法。”

要访问另一个类的类的方法: 1)访问一个非静态方法: - 我们必须创建第一个类的对象到第二个类。然后使用我们称之为第一类公共方法的对象。

2)访问静态方法: - 通过使用类名称可以调用一个类的公共方法。

class First 
    { 
     public void fun() 
     { 
      System.out.println("fun called"); 
     } 

     public static void static_fun() 
     { 
      System.out.println("static_fun called"); 
     } 

    } 


class Second 
{ 
    public static void main(String args[]) 

    //calling non-static method 
    First obj=new First(); 
    obj.fun(); 

    //calling static method 
    First.static_fun() 
}