Learn polymorphism with teacher Wang (iii) conversion of parent class to subclass and instance of operator

Source: Internet
Author: User

Parent class tothe transformation of subclasses andinstanceofoperatorKeynote Teacher: Wang ShaohuaQQGroup number:483773664Learning Goals:

s) Parent and subclass conversions: down conversion

p) Mastering the use of the instanceof operator

First, Problem: Play function for host and pet (i) Demand:

play the game with the dog , the dog's health value is reduced by 5 , with the owner's intimacy increased

playing swimming games with penguins, the Penguin's health value decreased by 5 , with the owner of the increase in intimacy

(ii) Implementation ideas

s) Adding catchingflydisc method to Dog class to realize the function of frisbee;

2) Adding swimming method to Penguin class to realize swimming function;

3) Add the play (Pet Pet) method to the host, if pet is playing a Frisbee game on behalf of Dog , if pet represents Penguin is playing swimming games.

R) Create a test class that creates a master, dog, and Penguin object and invokes the appropriate method to implement the host and pet play function.

(iii) Specific implementation

Follow the steps below to step through the task.

1. Dog

12345678 /**     * 实现接飞盘方法     */    public void catchingFlyDisc() {        System.out.println("狗狗" + this.getName() + "正在接飞盘。");        this.setHealth(this.getHealth()-10);        this.setLove(this.getLove()+5);    }
2. Penguin
12345678 /**    * 实现游泳方法    */   public void swimming() {       System.out.println("企鹅" + this.getName() + "正在游泳。");       this.setHealth(this.getHealth()-10);       this.setLove(this.getLove()+5);   }
3. Master
1234 publicvoidPlay(Pet pet){ //1 判断Pet是什么对象 //2 将Pet转化为具体对象,因为父对象没有办法调用子对象的特有的方法 }
Second, Parent class toConversion of subclasses (downward transition)

Assigns a reference to a subclass object to a parent class reference , called a downward transformation, which must be cast at this time.

(i) Demand

What if you assign a dog object to a Pet type reference variable and want to play a Frisbee game with dog ?

(ii) Realize
12345678 publicclass Test {    public static void main(String[] args) {        Pet pet = newDog(

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.