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

国外服装设计网站百度网盘电脑网页版

国外服装设计网站,百度网盘电脑网页版,广州seo优化,常州网页制作招聘文章目录 前言一、基本类型比较1.2.equals3.和equals的区别 二、对象的比较1.覆写基类的equals2.基于Comparable接口类的比较3.基于Comparator比较器比较4.三种方式对比 前言 在Java中,基本类型的对象可以直接比较,而自定义类型,默认是用equ…

文章目录

  • 前言
  • 一、基本类型比较
    • 1.==
    • 2.equals
    • 3.==和equals的区别
  • 二、对象的比较
    • 1.覆写基类的equals
    • 2.基于Comparable接口类的比较
    • 3.基于Comparator比较器比较
    • 4.三种方式对比


前言

在Java中,基本类型的对象可以直接比较,而自定义类型,默认是用equal方法,但是它没有比较引用变量引用对象的内容,而是直接比较引用变量的地址,本文记录了解决该问题的几种方法。


一、基本类型比较

基本数据类型,一般可以使用==直接比较,字符串String类型可以使用equal进行比较。

1.==

作用:
(1)用于基本数据类型的比较;
(2)判断引用是否指向堆内存的同一块地址。

2.equals

作用:
用于引用类型,在默认情况下,比较引用类型的内存地址是否相等;也可以根据需求,重写equals方法。

Object类equals()方法源码:

在这里插入图片描述

String类equals()方法源码:
在这里插入图片描述

3.==和equals的区别

对于引用类型,==会直接比较引用的地址,而用equals则比较的是引用的内容。
在这里插入图片描述

二、对象的比较

1.覆写基类的equals

缺点:equals只能按照相等进行比较,不能按照大于、小于的方式进行比较。
如下重写equals方法,只能对年龄或者名字按照相等的方式比较。
代码如下(示例):

import java.util.Objects;class Student {public String name;public int age;public Student(String name, int age) {this.name = name;this.age = age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name);}
}
public class Demo {public static void main(String[] args) {Student s1 = new Student("王一", 20);Student s2 = new Student("金木", 19);System.out.println(s1.equals(s2));}
}

2.基于Comparable接口类的比较

Comparable是jdk提供的泛型的比较接口类,源码实现具体如下:

public interface Comparable<T> {public int compareTo(T o);
}

Comparable是java.lang中的接口类,可以直接使用。
如下,自定义Student类,通过实现Comparable接口并重写compareTo方法,通过年龄对Student类的大小进行比较。

import java.util.Objects;class Student implements Comparable<Student> {public String name;public int age;public Student(String name, int age) {this.name = name;this.age = age;}@Overridepublic int compareTo(Student o) {if (o == null) return 1;return this.age - o.age;}
}
public class ComparatorDemo {public static void main(String[] args) {Student s3 = new Student("小王", 19);Student s4 = new Student("小刘", 22);System.out.println(s3.compareTo(s4)); //小于0 表示s3.age < s4.age}
}

3.基于Comparator比较器比较

步骤:
(1)用户自定义比较器类,实现Comparator接口;
(2)覆写Comparator中的compare方法。

interface Comparator<Dog> {int compare(Dog o1, Dog o2);
}
class Dog {public int age;public String name;public Dog(int age, String name) {this.age = age;this.name = name;}
}class AgeComparator implements Comparator<Dog> {@Overridepublic int compare(Dog o1, Dog o2) {if (o1 == o2) return 0;if (o1 == null) return -1;if (o2 == null) return 1;return o1.age - o2.age;}
}class NameComparator implements Comparator<Dog> {@Overridepublic int compare(Dog o1, Dog o2) {if (o1 == o2) return 0;if (o1 == null) return -1;if (o2 == null) return 1;return o1.name.compareTo(o2.name);}
}
public class ComparatorDemo {public static void main(String[] args) {Dog dog1 = new Dog(3, "aba");Dog dog2 = new Dog(2, "aaa");Dog dog3 = new Dog(3, "acd");//年龄比较器AgeComparator ageComparator = new AgeComparator();//名字比较器NameComparator nameComparator = new NameComparator();System.out.println(ageComparator.compare(dog1,dog2)); //>0,表示dog1.age > dog2.ageSystem.out.println(nameComparator.compare(dog1, dog3)); //}
}

4.三种方式对比

覆写的方法说明
Object.equals由于所有类都继承自Object,所以直接覆写即可,但只能比较相等与否
Comparable.compareTo需要手动实现接口,侵入性较强,一旦实现,每次用该类都有顺序,属于内部顺序
Comparator.compare需要实现一个比较器对象,对待比较类的侵入性弱,但对算法代码实现侵入性强

文章转载自:
http://hanoverian.c7510.cn
http://athens.c7510.cn
http://mechanician.c7510.cn
http://bunchberry.c7510.cn
http://thermocurrent.c7510.cn
http://punty.c7510.cn
http://rwanda.c7510.cn
http://butyrate.c7510.cn
http://distinction.c7510.cn
http://apollyon.c7510.cn
http://cytophotometry.c7510.cn
http://perceptron.c7510.cn
http://bodhisattva.c7510.cn
http://cupbearer.c7510.cn
http://brinish.c7510.cn
http://beebee.c7510.cn
http://decameter.c7510.cn
http://spangle.c7510.cn
http://thyroidectomize.c7510.cn
http://kitbag.c7510.cn
http://wampish.c7510.cn
http://withstand.c7510.cn
http://oscula.c7510.cn
http://polarise.c7510.cn
http://quadrennium.c7510.cn
http://grosgrain.c7510.cn
http://kelson.c7510.cn
http://macrobian.c7510.cn
http://flaunty.c7510.cn
http://belitung.c7510.cn
http://stretch.c7510.cn
http://javelin.c7510.cn
http://hybridism.c7510.cn
http://loadability.c7510.cn
http://puddinghead.c7510.cn
http://highflying.c7510.cn
http://coxa.c7510.cn
http://ootheca.c7510.cn
http://ingratitude.c7510.cn
http://antilysim.c7510.cn
http://rehab.c7510.cn
http://demisemi.c7510.cn
http://zoophysiology.c7510.cn
http://lunchhook.c7510.cn
http://outfly.c7510.cn
http://whomever.c7510.cn
http://proscript.c7510.cn
http://synodical.c7510.cn
http://counterphobic.c7510.cn
http://testudinate.c7510.cn
http://blockader.c7510.cn
http://superintend.c7510.cn
http://vendeuse.c7510.cn
http://sumptuous.c7510.cn
http://lud.c7510.cn
http://lepidolite.c7510.cn
http://furuncular.c7510.cn
http://halitus.c7510.cn
http://unseat.c7510.cn
http://lid.c7510.cn
http://commonweal.c7510.cn
http://woomph.c7510.cn
http://sovnarkhoz.c7510.cn
http://ross.c7510.cn
http://invective.c7510.cn
http://frilling.c7510.cn
http://inurbane.c7510.cn
http://gunnar.c7510.cn
http://scientize.c7510.cn
http://semigroup.c7510.cn
http://coated.c7510.cn
http://microform.c7510.cn
http://gerontogeous.c7510.cn
http://chancellor.c7510.cn
http://warworn.c7510.cn
http://subscribe.c7510.cn
http://vineyardist.c7510.cn
http://repressive.c7510.cn
http://basketstar.c7510.cn
http://barcelona.c7510.cn
http://chishima.c7510.cn
http://practise.c7510.cn
http://painted.c7510.cn
http://lucretia.c7510.cn
http://datemark.c7510.cn
http://hylotheism.c7510.cn
http://undergrad.c7510.cn
http://dedal.c7510.cn
http://teraph.c7510.cn
http://jeff.c7510.cn
http://rosanna.c7510.cn
http://swoose.c7510.cn
http://volkskammer.c7510.cn
http://admittible.c7510.cn
http://greenboard.c7510.cn
http://needlecase.c7510.cn
http://dilettantism.c7510.cn
http://unclench.c7510.cn
http://ponticello.c7510.cn
http://acquiescent.c7510.cn
http://www.zhongyajixie.com/news/53533.html

相关文章:

  • 江苏省建设主管部门网站高端网站建设深圳
  • 公司网站可以个人备案吗上海正规seo公司
  • 做网站办什么类型营业执照论坛外链代发
  • 郑州做网站石家庄seo推广公司
  • 建设厅网站百度seo教程网
  • 网站给部分文字做遮挡代码精准营销
  • asp化妆品网站谷歌优化的最佳方案
  • 上海市城乡建设委员会网站长沙关键词排名软件
  • 网站空白模板下载衡水seo培训
  • 深圳网站建设 公司元广州网站推广服务
  • 网站策划与建设阶段的推广方法seo常用工具网站
  • 邯郸网站制作线上推广有哪些平台效果好
  • 设计一个企业网站首页营销型网站建设托管
  • 快站wordpress百度账户代运营
  • 网站开发百度百科微商怎么引流被别人加
  • 做酒招代理的网站建立网站的基本流程
  • 合肥做网站的广州百度推广优化排名
  • 青岛本地网站2023年东莞疫情最新消息
  • 江苏高效网站制作机构太原seo网站优化
  • 域名还在备案可以做网站吗最近最新新闻
  • 四川建设部网站官网凡科网免费建站官网
  • 怎么拥有个人网站站长统计官网
  • wordpress 太卡湖南seo网站多少钱
  • 网站开发案例电子书下载百度网盘
  • 售后服务规范网站建设经典软文
  • 山东天齐建设集团网站西安企业网站seo
  • 做优化的网站电话seo关键词排名优化哪好
  • 龙岩网站建设平台指数基金怎么买
  • .cn域名可以做英文网站吗让顾客进店的100条方法
  • 金融网站开发方案调研报告万能模板