博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多态性的使用
阅读量:5225 次
发布时间:2019-06-14

本文共 3296 字,大约阅读时间需要 10 分钟。

public class Department {    public String name;    private int number;    public Department() {        super();    }    public Department(String name, int number) {        super();        this.name = name;        this.number = number;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getNumber() {        return number;    }    public void setNumber(int number) {        this.number = number;    }    public void business(){        System.out.println("为集团引进IT设备");    }    public int aveSalary(int sum){        System.out.println("部门平均薪水"+sum);        return sum;    }
public class SoftDep extends Department {    private boolean programming;    public SoftDep() {        super();    }    public SoftDep(boolean programming) {        super();        this.programming = programming;    }    public boolean isProgramming() {        return programming;    }    public void setProgramming(boolean programming) {        this.programming = programming;    }    public void business(){        System.out.println("为集团引进软件和系统");    }    public int aveSalary(int sum){        System.out.println("薪水较高");        return sum;    }    public void future(){        System.out.println("前景较好");    }
package lianxi2;public class HardDep extends Department {    private boolean communicate;    public boolean isCommunicate() {        return communicate;    }    public void setCommunicate(boolean communicate) {        this.communicate = communicate;    }    public HardDep() {        super();    }    public HardDep(boolean communicate) {        super();        this.communicate = communicate;    }    @Override    public int aveSalary(int sum) {        System.out.println("薪水较低");        return sum;    }    @Override    public void business() {        System.out.println("负责引进硬件设施和网络");    }    public void future(){        System.out.println("前景堪忧");    }}
package lianxi2;public class TestDep {    public static void main(String[] args) {        TestDep t = new TestDep();        Department d = new Department("信息中心",001);        //d.name;                         --------------只是一个属性不能作为语句,调用一个方法可以        System.out.println(d.name);              System.out.println(d.getName());        System.out.println(d.getNumber());        d.business();        System.out.println(d.aveSalary(4000));                Department d1 = new SoftDep(true);     //向上转型,只能调用子类中重写父类的方法        if(d1 instanceof SoftDep){                   //    System.out.println(d1.isProgramming()); ---------------属性没有多态性        System.out.println(d1.aveSalary(5000));        d1.business();        SoftDep so = (SoftDep)d1;            //向下转型,可以调用所有子类中的方法        System.out.println("$"+so.aveSalary(5000));        so.future();        System.out.println(so.isProgramming());         t.func(new SoftDep());        }        System.out.println("\n\n");        t.func(new HardDep());    }    public void func(Department d3){        d3.business();        System.out.println(d3.aveSalary(6000));        if(d3 instanceof HardDep){            HardDep hd = (HardDep)d3;            hd.future();        }        if(d3 instanceof SoftDep){            SoftDep so = (SoftDep)d3;            so.future();        }    }}

转载于:https://www.cnblogs.com/yjtm53/p/4130402.html

你可能感兴趣的文章
2018-2019-2 网络对抗技术 20165202 Exp3 免杀原理与实践
查看>>
NPM慢怎么办 - nrm切换资源镜像
查看>>
Swift - UIView的常用属性和常用方法总结
查看>>
Swift - 异步加载各网站的favicon图标,并在单元格中显示
查看>>
Java编程思想总结笔记Chapter 5
查看>>
[LeetCode]662. Maximum Width of Binary Tree判断树的宽度
查看>>
【Python学习笔记】1.基础知识
查看>>
梦断代码阅读笔记02
查看>>
selenium学习中遇到的问题
查看>>
大数据学习之一——了解简单概念
查看>>
Linux升级内核教程(CentOS7)
查看>>
Lintcode: Partition Array
查看>>
分享适合个人站长的5类型网站
查看>>
类别的三个作用
查看>>
【SICP练习】85 练习2.57
查看>>
runC爆严重安全漏洞,主机可被攻击!使用容器的快打补丁
查看>>
Maximum Product Subarray
查看>>
solr相关配置翻译
查看>>
通过beego快速创建一个Restful风格API项目及API文档自动化(转)
查看>>
解决DataSnap支持的Tcp长连接数受限的两种方法
查看>>