简体   繁体   English

对象属性的Java传值值解决方法

[英]Java pass-by-value workaround for object properties

In one method of my Java source code, the same block of code occurs many times: 在我的Java源代码的一种方法中,相同的代码块多次出现:

<some code>
<some code>
<block of code>
<some code>
<block of code>
<some code>
<some code>
<some code>
<block of code>
<some code>
<some code>
<block of code>
<some code>
etc..

What this block of code does is it finds out what property of an object should be the target of the code to follow. 这段代码的作用是找出对象的哪些属性应该是要遵循的代码的目标。 More specifically, at the beginning of the block we have a string "Object.property", and the task of the code is to parse it so that it finds out what type of an object "Object" is, and what property of "Object" "property" is. 更具体地说,在块的开头我们有一个字符串“Object.property”,代码的任务是解析它,以便找出对象“Object”的类型,以及“Object”的属性“”财产“是。 When that is done, and the block is over, the execution continues to do something with said property of said object. 当完成该操作并且块结束时,执行继续对所述对象的所述属性执行某些操作。 The property could be used in a comparison, or something could be assigned to it. 该属性可用于比较,或者可以分配给它。

Now, my problem is that I would like to tidy up the code a little bit by placing this block inside another method, which I then could call multiple times instead. 现在,我的问题是我想通过将这个块放在另一个方法中来稍微整理代码,然后我可以多次调用它。 Furthermore, if I ever would find a bug in this block, I wouldn't need to go through every occurence to fix it - just the one method. 此外,如果我在这个区块中发现了一个错误,我就不需要经历每一次修复它 - 只需要一种方法。

So this is basically what I want to do: 所以这基本上就是我想做的事情:

<some code>
<some code>
property = parse();
<some code>
property = parse();
<some code>
<some code>
<some code>
property = parse();
<some code>
<some code>
property = parse();
<some code>
etc..

property parse(){
 <block of code>
 return property;
}

Now, thanks to Java not having any pointers I can't for example return a property that is an integer from this new method. 现在,由于Java没有任何指针,我不能举例说明从这个新方法返回一个整数属性。 A copy of that integer would be returned (a pass-by-value), and all the rest of my code would be dealing with is the local integer copy of a property, and not the property itself. 将返回该整数的副本(按值传递),我的所有其余代码将处理的是属性的本地整数副本,而不是属性本身。 This is fine of course if the property only is to be used in a comparison, but when the rest of my code want's to assign something to it, things don't work anymore. 当然,如果仅在比较中使用属性,但当我的其余代码想要为其分配内容时,这样就不行了。

Is there a smart and simple workaround, so that I could shorten my Java code, and when modifying it, I'd only need to modify one part of the code? 是否有一个聪明而简单的解决方法,以便我可以缩短我的Java代码,并在修改它时,我只需要修改代码的一部分?

EDIT: 编辑:

To give you some real code, the block of code could look like this, when it's not placed in a different method, after it has gotten a string named toParse (that for this examples sake could be "niceShirt.size"): 为了给你一些真正的代码,代码块看起来像这样,当它没有放在一个不同的方法中,在它得到一个名为toParse的字符串后(为了这个例子,可以是“niceShirt.size”):

String[] parts = toParse.split('\\.');
if(pantsList.containsKey(parts[0])){
    if(parts[1] == "size") pantsList.get(parts[0]).size += 1;
    if(parts[1] == "daysWorn") pantsList.get(parts[0]).daysWorn += 1;
}
if(shirtsList.containsKey(parts[0])){
    if(parts[1] == "size") shirtsList.get(parts[0]).size += 1;
    if(parts[1] == "daysWorn") shirtsList.get(parts[0]).daysWorn += 1;
}
if(shoesList.containsKey(parts[0])){
    if(parts[1] == "size") shoesList.get(parts[0]).size += 1;
    if(parts[1] == "daysWorn") shoesList.get(parts[0]).daysWorn += 1;
}

Now as code like this occurs many times, I'd love to have a method like this: 现在像这样的代码多次发生,我希望有这样的方法:

String[] parts = toParse.split('\\.');
if(pantsList.containsKey(parts[0])){
    if(parts[1] == "size") return pantsList.get(parts[0]).size;
    if(parts[1] == "daysWorn") return pantsList.get(parts[0]).daysWorn;
}
if(shirtsList.containsKey(parts[0])){
    if(parts[1] == "size") return shirtsList.get(parts[0]).size;
    if(parts[1] == "daysWorn") return shirtsList.get(parts[0]).daysWorn;
}
if(shoesList.containsKey(parts[0])){
    if(parts[1] == "size") return shoesList.get(parts[0]).size;
    if(parts[1] == "daysWorn") return shoesList.get(parts[0]).daysWorn;
}

and all I'd need to do in the rest of my code would be: 而我在其余代码中需要做的就是:

parse(toParse) += 1;

..but this doesn't work since the parse(toParse) is now a copy, a return-by-value, so it would do nothing to the real property of the object. ..但这不起作用,因为解析(toParse)现在是一个副本,一个按值返回,所以它对该对象的不动产无效。

I hope this helps clarify the problem? 我希望这有助于澄清问题?

You can wrap a getter and a setter in an object of an anonymous class implementing a simple interface: 您可以将getter和setter包装在实现简单接口的匿名类的对象中:

interface PropertyInfo {
    Object get();
    void set(Object value);
}

The parse() method would return an implementation of this interface that knows how to do a get and also knows how to do a set operation, based on other data passed in from the caller: parse()方法将返回此接口的实现,该接口知道如何执行get,并且还知道如何根据从调用者传入的其他数据执行set操作:

private PropertyInfo parse(...) { // take some parameters
    // Find your getter and setter through reflection
    final Method getter = ...
    final Method setter = ...
    return new PropertyInfo() {
        public Object get() {
            // Use the getter
        }
        public void set(Object value) {
            // Use the setter
        }
    };
}

When you need to use the property in a condition, you'd use something like this: 当您需要在条件中使用该属性时,您将使用以下内容:

PropertyInfo p = parse(...); // pass whatever needs to be parsed
if (p.get() == 123) ...

When you need to set the property, you'd be able to call it like this: 当您需要设置属性时,您可以像这样调用它:

PropertyInfo p = parse(...); // pass whatever needs to be parsed
p.set(123);

Make your method void and have the first argument be a "handle" object - a simple object that has an integer field. 使您的方法无效并使第一个参数成为“句柄”对象 - 一个具有整数字段的简单对象。 That object will be passed by value, but its value can be modified as much as you like. 该对象将按值传递,但其值可以根据需要进行修改。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM