网站app开发哪家好,企业如何做好网站的seo优化,网站建设的运营计划,如何开网店无货源不需要投资对象的起源 Object 文章来源#xff1a;《Head First Java》修炼感悟。
上一篇文章中#xff0c;老白学习了抽象类和抽象方法#xff0c;不禁感慨#xff0c;原来 Java 还可以这样玩。 同时又有了新的疑问#xff0c;这些父类从何而来的#xff1f; 本篇文章老白来聊一聊…对象的起源 Object 文章来源《Head First Java》修炼感悟。
上一篇文章中老白学习了抽象类和抽象方法不禁感慨原来 Java 还可以这样玩。 同时又有了新的疑问这些父类从何而来的 本篇文章老白来聊一聊「对象的起源」再次捂脸好炫酷的用词。
与生俱来的「超能力」
Java 中的所有类都是由 Object 继承而来即便是你自己创建的类也是如此。 也就是说Object 是所有类的父类。 比如我们设计 Dog 类时看起来没有继承任何类其实还是隐式地继承自 Object #mermaid-svg-YutuhZV3GxYapazn {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-YutuhZV3GxYapazn .error-icon{fill:#552222;}#mermaid-svg-YutuhZV3GxYapazn .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-YutuhZV3GxYapazn .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-YutuhZV3GxYapazn .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-YutuhZV3GxYapazn .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-YutuhZV3GxYapazn .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-YutuhZV3GxYapazn .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-YutuhZV3GxYapazn .marker{fill:#333333;stroke:#333333;}#mermaid-svg-YutuhZV3GxYapazn .marker.cross{stroke:#333333;}#mermaid-svg-YutuhZV3GxYapazn svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-YutuhZV3GxYapazn g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-YutuhZV3GxYapazn g.classGroup text .title{font-weight:bolder;}#mermaid-svg-YutuhZV3GxYapazn .nodeLabel,#mermaid-svg-YutuhZV3GxYapazn .edgeLabel{color:#131300;}#mermaid-svg-YutuhZV3GxYapazn .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-YutuhZV3GxYapazn .label text{fill:#131300;}#mermaid-svg-YutuhZV3GxYapazn .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-YutuhZV3GxYapazn .classTitle{font-weight:bolder;}#mermaid-svg-YutuhZV3GxYapazn .node rect,#mermaid-svg-YutuhZV3GxYapazn .node circle,#mermaid-svg-YutuhZV3GxYapazn .node ellipse,#mermaid-svg-YutuhZV3GxYapazn .node polygon,#mermaid-svg-YutuhZV3GxYapazn .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-YutuhZV3GxYapazn .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-YutuhZV3GxYapazn g.clickable{cursor:pointer;}#mermaid-svg-YutuhZV3GxYapazn g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-YutuhZV3GxYapazn g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-YutuhZV3GxYapazn .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-YutuhZV3GxYapazn .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-YutuhZV3GxYapazn .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-YutuhZV3GxYapazn .dashed-line{stroke-dasharray:3;}#mermaid-svg-YutuhZV3GxYapazn #compositionStart,#mermaid-svg-YutuhZV3GxYapazn .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-YutuhZV3GxYapazn #compositionEnd,#mermaid-svg-YutuhZV3GxYapazn .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-YutuhZV3GxYapazn #dependencyStart,#mermaid-svg-YutuhZV3GxYapazn .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-YutuhZV3GxYapazn #dependencyStart,#mermaid-svg-YutuhZV3GxYapazn .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-YutuhZV3GxYapazn #extensionStart,#mermaid-svg-YutuhZV3GxYapazn .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-YutuhZV3GxYapazn #extensionEnd,#mermaid-svg-YutuhZV3GxYapazn .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-YutuhZV3GxYapazn #aggregationStart,#mermaid-svg-YutuhZV3GxYapazn .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-YutuhZV3GxYapazn #aggregationEnd,#mermaid-svg-YutuhZV3GxYapazn .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-YutuhZV3GxYapazn .edgeTerminals{font-size:11px;}#mermaid-svg-YutuhZV3GxYapazn :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 隐式继承 Object Dog 虽然后来 Dog 继承了 Canine、Animal但最终还是会继承自 Object #mermaid-svg-w1QcqrSxvYGWs47C {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-w1QcqrSxvYGWs47C .error-icon{fill:#552222;}#mermaid-svg-w1QcqrSxvYGWs47C .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-w1QcqrSxvYGWs47C .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-w1QcqrSxvYGWs47C .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-w1QcqrSxvYGWs47C .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-w1QcqrSxvYGWs47C .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-w1QcqrSxvYGWs47C .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-w1QcqrSxvYGWs47C .marker{fill:#333333;stroke:#333333;}#mermaid-svg-w1QcqrSxvYGWs47C .marker.cross{stroke:#333333;}#mermaid-svg-w1QcqrSxvYGWs47C svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-w1QcqrSxvYGWs47C g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-w1QcqrSxvYGWs47C g.classGroup text .title{font-weight:bolder;}#mermaid-svg-w1QcqrSxvYGWs47C .nodeLabel,#mermaid-svg-w1QcqrSxvYGWs47C .edgeLabel{color:#131300;}#mermaid-svg-w1QcqrSxvYGWs47C .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-w1QcqrSxvYGWs47C .label text{fill:#131300;}#mermaid-svg-w1QcqrSxvYGWs47C .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-w1QcqrSxvYGWs47C .classTitle{font-weight:bolder;}#mermaid-svg-w1QcqrSxvYGWs47C .node rect,#mermaid-svg-w1QcqrSxvYGWs47C .node circle,#mermaid-svg-w1QcqrSxvYGWs47C .node ellipse,#mermaid-svg-w1QcqrSxvYGWs47C .node polygon,#mermaid-svg-w1QcqrSxvYGWs47C .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-w1QcqrSxvYGWs47C .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-w1QcqrSxvYGWs47C g.clickable{cursor:pointer;}#mermaid-svg-w1QcqrSxvYGWs47C g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-w1QcqrSxvYGWs47C g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-w1QcqrSxvYGWs47C .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-w1QcqrSxvYGWs47C .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-w1QcqrSxvYGWs47C .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-w1QcqrSxvYGWs47C .dashed-line{stroke-dasharray:3;}#mermaid-svg-w1QcqrSxvYGWs47C #compositionStart,#mermaid-svg-w1QcqrSxvYGWs47C .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w1QcqrSxvYGWs47C #compositionEnd,#mermaid-svg-w1QcqrSxvYGWs47C .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w1QcqrSxvYGWs47C #dependencyStart,#mermaid-svg-w1QcqrSxvYGWs47C .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w1QcqrSxvYGWs47C #dependencyStart,#mermaid-svg-w1QcqrSxvYGWs47C .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w1QcqrSxvYGWs47C #extensionStart,#mermaid-svg-w1QcqrSxvYGWs47C .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w1QcqrSxvYGWs47C #extensionEnd,#mermaid-svg-w1QcqrSxvYGWs47C .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w1QcqrSxvYGWs47C #aggregationStart,#mermaid-svg-w1QcqrSxvYGWs47C .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w1QcqrSxvYGWs47C #aggregationEnd,#mermaid-svg-w1QcqrSxvYGWs47C .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w1QcqrSxvYGWs47C .edgeTerminals{font-size:11px;}#mermaid-svg-w1QcqrSxvYGWs47C :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 隐式继承 Object Animal Canine Dog 既然 Object 是所有类最原始的父类那所有类肯定会继承到 Object 的某些行为而且这些行为应该是最通用的、能适用于所有对象的。 很好奇 Object 到底都提供了哪些行为呢
设想一下假如自己设计 Object 类的话会抽取哪些共性的元素呢
能与其它对象进行对比的方法能生成自己唯一标识的方法能描述自己的方法等等…
好消息是Object 真的实现了类似的方法而且还远不止这些 #mermaid-svg-ccEhCOx1AMTqA4sw {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ccEhCOx1AMTqA4sw .error-icon{fill:#552222;}#mermaid-svg-ccEhCOx1AMTqA4sw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ccEhCOx1AMTqA4sw .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-ccEhCOx1AMTqA4sw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ccEhCOx1AMTqA4sw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ccEhCOx1AMTqA4sw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ccEhCOx1AMTqA4sw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ccEhCOx1AMTqA4sw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ccEhCOx1AMTqA4sw .marker.cross{stroke:#333333;}#mermaid-svg-ccEhCOx1AMTqA4sw svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ccEhCOx1AMTqA4sw g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-ccEhCOx1AMTqA4sw g.classGroup text .title{font-weight:bolder;}#mermaid-svg-ccEhCOx1AMTqA4sw .nodeLabel,#mermaid-svg-ccEhCOx1AMTqA4sw .edgeLabel{color:#131300;}#mermaid-svg-ccEhCOx1AMTqA4sw .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-ccEhCOx1AMTqA4sw .label text{fill:#131300;}#mermaid-svg-ccEhCOx1AMTqA4sw .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-ccEhCOx1AMTqA4sw .classTitle{font-weight:bolder;}#mermaid-svg-ccEhCOx1AMTqA4sw .node rect,#mermaid-svg-ccEhCOx1AMTqA4sw .node circle,#mermaid-svg-ccEhCOx1AMTqA4sw .node ellipse,#mermaid-svg-ccEhCOx1AMTqA4sw .node polygon,#mermaid-svg-ccEhCOx1AMTqA4sw .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ccEhCOx1AMTqA4sw .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-ccEhCOx1AMTqA4sw g.clickable{cursor:pointer;}#mermaid-svg-ccEhCOx1AMTqA4sw g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-ccEhCOx1AMTqA4sw g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-ccEhCOx1AMTqA4sw .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-ccEhCOx1AMTqA4sw .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-ccEhCOx1AMTqA4sw .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-ccEhCOx1AMTqA4sw .dashed-line{stroke-dasharray:3;}#mermaid-svg-ccEhCOx1AMTqA4sw #compositionStart,#mermaid-svg-ccEhCOx1AMTqA4sw .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ccEhCOx1AMTqA4sw #compositionEnd,#mermaid-svg-ccEhCOx1AMTqA4sw .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ccEhCOx1AMTqA4sw #dependencyStart,#mermaid-svg-ccEhCOx1AMTqA4sw .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ccEhCOx1AMTqA4sw #dependencyStart,#mermaid-svg-ccEhCOx1AMTqA4sw .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ccEhCOx1AMTqA4sw #extensionStart,#mermaid-svg-ccEhCOx1AMTqA4sw .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ccEhCOx1AMTqA4sw #extensionEnd,#mermaid-svg-ccEhCOx1AMTqA4sw .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ccEhCOx1AMTqA4sw #aggregationStart,#mermaid-svg-ccEhCOx1AMTqA4sw .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ccEhCOx1AMTqA4sw #aggregationEnd,#mermaid-svg-ccEhCOx1AMTqA4sw .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ccEhCOx1AMTqA4sw .edgeTerminals{font-size:11px;}#mermaid-svg-ccEhCOx1AMTqA4sw :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 隐式继承 Object Class getClass() int hashCode() String toString() equals() YourClass 以上仅是列出了 Object 的部分方法并且这些方法都已经实现了意味着你所创建的每一个类都可以直接使用它们比如
public class ObjectTest {public static void main(String[] args) {Dog dogA new Dog();Dog dogB new Dog(); // testSystem.out.println(dogA.equals(dogB));System.out.println(dogA.hashCode());System.out.println(dogA.getClass());System.out.println(dogA.toString());}
}
class Dog {}编译执行结果 简直不可思议Dog 刚被创建出来就拥有了「超能力」关键是它什么也没做。
Object 并不适合多态引用
Object 作为最顶端的父类那是不是可以说允许用任何子类引用它 试想一下如果把 ArrayList 的元素类型声明为 Object那岂不是可以保存任何类型了想法一点也没错请看
ArrayListObject dogList new ArrayListObject();
Dog dogA new Dog();
dogList.add(dogA);以上代码没有问题确实可以将 Dog 对象保存到列表中。 但是如果从列表中取出对象时会出现问题
Dog dogB dogList.get(0);可以看到无论你放入的是什么类型取出时总是被当作 Object 类型。 编译器无法将取出的对象识别为 Object 以外的其它类型。
方法返回 Object 类型时也会出现同样问题
Dog dogA new Dog();
Dog dog getObject(dogA);
public Object getObject(Object o) {return o;
}虽然确实传入了 Dog 对象但 getObject() 返回的只能是 Object 类型。 如果还是不死心强制执行 Dog 应有的方法比如
getObject(dogA).bark();编译器还是会拒绝因为编译器不认为 Object 有 bark() 这个方法它只认得 Object 应有的那些方法。 《 上一篇 抽象类和抽象方法下一篇 抽象的极致是「接口」》