4 changed files with 46 additions and 0 deletions
@ -0,0 +1,4 @@ |
|||||
|
1.利用AI复习泛型相关知识 |
||||
|
2.查询的静态方法的相关知识,了解了静态方法是使用 static 关键字修饰的方法,它属于类本身 ,而不是类的实例 |
||||
|
3.查询swap方法的使用场景,用于交换两个数据的位置或值 |
||||
|
4.了解了静态方法的使用原则,如果方法不依赖于对象的状态,就使用静态方法 |
||||
@ -0,0 +1,29 @@ |
|||||
|
public class Fanxing<K, V> { |
||||
|
private K key; |
||||
|
private V value; |
||||
|
|
||||
|
public Fanxing(K key, V value) { |
||||
|
this.key = key; |
||||
|
this.value = value; |
||||
|
} |
||||
|
|
||||
|
public K getKey() { |
||||
|
return key; |
||||
|
} |
||||
|
|
||||
|
public V getValue() { |
||||
|
return value; |
||||
|
} |
||||
|
|
||||
|
public void setKey(K key) { |
||||
|
this.key = key; |
||||
|
} |
||||
|
|
||||
|
public void setValue(V value) { |
||||
|
this.value = value; |
||||
|
} |
||||
|
|
||||
|
public static <K, V> Fanxing<V, K> swap(Fanxing<K, V> pair) { |
||||
|
return new Fanxing<>(pair.getValue(), pair.getKey()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
public class Fanxingtest { |
||||
|
public static void main(String[] args) { |
||||
|
Fanxing<Integer, String> fanxing = new Fanxing<>(888, "Fanxingtest"); |
||||
|
System.out.println("原始Pair:"); |
||||
|
System.out.println("Key: " + fanxing.getKey()); |
||||
|
System.out.println("Value: " + fanxing.getValue()); |
||||
|
|
||||
|
Fanxing<String, Integer> swapped = Fanxing.swap(fanxing); |
||||
|
System.out.println("\n交换后的Pair:"); |
||||
|
System.out.println("Key: " + swapped.getKey()); |
||||
|
System.out.println("Value: " + swapped.getValue()); |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 231 KiB |
Loading…
Reference in new issue