首页 > 编程知识 正文

implements java,java中serializable接口的作用

时间:2023-05-04 17:08:23 阅读:153729 作者:3668

Comparable示例:对集合ArrayListJob jobs的提交时间进行排序。 提交时间为属性:提交。

首先,对Job类实现Comparable接口: publicclassjobimplementscomparable

实现比较到方法。 返回值为1,- 1,0。 分别切换大、小1、-1的位置是切换大小排序方式

ps:1,-1,0的模式比较比1,- 1更准确,因为无法进行小数比较。

@Override

公共公司到(object arg0) {

//todo自动生成方法stub

floatsubmit=(job ) arg0).submit;

if(this.submitsubmit ) {

返回1;

}elseif((this.submitsubmit ) )。

返回- 1;

}else {

返回0;

}

}

在后主函数内使用: collectios.sort(jobs ); 即,Collectios是List、Set、Map三种集合的父接口。

Colne Java对象的克隆是通过Cloneable接口实现的。 分为浅克隆和深克隆。 其区别在于浅克隆特性,深克隆特性是对象的特性;

实质上深克隆是多次浅克隆(实例化对象a具有对象属性b,b具有name属性)。 深克隆a是浅克隆a的非对象属性,其中浅克隆b的属性。 以下代码:

核心是Student类的(深度)

公共对象clone () throwsclonenotsupportedexception {

students=(student ) super.clone ); //克隆非对象属性

s.t=(Teacher ) t.clone ); //克隆了对象的属性

返回s;

}

}

浅克隆:

公共对象clone () throwsclonenotsupportedexception {

return super.clone (; //仅克隆非对象属性

}

Student类: package clone; publicclassstudentimplementscloneable { public string str='尝试深度克隆'; 公共字符串名称; 公共输入否; 私有技术t; 公共技术获取() {return t; }publicvoidsett(Teachert ) {this.t=t; }publicstudent(stringname,int no ) ) {super; this.name=name; this.no=no; }public String getStr () {return str; }public String getName () {return name; }publicvoidsetname(stringname ) {this.name=name; }public int getNo () {return no; }publicvoidsetno(intno ) ) {this.no=no; } @ overridepublicstringtostring ((return ' student ) str='str ',name=' name ',no=' no ',t=' t.getName ) } publicobjectclone (throwsclonenotsupportedexception ) students=(student ) super.clone ); s.t=(Teacher ) t.clone ); 返回s; }

Teacher类:

package clone; publicclassteacherimplementscloneable { privatestring name; 公共字符串获取名称() {return name; }publicvoidsetname(stringname ) {this.name=name; } publicobjectclone (throwsclonenotsupportedexception ) returnsuper.clone ); }

实例类:

package clone; public class doit (publicstaticvoidmain (string [ ] args ) throwsclonenotsupportedexception ) studentS1=newstudent ) Lisi te t.setName ()悲伤的蜻蜓); system.out.println(t.getname ); S1.SETT(t; sudentS2=(student ) s1.clone ); system.out.println(before: ); system.out.println(S1 ) S1.tostring ) ); system.out.println(S2 ) S2.tostring ) ); teacher t2=s2.getT (; t2.setName ('美味薯片) ); S2.SETT(T2; S2.SETSTR(S2的信息变更); S2.setno(6; system.out.println(after: ); system.out.println(S1 ) S1.tostring ) ); system.out.println(S2 ) S2.tostring ) ); }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。