wmyskxz / morethanjava Goto Github PK
View Code? Open in Web Editor NEW学习, 不止 Code 👨💻
学习, 不止 Code 👨💻
在Day 6:面向对象进阶——多态文中,有一个测试题解析:
拿这里的 aRefB.show(b) 来说好了,aRefB 虽然是 A 类型的引用,但首先会查找 B 对象中的方法 (因为它实际的指向是 B),而引用 b 正好是一个 B 类型 (实质上是 is-a A 类型),所以符合 B 对象中签名为 show(A) 的方法,就输出了 B and A。如果 B 类型中没有符合签名的方法,那么会从父类中查找,继续这个过程直到找到或者报错。
这里 首先会查找 B 对象中的方法
的表述我觉得有点问题,我理解的是首先aRefB
是A类型,拥有的方法是:
public String show(D object) { return "A and D"; }
public String show(A object) { return "A and A"; }
A aRefB = new B();
// 这里的 aRefB 是一个A类型aRefB.show(b)
// 这里先调用的是A的show(A),因为查找出 A 类中 符合 show(B) 的方法只有 show(A)aRefB.show(b)
运行时 实际 调用的是 B 中的 show(A) , 这一步才用到多态, B类中重写了 A 类的 show(A) 方法所以aRefB.show(b)
对应的是 B 的show(A object)
方法
点开的路径不对
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.