• 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);
            
        }
    
    }
  • 相关阅读:
    sys:1: RuntimeWarning: coroutine 'Launcher.killChrome' was never awaited
    python 引用对象相等,还是用list.extend()
    python 3.7.4 垃圾,一整天安装那个mitmproxy ,不行,卡在一个破库叫urwid ,说是os.path()为空,换3.8.2安装成功,垃圾3.7.4迟早要完
    python @staticmethod 注解,静态方法,可以省略类里那个self参数
    spring boot 记一次花了两天还是没有解决的奇怪bug(失去所有响应,post不到了,啥反应也没有了)
    Yum常用命令
    Centos安装与配置
    遍历hashmap的6种方法
    Java定时调度
    ElasticSearch的应用
  • 原文地址:https://www.cnblogs.com/bekeyuan123/p/6882244.html
Copyright © 2020-2023  润新知