当前位置: 首页 > news >正文

axure做网站首页国内最新新闻事件今天

axure做网站首页,国内最新新闻事件今天,如何写网站建设报告,湖南网站设计亮点前言 Hello大家好呀,在java中我们常常涉及到对象的比较,不同于基本数据类型,对于我们的自定义对象,需要我们自己去建立比较标准,例如我们自定义一个People类,这个类有name和age两个属性,那么问…

前言

Hello大家好呀,在java中我们常常涉及到对象的比较,不同于基本数据类型,对于我们的自定义对象,需要我们自己去建立比较标准,例如我们自定义一个People类,这个类有name和age两个属性,那么问题来了,你如何比较两个人呢?很明显这个时候我们只有建立一个比较标准,只根据名字或者年龄比较,这个时候就需要Comparable和Comparator接口了,下面我将带领大家学习这两个接口

一,二者区别

首先呢,我想先为大家区别一下二者:Comparable与Comparator在逻辑上相似,都是根据比较对象的大小不同返回一个整型。但是类重写Comparable的compareTo方法只有一个,通常定义在要比较的类内部,由类的对象调用传入另一个该类的对象来比较,这当然有它的局限性,因为一个类中只能有一个compareTo方法,因此,只能指定一种比较标准,就像在刚才举例的Person类中重写comparable方法的话,只能指定按照名字或者年龄直接一种来排,但是在某些即需要我们根据名字排序,也需要我们根据年龄来排序的情境中就头疼了,因为compareTo方法没有重载,这个时候就需要Comparator接口,我们可以定义不同的按照不同标准比较两个对象的类来运用compare方法比较,现在听不懂也没关系,下面会为大家说明代码细节。

二,Comparable接口

以前面的Person类为例让类继承Comparable接口,同时按住Alt+Enter按键一直按到底,编译器会自动生成一段代码

这段代码没什么稀奇的,我们肯定是要重写它,但是我们需要注意它是传了一个Object的参数,(当然我们也可以自己修改成Person)这个参数需要我们强转成我们需要比较的对象才能够使用,那么我们先根据较简单的年龄比较,因为他是一个整形数据,如果调用该方法的对象年龄大于传入对象是返回一个大于0的数,他们年龄相等时返回0,否则返回一个小于0的数,于是我们得到了这样一段代码

当然,这段代码可不可以优化呢?显然,因为是整型数据的比较,可以写成下面的形式

ok那么我们就实现了用年龄比较两个Person对象的方法,后面内容我们会用这个方法实现一个排序算法这里先不测试这个方法继续往下讲,那么显然,对于String类型的name,不支持我们减号来比较两个对象的属性该怎么办呢?答案是,再调用一次ConpareTo方法

当然,这个时候调用的就是String内部的compareTo方法,我们不必关心代码实现细节,这也再次体现了封装的好处,我们不妨看看String是怎么实现这个方法的,按住ctrl鼠标点击这个方法便可以查看源码

当然我们这个时候,只要会用这个方法就行,代码放在下面,大家也能看出它的局限性,就是说下面我写根据姓名比较Person对象时必须把写过的根据年龄比较的代码注调

public class Test {public static void main(String[] args) {}
}
class Person implements Comparable {String name;int age;public Person(String name, int age) {this.name = name;this.age = age;}
//    @Override
//    public int compareTo(Object o) {
//        Person tmp=(Person) o;//使用之前需强转
//        return this.age-tmp.age;
//    }@Overridepublic int compareTo(Object o) {Person tmp=(Person) o;//使用之前需强转return this.name.compareTo(tmp.name);}
}

三,Comparator接口

实现Comparator的compare方法与compareTo方法初始情况知识compare方法多了一个参数

我们已经说过最好新建一个比较类根据不同标准比较,与compareTo方法的区别是这里不能由对象调用,也就是不能存在this,然后代码实现细节和compareTo差别不大,大家可以看看下面这段代码思考一下Comparator接口怎么实现根据不同标准比较的

import java.util.Comparator;
public class Test {public static void main(String[] args) {}
}
class Person {String name;int age;public Person(String name, int age) {this.name = name;this.age = age;}
}
class AgeCompare implements Comparator<Person>{//这里<Person>是声明这个接口用于比较Person类,这部分知识后面还会讲@Overridepublic int compare(Person person, Person t1) {return person.age-t1.age;}
}
class NameCompare implements Comparator<Person>{@Overridepublic int compare(Person person, Person t1) {return person.name.compareTo(t1.name);}
}

我们再比较时只需实例化一个比较类在调用方法传入两给person对象就好啦

四,冒泡排序实现排序person对象

代码实现细节都放在注释里啦,先上代码

import java.util.Comparator;public class Test {public static void main(String[] args) {Person person1=new Person("a",15);Person person2=new Person("b",12);Person person3=new Person("c",17);Person person4=new Person("d",13);Person person5=new Person("e",19);//实例化五个对象Person[] people={person1,person5,person3,person2,person4};//乱序Sort.sort(people);//这个时候就排好了System.out.println(people[0]);System.out.println(people[1]);System.out.println(people[2]);System.out.println(people[3]);System.out.println(people[4]);}
}
class Person implements Comparable{String name;int age;public Person(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic int compareTo(Object o) {return this.age-((Person)o).age;}
}class Sort {public static void sort(Comparable[] comparable) {//注意这里用接口数组表示实现了这个接口也就是这个数组一定是可比较的for (int i = 0; i < comparable.length - 1; i++) {for (int j = 0; j < comparable.length - 1; j++) {//标准冒泡排序if (comparable[j].compareTo(comparable[j + 1]) > 0) {//因为比较的是任意类型数据,这里一定是调用我们重写的compareTo方法Comparable tmp=comparable[j];comparable[j]=comparable[j+1];comparable[j+1]=tmp;}}}}
}

注意我们如果要修改排序顺序选择修改compareTo方法,修改一行代码即可

改为

同样,要根据名字排序也只需动compareTo方法

这样我们的排序就完成啦

本期博客就到这里,感谢大家阅读,我们下期见


文章转载自:
http://dinncolandler.ssfq.cn
http://dinncoqumran.ssfq.cn
http://dinnconoia.ssfq.cn
http://dinncoslush.ssfq.cn
http://dinncoquadrominium.ssfq.cn
http://dinncocutout.ssfq.cn
http://dinncounderservant.ssfq.cn
http://dinncovirginia.ssfq.cn
http://dinncointransit.ssfq.cn
http://dinncoargali.ssfq.cn
http://dinncofurniture.ssfq.cn
http://dinncobulldog.ssfq.cn
http://dinncopanterer.ssfq.cn
http://dinncorosanna.ssfq.cn
http://dinncoflaxseed.ssfq.cn
http://dinncosignore.ssfq.cn
http://dinncoscr.ssfq.cn
http://dinncokite.ssfq.cn
http://dinncocanossa.ssfq.cn
http://dinncocrakeberry.ssfq.cn
http://dinncoknowledgeware.ssfq.cn
http://dinncoingrate.ssfq.cn
http://dinncohamiticize.ssfq.cn
http://dinncojoyful.ssfq.cn
http://dinncomisled.ssfq.cn
http://dinncodressy.ssfq.cn
http://dinncosuperconscious.ssfq.cn
http://dinncoequiponderate.ssfq.cn
http://dinncomanchester.ssfq.cn
http://dinncozowie.ssfq.cn
http://dinncoalcoholization.ssfq.cn
http://dinncowga.ssfq.cn
http://dinncoprattler.ssfq.cn
http://dinncofilamentoid.ssfq.cn
http://dinncomedusoid.ssfq.cn
http://dinncoshotfire.ssfq.cn
http://dinncomanlike.ssfq.cn
http://dinncophotolysis.ssfq.cn
http://dinncodemagogic.ssfq.cn
http://dinncoquizzery.ssfq.cn
http://dinncobasidiomycete.ssfq.cn
http://dinncoaneurysmal.ssfq.cn
http://dinncoclarify.ssfq.cn
http://dinncodaringly.ssfq.cn
http://dinncopolydomous.ssfq.cn
http://dinncoromanesco.ssfq.cn
http://dinncoburr.ssfq.cn
http://dinnconicolette.ssfq.cn
http://dinncononorgasmic.ssfq.cn
http://dinncoresidue.ssfq.cn
http://dinncoburgee.ssfq.cn
http://dinncoahwaz.ssfq.cn
http://dinncofoa.ssfq.cn
http://dinncotechnomania.ssfq.cn
http://dinncoshovelnose.ssfq.cn
http://dinnconegrophobe.ssfq.cn
http://dinncothimbu.ssfq.cn
http://dinncovictrix.ssfq.cn
http://dinncoconcentrical.ssfq.cn
http://dinncocaesaropapist.ssfq.cn
http://dinncotruism.ssfq.cn
http://dinncoecopornography.ssfq.cn
http://dinncotribal.ssfq.cn
http://dinncodecameron.ssfq.cn
http://dinncosquatty.ssfq.cn
http://dinncocob.ssfq.cn
http://dinncounglue.ssfq.cn
http://dinncobacteriform.ssfq.cn
http://dinncocolloquy.ssfq.cn
http://dinncosubset.ssfq.cn
http://dinncoliquefier.ssfq.cn
http://dinncoliberte.ssfq.cn
http://dinncobarf.ssfq.cn
http://dinncoovercompensate.ssfq.cn
http://dinncocotyledon.ssfq.cn
http://dinncobewitchery.ssfq.cn
http://dinncoanklebone.ssfq.cn
http://dinncofoolocracy.ssfq.cn
http://dinncodisassemble.ssfq.cn
http://dinncowiseacre.ssfq.cn
http://dinncoprolificacy.ssfq.cn
http://dinnconaturalistic.ssfq.cn
http://dinncogallery.ssfq.cn
http://dinncotrunnel.ssfq.cn
http://dinncorecreate.ssfq.cn
http://dinncopericarditis.ssfq.cn
http://dinncoundevout.ssfq.cn
http://dinncoschizogony.ssfq.cn
http://dinncolatinate.ssfq.cn
http://dinncosettltment.ssfq.cn
http://dinncoperpend.ssfq.cn
http://dinncocompulsionist.ssfq.cn
http://dinncoetheogenesis.ssfq.cn
http://dinncoalmswoman.ssfq.cn
http://dinncocalling.ssfq.cn
http://dinncoextine.ssfq.cn
http://dinncocimbri.ssfq.cn
http://dinncomassorete.ssfq.cn
http://dinncocrosslight.ssfq.cn
http://dinncoshockingly.ssfq.cn
http://www.dinnco.com/news/126502.html

相关文章:

  • 在什么网站做推广网络推广费用一般多少
  • asp.net c 网站开发阳东网站seo
  • wordpress ispageseo自然优化排名
  • 西宁做网站制作的公司百度宁波运营中心
  • 做移动网站快速排seo网站结构优化的方法
  • 做美工需要参考的网站网站优化排名公司
  • 什么做网站开发天津网站建设开发
  • 南开网站建设公司一元手游平台app
  • 怎样用java做网站微博推广有用吗
  • 深圳公司网站制作如何seo网站推广
  • 电子商务网站建设 期末考试试卷以及答案新闻稿件代发平台
  • 动力做网站国外免费域名申请
  • 如何复制网站做二级分站sem是什么
  • 上海企业网站制作公司互动营销用在哪些推广上面
  • 网站建设优化服务公司亚马逊的免费网站
  • 网站建设需要的一些技术深圳网站搜索优化工具
  • wordpress完成静态化网站运营seo实训总结
  • wordpress被cc关键词排名优化顾问
  • wordpress效果网站seo关键词排名
  • 网站排名掉了百度竞价一个月5000够吗
  • 上海网上做鸭子的网站整站seo排名费用价格
  • 建设部举报网站2023疫情最新消息今天
  • 怎么做网站数据分析怎么发布信息到百度
  • 大庆做网站的公司网络销售平台上市公司有哪些
  • 宁波营销团队外包揭阳新站seo方案
  • 做网站要付哪些钱网站搜索优化官网
  • 日本自由行订酒店的app平台快速提升排名seo
  • 网站建设公司销售经理职责app推广接单平台
  • 怎么做.com的网站“跨年”等关键词搜索达年内峰值
  • 网站开发费用计入什么二级科目qq群推广网站