package third;
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
Person p1=new Person(12);
Person p2=new Person(23);
System.out.println("1:"+p1);
System.out.println("2:"+p2);
System.out.println("--------------------------------------------------");
swap(p1, p2);
System.out.println("1:"+p1);
System.out.println("2:"+p2);
System.out.println("--------------------------------------------------");
Person ptemp=p1;
p1=p2;
p2=ptemp;
System.out.println("1:"+p1);
System.out.println("2:"+p2);
}
public static void swap(Person p1,Person p2)
{
Person ptemp=p1;
p1=p2;
p2=ptemp;
}
public static class Person{
public int age;
public Person(int age)
{
this.age=age;
}
@Override
public String toString() {
return "age:"+age;
}
}
}
运行结果:
1:age:12
2:age:23
--------------------------------------------------
1:age:12
2:age:23
--------------------------------------------------
1:age:23
2:age:12
package third;
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
Person p1=new Person(12);
Person p2=new Person(23);
System.out.println("1:"+p1);
System.out.println("2:"+p2);
System.out.println("--------------------------------------------------");
swap(p1, p2);
System.out.println("1:"+p1);
System.out.println("2:"+p2);
System.out.println("--------------------------------------------------");
Person ptemp=p1;
p1=p2;
p2=ptemp;
System.out.println("1:"+p1);
System.out.println("2:"+p2);
}
public static void swap(Person p1,Person p2)
{
int age=p1.getAge();
Person temp=new Person(age);
p1.setAge(p2.getAge());
p2.setAge(temp.getAge());
}
public static class Person extends Object{
public int age;
public Person(int age)
{
this.age=age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "age:"+age;
}
}
}
运行结果:
1:age:12
2:age:23
--------------------------------------------------
1:age:23
2:age:12
--------------------------------------------------
1:age:12
2:age:23
public class forth {
public static void Swap(char[] ch,char c)
{
ch[0]='B';
c='D';
}
/**
* @param args
*/
public static void main(String[] args) {
char[] ch={'A','C'};
Swap(ch,ch[1]);
for(char c:ch)
{
System.out.println(c);
}
}
}
运行结果:
B
C