Java 构造矩形

Java 构造矩形,java,Java,我正在构造一个矩形类。我试了很多次,看了很多教程,但我的程序不起作用。这是我目前的代码: public class Rectangle { public static void main(String[] args) { Rectangle box = new Rectangle(5,10,20,30){ System.out.println(new Rectangle());} } 有几件事需要指出。首先,您的代码无法编译。试试这个 imp

我正在构造一个
矩形
类。我试了很多次,看了很多教程,但我的程序不起作用。这是我目前的代码:

public class Rectangle {

    public static void main(String[] args) {

        Rectangle box = new Rectangle(5,10,20,30){
            System.out.println(new Rectangle());}
}

有几件事需要指出。首先,您的代码无法编译。试试这个

import java.awt.Rectangle;

public class RectangleExample {

    public static void main(String[] args) {
        Rectangle box= new Rectangle(5,10,20,30);
        System.out.println(box);
    } 
}
你需要一个
矩形行末尾的分号,而不是括号
{
。此外,最好将类重命名为与矩形不同的名称,以防止在调用
矩形
类时出现兼容性问题。此外,在打印矩形时,您希望使用框引用,而不是构造新的矩形

第二,这不会在屏幕上或窗口中绘制矩形,这将只打印矩形
toString
方法。根据,该方法将只打印
表示此矩形对象坐标和大小值的字符串。

如果您想实际绘制一个矩形,您需要查看类似JFrame的内容,或者查看
paint
,使用类似于此的内容 我假设您不熟悉Java语言的语法,并试图拼凑您的第一个Java类

我还假设您需要以下东西:

  • 矩形的定义
  • 属性
    x
    y
    ,指示
    矩形的原点
  • 属性
    宽度
    高度
    ,指示
    矩形
    的尺寸
  • 通过指定这四个关键属性来创建
    矩形的功能
  • 显示现有
    矩形的属性的功能
  • 你已经拥有了什么 现在,你在这件事上有一个好的开始,但有点不顺心

    您的第一行和最后一行:

    public class Rectangle {
    
    }
    
    定义矩形类。它作为代码单元和创建对象的模板。类定义了方法(行为)和字段/属性(信息),用于指示该类的对象如何操作,并描述每个对象上可访问的数据

    简言之,这定义了可以创建的一般类型的对象;在您的示例中,您定义了对象在代码中成为
    矩形的含义

    您的第二行及其后续内容:

    public static void main(String[] args) {
    
    }
    
    定义一个名为
    main
    的静态方法。这里有很多东西要解释,但现在,只需将其视为Java程序的入口点。如果要将该类作为Java程序运行,该方法将(几乎)是第一个运行的代码

    下一行:

    Rectangle box = new Rectangle(5,10,20,30);   // notice the correction here, no curly braces, but a semicolon to end the line of code.
    
    System.out.println(new Rectangle())  // NOTE: This is incorrect, see below
    
    您将声明一个名为
    box
    的矩形,然后创建一个新的矩形对象,并将该对象分配给名为
    box
    的变量。换句话说,您正在创建一个名为
    box
    的新
    矩形

    关键字
    new
    表示您正在创建一个类型为
    Rectangle
    的新对象。括号中的数字是参数,用于定义所创建对象的某些内容。这些参数被传递到
    Rectangle
    类的构造函数方法中,该类将这些参数放入新创建的对象中

    不过,现在还没有构造函数,所以您的代码在这方面无法正常工作。我会写一个,继续阅读

    下一行:

    Rectangle box = new Rectangle(5,10,20,30);   // notice the correction here, no curly braces, but a semicolon to end the line of code.
    
    System.out.println(new Rectangle())  // NOTE: This is incorrect, see below
    
    也有点复杂。基本上,
    System.out.println
    方法将文本写入程序的控制台,用户可以在控制台中看到文本。它用于从程序中写入消息,或向屏幕显示程序中变量的内容

    现在,您正在向它传递一个新的
    Rectangle
    对象,它没有真正意义。
    Rectangle
    不是文本,也不是基本变量(如整数、字节、十进制数、文本的单个字符或真/假值),因此,如果您尝试将新的矩形对象打印到屏幕上,您将看到Java提供的一些gobbledy gook文本,如下所示:

    Rectangle@1a2fc866
    
    public class Rectangle {
        public int x; // Defined with a scope (public), a type (int), and a name (x).
        public int y;
        public int width, height; // We can do multiple on the same line, if they share the same scope and type.
    }
    
    Rectangle@1a2fc866
    
    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        public Rectangle(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            width = w;
            height = h;
        }
    
        public static void main(String[] args) {
            Rectangle box = new Rectangle(5, 10, 20, 30);
            System.out.println(box);
        }
    
        // Here is our toString method.
        // It's declared as an @Override, which means it overrides the toString method provided by the class that Rectangle is based on (in this case, java.lang.Object).
        // The method is in the public scope, returns a String-type value, and is called toString. It doesn't have parameters, so it gets empty parentheses.
        @Override
        public String toString() {
            // Now, we have to return a value from this method.
    
            // Start by declaring a local variable and filling it with some data.
            String stringValue = "Rectangle with location {";
    
            // We can "add" strings together to form a bigger string with the contents mashed next to each other (aka "concatenated").
            stringValue = stringValue + this.x + ",";
    
            // By "adding" this.y (the current object's y property) to a string, it also gets converted from an integer to a string without us needing to say anything special.
            stringValue = stringValue + this.y + "}";
    
            // We can take some shortcuts, too, by using the += operator so we don't have to rewrite stringValue twice every time!
            stringValue += ", width: " + this.width;
    
            // Remember, we don't need to use "this" when the name is not ambiguous, but it typically makes it clearer that some data is coming from the object instead of a local variable.
            stringValue += ", height: " + height;
    
            // Once we have a result value and no other code needs to be executed, we have to *return* it as the result of the method. Constructors and methods with "void" as their return type do not do this (though void methods can just say "return;")
            return stringValue;
        }
    }
    
    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        public Rectangle(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            width = w;
            height = h;
        }
    
        public static void main(String[] args) {
            Rectangle box = new Rectangle(5, 10, 20, 30);
            System.out.println(box);
        }
    
        @Override
        public String toString() {
            return "Rectangle with location {" + x + "," + y + "}, width: " + width + ", height: " + height;
        }
    }
    
    然而,我们有一个漂亮的小特性,让我们告诉Java如何将特定类的对象转换为文本字符串;我将在下面向您展示

    需要做什么 首先,我们需要一个矩形类

    public class Rectangle {
    
    }
    
    好了!我们已经定义了一个矩形类,用于创建矩形对象。现在,它需要一些属性。我们会说这些是整数,称为
    x
    y
    宽度
    ,和
    高度
    。我们的类现在看起来如下:

    Rectangle@1a2fc866
    
    public class Rectangle {
        public int x; // Defined with a scope (public), a type (int), and a name (x).
        public int y;
        public int width, height; // We can do multiple on the same line, if they share the same scope and type.
    }
    
    Rectangle@1a2fc866
    
    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        public Rectangle(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            width = w;
            height = h;
        }
    
        public static void main(String[] args) {
            Rectangle box = new Rectangle(5, 10, 20, 30);
            System.out.println(box);
        }
    
        // Here is our toString method.
        // It's declared as an @Override, which means it overrides the toString method provided by the class that Rectangle is based on (in this case, java.lang.Object).
        // The method is in the public scope, returns a String-type value, and is called toString. It doesn't have parameters, so it gets empty parentheses.
        @Override
        public String toString() {
            // Now, we have to return a value from this method.
    
            // Start by declaring a local variable and filling it with some data.
            String stringValue = "Rectangle with location {";
    
            // We can "add" strings together to form a bigger string with the contents mashed next to each other (aka "concatenated").
            stringValue = stringValue + this.x + ",";
    
            // By "adding" this.y (the current object's y property) to a string, it also gets converted from an integer to a string without us needing to say anything special.
            stringValue = stringValue + this.y + "}";
    
            // We can take some shortcuts, too, by using the += operator so we don't have to rewrite stringValue twice every time!
            stringValue += ", width: " + this.width;
    
            // Remember, we don't need to use "this" when the name is not ambiguous, but it typically makes it clearer that some data is coming from the object instead of a local variable.
            stringValue += ", height: " + height;
    
            // Once we have a result value and no other code needs to be executed, we have to *return* it as the result of the method. Constructors and methods with "void" as their return type do not do this (though void methods can just say "return;")
            return stringValue;
        }
    }
    
    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        public Rectangle(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            width = w;
            height = h;
        }
    
        public static void main(String[] args) {
            Rectangle box = new Rectangle(5, 10, 20, 30);
            System.out.println(box);
        }
    
        @Override
        public String toString() {
            return "Rectangle with location {" + x + "," + y + "}, width: " + width + ", height: " + height;
        }
    }
    
    好的,酷。现在,我们如何制作一个
    矩形
    ?使用构造函数方法!如果我们在制作一个新的
    矩形时不需要关于
    矩形的信息,我们可以省去它,使用Java提供给我们的默认构造函数,但是我们确实需要关于矩形的位置、宽度和高度的信息。我们的代码会增长对此:

    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        // This line defines a constructor with 4 parameters: x, y, w, and h.
        public Rectangle(int x, int y, int w, int h) {
            // This initializes our x, y, width, and height properties to what is passed in.
            this.x = x;    // We set our current object's x property to the x we're given.
            this.y = y;    // We have to specify which y is which, so we use this.y to indicate the current object, and y to specify the parameter we're given.
            width = w;     // We can leave off the "this" part if there's nothing else named "width" visible in the method.
            height = h;
        }
    }
    
    现在,我们可以创建一个
    矩形
    对象,并指定其位置、宽度和高度。但我们没有程序,因为没有入口点。让我们像最初那样添加一个,但修复了原始代码中的一些问题:

    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        public Rectangle(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            width = w;
            height = h;
        }
    
        // This is our program's entry point. It's static, and belongs to the Rectangle class, and NOT to the Rectangle objects.
        public static void main(String[] args) {
            // Inside our main method, let's make a new rectangle object.
            // Based on our constructor, the position is {5, 10}, the width is 20, and the height is 30.
            Rectangle box = new Rectangle(5, 10, 20, 30);
    
            // Now that it's created and named "box", let's print it out!
            // We pass the box variable as an argument to System.out.println,
            // and that method prints the box as if it were a string.
            System.out.println(box);
        }
    }
    
    我们现在有了一个程序的入口点,它可以做一些事情。我们创建一个矩形,将其分配给名为
    box
    的矩形变量。然后,我们尝试将
    box
    作为字符串打印到控制台窗口。如前所述,如果我们现在运行程序,我们将得到如下控制台输出:

    Rectangle@1a2fc866
    
    public class Rectangle {
        public int x; // Defined with a scope (public), a type (int), and a name (x).
        public int y;
        public int width, height; // We can do multiple on the same line, if they share the same scope and type.
    }
    
    Rectangle@1a2fc866
    
    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        public Rectangle(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            width = w;
            height = h;
        }
    
        public static void main(String[] args) {
            Rectangle box = new Rectangle(5, 10, 20, 30);
            System.out.println(box);
        }
    
        // Here is our toString method.
        // It's declared as an @Override, which means it overrides the toString method provided by the class that Rectangle is based on (in this case, java.lang.Object).
        // The method is in the public scope, returns a String-type value, and is called toString. It doesn't have parameters, so it gets empty parentheses.
        @Override
        public String toString() {
            // Now, we have to return a value from this method.
    
            // Start by declaring a local variable and filling it with some data.
            String stringValue = "Rectangle with location {";
    
            // We can "add" strings together to form a bigger string with the contents mashed next to each other (aka "concatenated").
            stringValue = stringValue + this.x + ",";
    
            // By "adding" this.y (the current object's y property) to a string, it also gets converted from an integer to a string without us needing to say anything special.
            stringValue = stringValue + this.y + "}";
    
            // We can take some shortcuts, too, by using the += operator so we don't have to rewrite stringValue twice every time!
            stringValue += ", width: " + this.width;
    
            // Remember, we don't need to use "this" when the name is not ambiguous, but it typically makes it clearer that some data is coming from the object instead of a local variable.
            stringValue += ", height: " + height;
    
            // Once we have a result value and no other code needs to be executed, we have to *return* it as the result of the method. Constructors and methods with "void" as their return type do not do this (though void methods can just say "return;")
            return stringValue;
        }
    }
    
    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        public Rectangle(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            width = w;
            height = h;
        }
    
        public static void main(String[] args) {
            Rectangle box = new Rectangle(5, 10, 20, 30);
            System.out.println(box);
        }
    
        @Override
        public String toString() {
            return "Rectangle with location {" + x + "," + y + "}, width: " + width + ", height: " + height;
        }
    }
    
    这不是我们想要的;我们想看看矩形的位置、高度和宽度是什么!因此,让我们通过添加
    toString()
    方法来改变Java将矩形对象转换为文本字符串的方式。我们的矩形类现在如下所示:

    Rectangle@1a2fc866
    
    public class Rectangle {
        public int x; // Defined with a scope (public), a type (int), and a name (x).
        public int y;
        public int width, height; // We can do multiple on the same line, if they share the same scope and type.
    }
    
    Rectangle@1a2fc866
    
    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        public Rectangle(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            width = w;
            height = h;
        }
    
        public static void main(String[] args) {
            Rectangle box = new Rectangle(5, 10, 20, 30);
            System.out.println(box);
        }
    
        // Here is our toString method.
        // It's declared as an @Override, which means it overrides the toString method provided by the class that Rectangle is based on (in this case, java.lang.Object).
        // The method is in the public scope, returns a String-type value, and is called toString. It doesn't have parameters, so it gets empty parentheses.
        @Override
        public String toString() {
            // Now, we have to return a value from this method.
    
            // Start by declaring a local variable and filling it with some data.
            String stringValue = "Rectangle with location {";
    
            // We can "add" strings together to form a bigger string with the contents mashed next to each other (aka "concatenated").
            stringValue = stringValue + this.x + ",";
    
            // By "adding" this.y (the current object's y property) to a string, it also gets converted from an integer to a string without us needing to say anything special.
            stringValue = stringValue + this.y + "}";
    
            // We can take some shortcuts, too, by using the += operator so we don't have to rewrite stringValue twice every time!
            stringValue += ", width: " + this.width;
    
            // Remember, we don't need to use "this" when the name is not ambiguous, but it typically makes it clearer that some data is coming from the object instead of a local variable.
            stringValue += ", height: " + height;
    
            // Once we have a result value and no other code needs to be executed, we have to *return* it as the result of the method. Constructors and methods with "void" as their return type do not do this (though void methods can just say "return;")
            return stringValue;
        }
    }
    
    public class Rectangle {
        public int x;
        public int y;
        public int width, height;
    
        public Rectangle(int x, int y, int w, int h) {
            this.x = x;
            this.y = y;
            width = w;
            height = h;
        }
    
        public static void main(String[] args) {
            Rectangle box = new Rectangle(5, 10, 20, 30);
            System.out.println(box);
        }
    
        @Override
        public String toString() {
            return "Rectangle with location {" + x + "," + y + "}, width: " + width + ", height: " + height;
        }
    }
    
    现在,当我们运行程序时,它会打印出t