• Java--父类的引用指向子类的对象详解!


    例:

      第一步.创建一个Person类

    package com.maya.ball;
    
    public class Person {
        private int age;
        private String name;
        
        public Person(int age,String name){
            this.age = age;
            this.name = name;
        }
        
        public void speak(){
            
        }
        public int getAge(){
            return age;
        }
        public void setAge(int age){
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

      第二步.创建American类继承Person类。

    package com.maya.basketball;
    
    import com.maya.ball.Person;
    
    public class American extends Person {
        public double height;-----------------------------新增成员变量
        
        public American(int age,String name,double height){
            super(age, name);
            this.height = height;
    
        }
        public void speak(){
            System.out.println("我年龄:"+ this.getAge() +",我叫:"+getName()+",我身高:"+height);
        }
    }

      第三步.创建Chinese类,同意继承Person类

    package com.maya.football;
    
    import com.maya.ball.Person;
    
    public class Chinese extends Person{
    
        private String sex;-------------------新增成员变量
        
        public Chinese(int age, String name,String sex) {
            super(age, name);
            this.sex = sex;
        }
        
        public void speak(){
            System.out.println("我年龄:"+ getAge() +",我叫:"+ this.getName() +",我性别:"+sex);
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    }

      第四步.创建Test1类,通过show方法把Person的对象作为参数传进去

    package com.maya.test;
    
    import com.maya.ball.Person;
    import com.maya.basketball.American;
    import com.maya.football.Chinese;
    
    public class Test1 {
        public void show(Person per){
            if(per instanceof American){
                American am = (American) per;
                System.out.println(am.height);
            }
            if(per instanceof Chinese){
                Chinese ch = (Chinese) per;
                System.out.println(ch.getSex());
            }
        }
    }

      第五步.创建Main方法进行调用

    package com.maya.test;
    
    import com.maya.ball.Person;
    import com.maya.basketball.American;
    import com.maya.football.Chinese;
    
    public class Main1 {
        public static void main(String[] args){
            Person per = new American(32,"小明",175);
            American american = (American) per;
            double s = american.height;
            System.out.println(s);
            
            Person ame = new American(32,"小明",180);
            ame.speak();
            Person chinese = new Chinese(23,"小李","女");
            chinese.speak();
            Test1 t = new Test1();
            t.show(ame);
            t.show(chinese);
            
        }
    
    }
  • 相关阅读:
    python 中的 用chr()数值转化为字符串,字符转化为数值ord(s)函数
    如何使用Python-GnuPG和Python3 实现数据的解密和加密
    “瑞士军刀”Netcat使用方法总结
    pyppeteer模块的基本使用
    js加密数据爬取
    requests 返回 521
    Spring注解配置定时任务<task:annotation-driven/>
    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    tomcat同个端口配置多个项目后无后缀的页面跳转
    java中将jsonObject字符串转化为Map对象
  • 原文地址:https://www.cnblogs.com/bekeyuan123/p/6882244.html
Copyright © 2020-2023  润新知