- public class TestPlus {
- public static void main(String[] args) {
- Cat01 cata = new Cat01("a",13);
- Cat01 catb = new Cat01("b",5);
-
- System.out.println("cata的名字为:"+cata.getName()+",cata的年龄为:"+cata.getAge());
- System.out.println("catb的名字为:"+catb.getName()+",catb的年龄为:"+catb.getAge());
-
- swap(cata,catb);
- System.out.println("交换后结果如下:");
-
- System.out.println("cata的名字为:"+cata.getName()+",cata的年龄为:"+cata.getAge());
- System.out.println("catb的名字为:"+catb.getName()+",catb的年龄为:"+catb.getAge());
- }
-
- public static void swap(Cat01 a,Cat01 b) {
- //前面的同学说过了,不能直接交换对象。
- String nameA = a.getName();
- String nameB = b.getName();
-
- a.setName(nameB);
- b.setName(nameA);
-
- int ageA = a.getAge();
- int ageB = b.getAge();
-
- a.setAge(ageB);
- b.setAge(ageA);
- }
- }
- class Cat01 {
- private String name;
- private int age;
-
- public Cat01(String name,int age) {
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
- }
复制代码 |