我在我的 main 中制作了一个对象Recipe recipeOne = new Recipe("Pepperoni Pizza");
该对象是此处定义和构造的对象数组的实例!
public class Recipe implements Cloneable{
String Name;
final int INGREDIENT_ARRAY_MAX = 10;
Ingredient Recipe[] = new Ingredient[INGREDIENT_ARRAY_MAX];
public Recipe(String name){
Name = name;
}
Run Code Online (Sandbox Code Playgroud)
所以我想用这条线制作这个对象的深层副本Recipe ressippi = (Recipe) recipe.clone();,它把我发送到这里!
public Object clone(){
Recipe cloneRec = new Recipe(Name);
return cloneRec;
}
Run Code Online (Sandbox Code Playgroud)
我知道这目前是一个浅拷贝,因为该方法只传递引用,所以如果我尝试对我的新对象(它是recipeOne 的克隆)进行名称更改...它将更改它们的两个名称。显然我不希望这样,我对此很迷茫,有人可以帮忙吗?
编辑:@Rohit Jain
我的 Recipe 类和 Ingredient 类(菜谱数组保存的对象)都有 toString 方法,并且 Recipe 调用配料,以便以漂亮的小格式将其全部打印出来。当我在我的“recipeOne”对象(称为意大利辣香肠披萨的那个)上调用它时,我得到“意大利辣香肠披萨:1.0 磅面团,8.0 盎司酱汁,10.0 盎司奶酪”
然后我继续创建 ressippi 对象并将其设置为recipeOne 的克隆,所以从这里开始一切都很好...然后我将 ressippi 的名称更改为“Pineapple Pizza”,打印效果很好,但它不打印recipeOne 的 3 个成分对象存储,这是它应该做的!
我是编程以及本网站的新手,所以如果我搞砸了,请原谅我.我有一点时间搞清楚如何在这里正确发布我的代码.
package tester;
import java.util.*;
public class Mainclass2 {
public static void main(String[] args) {
int y = 3;
int[] x = {1, 2, 3, 4};
editnumbersArray(x);
editnumbersNotArray(y);
System.out.println(x[2]); **//now this was changed from 3 to 9...**
System.out.println(y); //but this one went unchanged.
}
//this accepts 'x[]' into the 'a[]' parameter.
public static void editnumbersArray(int[] a){
a[2] = 9; **//<---why does this one CHANGE the actual x[2] instead of just a[2]?**
}
//this accepts 'y' into the 'a' parameter.
public …Run Code Online (Sandbox Code Playgroud) 我总是理解静态变量在引用时共享一个实例.我想对此进行测试,但结果与我预期的不同.
static Integer counter = 0;
static Integer test = counter;
public static void main(String args[]) {
counter++;
System.out.println("counter: " + counter);
System.out.println("test: " + test);
}
Run Code Online (Sandbox Code Playgroud)
输出:
柜台:1
测试:0
由于test引用counter我认为,当我增加counter那么test将自动以及递增.但是,似乎test是0从某个地方引用,问题是,在哪里?
我在想.是否存在仅使用传递引用作为其评估策略的语言?
可能重复:
Java是否通过引用传递?
因此,请考虑以下两个示例及其各自的输出:
public class LooksLikePassByValue {
public static void main(String[] args) {
Integer num = 1;
change(num);
System.out.println(num);
}
public static void change(Integer num)
{
num = 2;
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
public class LooksLikePassByReference {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("url", "www.google.com");
change(properties);
System.out.println(properties.getProperty("url"));
}
public static void change(Properties properties2)
{
properties2.setProperty("url", "www.yahoo.com");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
www.yahoo.com
为什么会这样www.yahoo.com?对我来说看起来不像passbyvalue.
以下是我的代码:
class Demo{
public static void main(String[] args) {
Integer i = new Integer(12);
System.out.println(i);
modify(i);
System.out.println(i);
}
private static void modify(Integer i) {
i= i + 1;
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)
}
上面CODE的OUTPUT是12 12,但据我所知,我们传递包装器对象"i",这意味着它应该是"按引用调用",然后该值应该更改为13.但是它的12.任何人都有对此有适当的解释?
我正在开发一个java应用程序,现在我注意到一个让我困惑的奇怪行为.我有以下情况:
Integer currentIndex = 0;
doSomethings(currentIndex);
System.out.println("Print "+currentIndex);
private void doSomethings(Integer currentIndex){
//Do other things and update my current index
currentIndex++;
}
Run Code Online (Sandbox Code Playgroud)
但我总是像价值一样.我记得对象在java中像引用一样传递,而原始类型如copy.为什么我在这种情况下得0?
我被困在一件简单的事情上.我有一系列名为"标签"的布尔值.能够通过布尔值访问数组的每个元素对我来说很重要:
public boolean energetic, calming, happy, sad;
public boolean[] trackTags = {energetic, calming, happy, sad};
Run Code Online (Sandbox Code Playgroud)
我传入并为trackTags数组分配布尔值(让我们说[true,true,true,false].所以,当我调用trackTags [0]时,我得到"真实".但是,当我打印"精力充沛"时 -它应该与trackTags [0]相同,值总是为false.我知道布尔值初始化为false,但是当我在trackTags数组中切换某些值为"true"时,命名元素是否也不会改变?
第二个问题:对于我与布尔变量名称交互的好方法是什么?具体来说,如果我传入一个String [] [happy,sad],然后只想切换对应于我的String数组中名称的boolean []值,这是否可行?我在循环遍历两个数组的元素时没有问题,但我显然无法将字符串与布尔值进行比较.
总而言之,是否有更好的方式与布尔名称进行交互?我真的很困惑.
public abstract class Class1 {
protected static Object object1 = null;
protected static Object object2 = null;
public static Object[] objects = { object1, object2 };
public static void main(String[] args) {
new Class2();
for (Object o : objects) {
System.out.println(o);
}
}
}
public class Class2 extends Class1 {
public Class2() {
Class1.object1 = new String("String 1");
Class1.object2 = new String("String 2");
}
}
Run Code Online (Sandbox Code Playgroud)
这输出:
null
null
Run Code Online (Sandbox Code Playgroud)
为什么?
当我创建一个新实例时Class2,该类的构造函数初始化object1和object2.
objects据我所知,包含对这些对象的引用.所以在他们初始化之后,我期待任何东西,但是无效.
有人能解释一下吗 谢谢.
能否请您解释为什么在下面的代码String和StringBuffer区别对待,当价值被附加StringBuffer,但不是在字符串。
public class MyClass {
public static void main(String args[]) {
String str = new String("String Rohan ");
StringBuffer strBfr = new StringBuffer("String Buffer Rohan ");
strUpdate(str);
strBfrUpdate(strBfr);
System.out.println(str);
System.out.println(strBfr);
}
private static void strBfrUpdate(StringBuffer strBfr){
strBfr.append("Kushwaha");
}
private static void strUpdate(String str){
str += "Kushwaha";
}
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
串罗汉
字符串缓冲区Rohan Kushwaha
java ×9
arrays ×2
deep-copy ×1
methods ×1
object ×1
parameters ×1
return-value ×1
string ×1
stringbuffer ×1
void ×1