简体   繁体   English

有人可以告诉我在Java中设置此Robot类时我做错了什么吗?

[英]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 parametertype 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

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()
{
    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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人能告诉我我做错了什么吗? 通过LinkedList计数和循环 - Can someone tell me what i'm doing wrong? Counting and looping through LinkedList 谁能告诉我我在做什么错? -堆栈 - Can anyone tell me what I'm doing wrong? - Stacks HackerRank 任务“Mini Max Sum”解决方案未通过 13 个测试用例中的 3 个,有人能告诉我我做错了什么吗 - HackerRank Task “Mini Max Sum” solution not passing 3 of the 13 test cases, can someone tell me what i'm doing wrong 膨胀 class android.fragment.app.FragmentContainerView 时出错,有人可以告诉我我缺少什么 - Error inflating class android.fragment.app.FragmentContainerView, can someone tell me what I'm missing 当尝试根据给定查询查找给定字符串中的子字符串计数时,有人能告诉我我在 Java 代码中做错了什么吗? - Can someone tell me what am I doing wrong here in my Java code when trying to find count of substrings in given string according to given queries? 在 Java 中使用 generics,有人可以解释我做错了什么 - Using generics in Java, can someone please explain what I'm doing wrong 有人可以告诉我这个Java类做什么吗? I / O相关 - Can someone tell me what this Java class does? I/O related 有一种有效的“合并排序”功能,但不是完全可以,有人可以告诉我我做错了什么吗? - Have a sort of working Merge Sort, but not quite, could someone tell me what I am doing wrong? 请告诉我我做错了什么,if 语句和转换不起作用 - Please tell me what I'm doing wrong, the if statements and the conversions are not working Java将这种线程设置工作或我在做什么错 - java will this threading setup work or what can i be doing wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM