java 复习笔记——java中的传参及对象的clone()方法

一)
在Java中,传参时,都是以传值的方式进行。
[color=red]对于基本数据类型,传递的是数据的拷贝;对于引用类型,传递的引用的拷贝。[/color]
二)
为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。
在派生类中覆盖基类的clone()方法,并声明为public。
在派生类的clone()方法中,调用super.clone()。
在派生类中实现Cloneable接口。
三)为什么我们在派生类中覆盖Object的clone()方法时,一定要调用super.clone()呢?
在运行时刻,Object中的clone()识别出你要复制的是哪一个对象,然后为此对象分配空间,并进行对象的复制,将原始对象的内容一一复制到新对象的存储空间中。
四)

class StringTest
{
public static void change(int x,int y)
{
x=x+y;
y=x-y;
x=x-y;
}
public static void change(int[] num)
{
num[0]=num[0]+num[1];
num[1]=num[0]-num[1];
num[0]=num[0]-num[1];
}
public static void change(Point pt)
{
pt.x=pt.x+pt.y;
pt.y=pt.x-pt.y;
pt.x=pt.x-pt.y;
}

public static void main(String[] args)
{
/*int x=3;
int y=4;
change(x,y);
System.out.println("x="+x+","+"y="+y);*/
/*int[] num=new int[]{3,4};
change(num);
System.out.println("x="+num[0]+","+"y="+num[1]);*/
/*Point pt=new Point();
pt.x=3;
pt.y=4;
change(pt);
//System.out.println("x="+pt.x+","+"y="+pt.y);
System.out.println(pt);*/
//System.out.println(args[0]);
/*if(args.length>0)
{
for(int i=0;i<args.length;i++)
{
System.out.println(args[i]);
}
}*/
/*String str1=new String("abc");
String str2=new String("abc");
String str3=str1;*/
/*if(str1==str2)
{
System.out.println("str1==str2");
}
else
{
System.out.println("str1!=str2");
}*/
/*if(str1==str3)
{
System.out.println("str1==str3");
}
else
{
System.out.println("str1!=str3");
}*/
/*if(str1.equals(str2))
{
System.out.println("str1 equals str2");
}
else
{
System.out.println("str1 not equals str2");
}*/
/*int i=3;
float f=1.5f;
char ch='f';
boolean b=false;
//System.out.println(str1+i+f+ch+b);
StringBuffer sb=new StringBuffer();
sb.append(str1).append(i).append(f).append(ch).append(b);
//System.out.println(sb.toString());
sb.delete(4,8);
sb.insert(4,"mybole");
System.out.println(sb);*/
/*int[] num=new int[3];
for(int i=0;i<num.length;i++)
{
System.out.println(num[i]);
}
num=null;*/
/*Student[] students;
students=new Student[3];
for(int i=0;i<students.length;i++)
{
System.out.println(students[i]);
}*/
Professor p=new Professor("wangwu",50);
Student s1=new Student("zhangsan",18,p);
Student s2=(Student)s1.clone();
/*s2.name="lisi"; //对于引用类型,调用clone()方法时,拷贝的是引用地址;String 比较特殊,
//因为String是常量,会在内存中另外分配一块内存
s2.age=20;*/
s2.p.name="lisi";
s2.p.age=30;
//System.out.println("name="+s1.name+","+"age="+s1.age);
System.out.println("name="+s1.p.name+","+"age="+s1.p.age);
}
}

class Professor implements Cloneable
{
String name;
int age;
Professor(String name,int age)
{
this.name=name;
this.age=age;
}
public Object clone()
{
Object o=null;
try
{
o=super.clone();
}
catch(CloneNotSupportedException e)
{
System.out.println(e.toString());
}
return o;
}
}
class Student implements Cloneable
{
String name;
int age;
Professor p;
Student(String name,int age,Professor p)
{
this.name=name;
this.age=age;
this.p=p;
}
public Object clone()
{
//Object o=null;
Student o=null;
try
{
o=(Student)super.clone();
}
catch(CloneNotSupportedException e)
{
System.out.println(e.toString());
}
o.p=(Professor)p.clone();
return o;
}
}
class Point
{
int x,y;
public String toString()
{
return "x="+x+","+"y="+y;
}
}


摘自:孙鑫老师java视频
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值