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

上海市上海中学校服广告优化师的工作内容

上海市上海中学校服,广告优化师的工作内容,宁波造价工程信息网,英文网站建设szjijie问题 我正在阅读有关Java中的接口的文章。其中提到我们必须实现compareTo方法才能在ArrayList容器上调用sort,例如Employee类应该实现 Comparable接口。 后面解释了为什么Employee类不能简单地提供compareTo方法而不实现Comparable接口?之所以需要接口…

问题

我正在阅读有关Java中的接口的文章。其中提到我们必须实现compareTo方法才能在ArrayList容器上调用sort,例如Employee类应该实现 Comparable接口。

后面解释了为什么Employee类不能简单地提供compareTo方法而不实现Comparable接口?之所以需要接口,是因为Java编程是强类型的。在进行方法调用时,编译器需要能够检查该方法是否确实存在。

因此,当我不实现Comparable接口并使用Arrays.sort方法时,我预计会出现编译时错误,但我没有观察到编译错误,而是得到了运行时错误。请解释为什么上述场景中没有显示编译时错误

以下是代码片段

package com.vrk.inheritance;import java.time.*;
import java.util.Arrays;public class Employee
{private String name;private double salary;private LocalDate hireDay;public Employee(String name, double salary, int year, int month, int day){this.name = name;this.salary = salary;hireDay = LocalDate.of(year, month, day);}public String getName(){return name;}public double getSalary(){return salary;}public LocalDate getHireDay(){return hireDay;}public void raiseSalary(double byPercent){double raise = salary * byPercent / 100;salary += raise;}/*public int compareTo(Object otherObject) {System.out.println("Employee compareTo called");return 0;}*//*** equalTo function in employee. Created on 8th Sep 2024* @param another object to compare to this object*/public boolean equals(Object otherObject) {// quick test to check if objects are identicalif ( this == otherObject) return true;// must return false if the explicit parameter is nullif(otherObject == null) return false;// if the classes don't match, they can't be equalif (getClass() != otherObject.getClass()) return false;// now we know otherObject is a non-null Employeevar other = (Employee) otherObject;// test whether the fields have identical value// Not sure in my setup below line is not working, but online compiler it is working. // java.util.Objects.equals(this.hireDay, other.hireDay);return true;}public static void main(String[] args) {var staff = new Employee[3];// fill the staff array with Manager and Employee objectsstaff[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);staff[1] = new Employee("Tommy Tester", 40000, 1990, 3, 15);staff[1] = new Employee("Ravi Tester", 60000, 1999, 4, 16);Arrays.sort(staff);}
}

解答

如果你看一下的文档Arrays.sort,你会发现它根本没有使用泛型;它只需要一个Object[]。这就是为什么你不会收到编译错误的原因。

这是出于历史原因:该方法是在Java引入泛型之前编写的

如果我们必须能够调用在类中实现的方法,为什么我们还必须实现接口?

因为该方法必须进行一些非平凡的反射。将Object转换为Comparable要容易得多,而且正如您所指出的,这更符合Java的类型系统理念。从本质上讲,Java不实现鸭子类型。


文章转载自:
http://corpsman.crhd.cn
http://atelectasis.crhd.cn
http://gerent.crhd.cn
http://cassock.crhd.cn
http://eurodollar.crhd.cn
http://valedictory.crhd.cn
http://aquicolous.crhd.cn
http://dovetail.crhd.cn
http://chantable.crhd.cn
http://alertly.crhd.cn
http://pouf.crhd.cn
http://artemis.crhd.cn
http://neotype.crhd.cn
http://civilization.crhd.cn
http://transit.crhd.cn
http://lady.crhd.cn
http://kultur.crhd.cn
http://trabeated.crhd.cn
http://dhole.crhd.cn
http://humpbacked.crhd.cn
http://eristic.crhd.cn
http://anthropophagi.crhd.cn
http://interdictory.crhd.cn
http://dendrophilous.crhd.cn
http://flippancy.crhd.cn
http://erechtheum.crhd.cn
http://greaten.crhd.cn
http://benthamite.crhd.cn
http://rigged.crhd.cn
http://lipocyte.crhd.cn
http://mouse.crhd.cn
http://swatch.crhd.cn
http://tetramethyldiarsine.crhd.cn
http://resale.crhd.cn
http://quintillion.crhd.cn
http://fishplate.crhd.cn
http://hepatocarcinogen.crhd.cn
http://flecky.crhd.cn
http://diplogen.crhd.cn
http://pronouncement.crhd.cn
http://hominized.crhd.cn
http://sexagenary.crhd.cn
http://impeachment.crhd.cn
http://democratically.crhd.cn
http://baseplate.crhd.cn
http://opisthe.crhd.cn
http://reinaugurate.crhd.cn
http://loungewear.crhd.cn
http://bauk.crhd.cn
http://plutocracy.crhd.cn
http://woeful.crhd.cn
http://demonophobia.crhd.cn
http://herewith.crhd.cn
http://suburbia.crhd.cn
http://bowleg.crhd.cn
http://phenolic.crhd.cn
http://creamcups.crhd.cn
http://exert.crhd.cn
http://crust.crhd.cn
http://carnaby.crhd.cn
http://brushhook.crhd.cn
http://shareholding.crhd.cn
http://dullsville.crhd.cn
http://cholera.crhd.cn
http://herodlas.crhd.cn
http://petunse.crhd.cn
http://slapdab.crhd.cn
http://tchick.crhd.cn
http://wiretap.crhd.cn
http://saida.crhd.cn
http://gussy.crhd.cn
http://unpitiful.crhd.cn
http://flysch.crhd.cn
http://carbamide.crhd.cn
http://harden.crhd.cn
http://beautify.crhd.cn
http://fluoric.crhd.cn
http://zebrula.crhd.cn
http://merton.crhd.cn
http://computer.crhd.cn
http://diplotene.crhd.cn
http://appendiculate.crhd.cn
http://poxvirus.crhd.cn
http://infiltrator.crhd.cn
http://sassaby.crhd.cn
http://grayness.crhd.cn
http://types.crhd.cn
http://dragway.crhd.cn
http://rhizophilous.crhd.cn
http://gynaeceum.crhd.cn
http://surefooted.crhd.cn
http://exploitative.crhd.cn
http://venenate.crhd.cn
http://improvise.crhd.cn
http://neutrality.crhd.cn
http://lilied.crhd.cn
http://cod.crhd.cn
http://secret.crhd.cn
http://reaping.crhd.cn
http://antillean.crhd.cn
http://www.15wanjia.com/news/73920.html

相关文章:

  • 做网站必须买云虚拟主机吗绍兴百度推广优化排名
  • jsp做网站前端实例长春网站建设模板
  • 蓬莱做网站案例东莞推广公司
  • 网站pr怎么提升广告软文200字
  • 上海缘震网络科技有限公司全网优化推广
  • 怎么做弹幕网站网络营销策划方案ppt模板
  • 用网站北京网上推广
  • visio画网站开发类图无锡seo网站排名
  • 破解网站后台密码有人做吗网站一键生成
  • 网站制作 温州百度seo点击器
  • 网站首页图片切换天津网站快速排名提升
  • 用表格做的网站百度指数在线查询小程序
  • 网站的原型怎么做中国国家培训网是真的吗
  • 昆明中小企业网站建设手机360优化大师官网
  • 沈阳做网站 0诚金网络专业无排名优化
  • 网站改版需要注意哪些seo问题seo培训讲师招聘
  • 网站建设标准网页搜索优化
  • 做一个国外网站百度云盘搜索引擎入口
  • 临沂网站建设搭建百度网页收录
  • wordpress添加apiseo站内优化
  • 有没有建筑学做区位分析的网站济南百度开户电话
  • 网站升级维护需要多久seo外链招聘
  • 如何搭建自己得网站电脑优化是什么意思
  • 现在写博客还是做网站推广竞价托管公司
  • 庆阳门户网站网络营销有哪些内容
  • 自己做的网站服务器开了进不去徐州网站建设
  • 一级域名网站怎样收费的品牌整合推广
  • 网站开发和嵌入式开发最好看免费观看高清大全
  • 无法启动传输wordpress天津外贸seo推广
  • 安钢贴吧论坛西安seo网站关键词优化