instanceof-多态强转

instanceof关键字:

instanceof其实是Java的一个二元操作符,用于判断左边的对象是否是右边这个特定的类,或者它子类的实例,返回boolean类型的数据。

记忆方法:左边一个对象的引用(变量名),右边一个类名。判断左边的类型是不是右边的类。


多态向下强转案例:

有一个Anima类,将Cat和Dog类来继承Animal,Dog类中有自己特定的方法(注意,父类中没有定义此方法)。那么,在父类引用的情况下如何执行?    这时就用到了向下强转,这时就需要将父类引用强制转换成Dog类型,Dog d = (Dog) 父类引用;



下图中包含了多态中的强制向下转型:

Screenshot_2019-09-08-22-52-58-99_eb0d42eae00582acd709e6c27e347eed.png


将Women强制转换成Man类型时报错图:Screenshot_2019-09-08-22-45-24-73_eb0d42eae00582acd709e6c27e347eed.png
下一篇
梦-
发表评论 / Comment

用心评论~

金玉良言 / Appraise
AverysmulpLV 1
2019-09-10 19:30
Good day!  yq1024.cn

Have you ever heard of sending messages via contact forms?

Imagine that your offer will be readseen by hundreds of thousands of your probable customerscustomers.
Your message will not go to the spam folder because people will send the message to themselves. As an example, we have sent you our offer  in the same way.

We have a database of more than 30 million sites to which we can send your letter. Sites are sorted by country. Unfortunately, you can only select a country when sending a message.

The price of one million messages 49 USD.
There is a discount program when you purchase  more than two million message packages.


Free trial mailing of 50,000 messages to any country of your choice.


This letter is created automatically. Please use the contact details below to contact us.



Contact us.
Telegram - @FeedbackFormEU
Skype  FeedbackForm2019
Email - Contact@feedbackmessages.com
MichaelWabLV 1
2019-09-09 21:40
Profit +10% after 2 days
The minimum amount for donation is 0.0025 BTC.
Maximum donation amount is 10 BTC.

Ref bonus 3 levels: 5%,3%,1% paying next day after donation
https://quickchain.cc/