gpt4 book ai didi

java - 将值从一个对象继承到另一个对象

转载 作者:行者123 更新时间:2023-12-02 09:36:08 25 4
gpt4 key购买 nike

我这里有这段代码。Tuple 是一个使用 (int,int) 作为键来查找特定对象的类。我已经重写了 hashcode 和 equals,因为如果我有一个特定的 (x,y) 值,我希望能够进行比较。

import java.lang.Math; 
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;
import java.util.Queue;
import java.util.*;


class Tuple {
public Tuple (int x, int y) {
this.x = x;
this.y = y;
}
public String moves;
public int times;
public int getX(){
return this.x;

}
public int getY(){
return this.y;

}

@Override
public int hashCode() {
int hash = 17;
hash = 5 * hash + this.x;
hash = 5 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Tuple)) {
return false;
}
Tuple c = (Tuple) o;
return Double.compare(x, c.x) == 0
&& Double.compare(y, c.y) == 0;
}
private int x;
private int y;
}




public class HelloWorld
{

public static void main(String[] args)
{
Tuple t2=new Tuple(1,1);
t2.moves="aaa";

Tuple t1=new Tuple(1,1);
System.out.print(t1.moves);
}
}

是否有一种方法可以在不知道实际 t1 对象而仅通过 (1,1) 的情况下将 t1.moves 继承到 t2.moves?为了实现这一目标,字符串移动应该是公共(public)的吗?

最佳答案

听起来你需要一个 Key两个数字返回 String ,从你写问题的方式来看。为此,您可以使用 Map并以某种方式将其与您的两个数字联系起来。

最好的方法是在 int[] 上编写您自己的包装类。并覆盖 hashcodeequals 。在您的代码中,您基本上必须将代码分成 Class实现了这个想法,以及 Class包含 map .

下面我没有使用 Arrays.toString() 实现我自己的包装类来完成此操作。作为key而不是实际的数组。如果您想修改它,可以以此为基础来使用 Array 包装类。

下面是不使用包装类的完整功能代码:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

class Tuple {

private Map<String,String> map = new HashMap<>();

public Tuple() {
}

public String get(int x, int y) {
return map.get(Arrays.toString(new int[] {x,y}));
}

public void put(int x, int y, String str) {
this.map.put(Arrays.toString(new int[] {x,y}), str);
}

public Map<String, String> getMap() {
return map;
}

public void setMap(Map<String, String> map) {
this.map = map;
}

public static void main(String [] args) {

Tuple tup = new Tuple();

tup.put(1, 1, "I am 1 1!");
tup.put(2, 3, "I am 2 3!");
tup.put(4, 4, "I am 4 4!");

System.out.println(tup.get(1,1));
System.out.println(tup.get(2,3));
System.out.println(tup.get(4,4));
System.out.println(tup.get(3,2)); //Does not exist
}



}

输出:

I am 1 1!

I am 2 3!

I am 4 4!

null

此代码使用自定义 put接受输入为 int, int, String创建<Key, Value>并将其存储到 Map .

它还有一个get(int, int)当您需要获取原始 String 时可以使用它后退。牢记(x, y)(y, x) 不同.

此外,我建议使用 setter 和 getter 并将类变量设置为 private而不是公开它们。

编辑:

这里有一种方法可以通过删除 String 来使用当前代码来实现此目的。从里面Tuple并将其用作 Map值并使用当前的哈希实现:

class Tuple {

private int x;
private int y;
private int times;

public Tuple (int x, int y) {
this.x = x;
this.y = y;
}


@Override
public int hashCode() {
int hash = 17;
hash = 5 * hash + this.x;
hash = 5 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Tuple)) {
return false;
}
Tuple c = (Tuple) o;
return Double.compare(x, c.x) == 0
&& Double.compare(y, c.y) == 0;
}


public static void main(String [] args) {

Map<Tuple,String> map = new HashMap<>();

Tuple tup1 = new Tuple(1, 1);
Tuple tup2 = new Tuple(2, 2);
Tuple tup3 = new Tuple(3, 4);
map.put(tup1, "aaa");
map.put(tup2, "bbbb");
map.put(tup3, "dddddd");

System.out.println(map.get(new Tuple(1, 1)));
System.out.println(map.get(new Tuple(2, 2)));
System.out.println(map.get(new Tuple(3, 4)));
System.out.println(map.get(new Tuple(4, 3))); //Prints null

//OR LIKE YOUR EXAMPLE CASE
Tuple tup4 = new Tuple(1, 1);
System.out.println(map.get(tup4));
}

}

输出:

aaa

bbbb

dddddd

null

aaa

理想情况下您会使用 Class包装这个Map就像我原来的方法一样,所以你不需要使用 new Tuple(1, 1)获取String返回,因为您可以在内部创建一个方法,通过输入 int, int 为您完成此操作.

关于java - 将值从一个对象继承到另一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57501316/

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