[英]Can someone tell me what I'm doing wrong to setup this Robot class in Java?
I started learning Java last week so bear with me as I'm just getting the hang of things. 我上周开始学习Java,所以请耐心等待,因为我刚刚掌握了很多东西。 I'm trying to make a class that extends the Java Robot class. 我正在尝试创建一个扩展Java Robot类的类。
I am getting an "Identifier Expected" on this line: 我在此行上收到“期望的标识符”:
public ChanseyRobot(bot)
Robot Class: 机器人类别:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;
public class ChanseyRobot extends Robot
{
private Robot bot;
public ChanseyRobot(bot)
{
try
{
this.bot = new Robot();
}
catch (AWTException e)
{
throw new RuntimeException(e);
}
}
}
Main Class: 主类:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;
public class Main
{
public static void main(String args[])
{
ChanseyRobot robot = new ChanseyRobot(robot);
}
}
Change this: 更改此:
public ChanseyRobot(bot) { }
to 至
public ChanseyRobot(Robot bot) { }
You need to declare Data Type before the variable name, and it is a very basic of Java. 您需要在变量名称之前声明数据类型,这是Java的基础知识。
Read up on Java Inheritance 阅读Java继承
Your class IS A robot. 您的课程是机器人。 So it doesn't need to create a Robot bot
internally too. 因此,它也不需要在内部创建Robot bot
。
public class ChanseyRobot extends Robot
{
public ChanseyRobot()
{
}
}
And then just : 然后:
ChanseyRobot robot = new ChanseyRobot();
You need to provide the type of the parameter
in your constructor. 您需要在构造函数中提供 type of the parameter
的type of the parameter
。
change 更改
public ChanseyRobot(bot)
to 至
public ChanseyRobot(Robot bot) throws AWTException
You also need to declare AWTException
in your constructor declaration as Robot's default constructor throws AWTException. 您还需要在构造函数声明中声明AWTException
,因为Robot的默认构造函数会引发AWTException。
public Robot()
throws AWTException
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;
public class ChanseyRobot extends Robot
{
private Robot bot;
public ChanseyRobot()
{
try
{
this.bot = new Robot();
}
catch (AWTException e)
{
throw new RuntimeException(e);
}
}
}
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;
public class Main
{
public static void main(String args[])
{
ChanseyRobot robot = new ChanseyRobot();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.