java如何显示从一个类到另一个类的主方法的方法
我正在用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);
}
}
这是一个有效的版本,但它不是最有序的方式。对于这一个,所有的代码都在main方法中,这不是我试图改变的最佳格式
/**
* 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);
}
}
# 1 楼答案
java类只是对象的蓝图。因此,必须首先创建对象或实例化类。实例化一个对象是通过调用类名,为其分配一个变量,然后将其分配给对象构造函数来完成的
创建对象后,可以使用“”或点运算符调用在类文件中创建的方法
从类文件调用静态方法。您不需要实例化对象。只需使用类名后跟“.”或点运算符
所以
# 2 楼答案
通过查看您的问题:“我无法将第一个类中的方法显示为第二个类中的主方法。”
要将类的方法访问到另一个类,请执行以下操作: 1) 要访问非静态方法:-我们必须将第一个类的对象创建为第二个类。然后使用我们称之为第一类公共方法的对象
2)访问静态方法:-通过使用类名,可以调用类的公共方法