gpt4 book ai didi

java - 在Java中,传入value的变量数量可以小于对象类中的变量数量吗?

转载 作者:行者123 更新时间:2023-12-01 06:31:57 25 4
gpt4 key购买 nike

这是 Oracle Java 教程网站的摘录。它没有显示实际的 .java 文件,但我猜测“矩形”是一个类。但如果您注意到, rectOne 和 rectTwo 向下传递的参数(按值?)是不同的。 (一个有 origin 变量,两个没有)

如果对象有一定数量的参数,实际传递的值数量是否可以少于这个数量?我假设默认情况下不能再多了。

此外,我搜索了答案但找不到。

// 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);

最佳答案

对象没有参数——方法或构造函数有。在这种情况下,基本上有两个重载构造函数。例如:

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

单个重载的参数表达式数量唯一可以变化的是可变参数:

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" })

关于java - 在Java中,传入value的变量数量可以小于对象类中的变量数量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38256127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com