这是Oracle Java教程网站的摘录.它没有显示实际的.java文件,但我猜"Rectangle"是一个类.但是如果你注意到,rectOne和rectTwo传递的参数(按值?)是不同的.(一个有原点变量而两个没有)
如果对象有一定数量的参数,实际传递的数值是否可以小于?我假设它默认情况下不能更多.
此外,我搜索了答案,但找不到.
// Declare and create a point object and two rectangle objects.
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 12
一个对象没有参数-方法或构造函数.在这种情况下,基本上有两个重载的构造函数.例如:
public Rectangle(Point origin, int width, int height)
{
this.origin = origin;
this.width = width;
this.height = height;
}
public Rectangle(int width, int height)
{
this(Point.ZERO, width, height);
}
Run Code Online (Sandbox Code Playgroud)
单个重载的参数表达式数量唯一可以变化的时间是varargs:
public void foo(String... bar)
{
...
}
foo("x"); // Equivalent to foo(new String[] { "x" })
foo("x", "y"); // Equivalent to foo(new String[] { "x", "y" })
foo("x", "y", "z"); // Equivalent to foo(new String[] { "x", "y", "z" })
Run Code Online (Sandbox Code Playgroud)