zoukankan      html  css  js  c++  java
  • 如果对象的类型为T1,就做某件事;如果对象的类型为T2,就做另外一件事,请赏自己一个巴掌

    前人的至理名言,看看之前我写的代码,应该被赏了n个巴掌了吧。
    看看下面的例子:

    1.被赏巴掌的例子

    package com.tn.salary;
    
    public interface Employ {
       int getSalary();
    }
    
    package com.tn.salary;
    
    public class Manager implements Employ {
    
    	public int getSalary() {
    		return 5000;
    	}
    
    	public int getBonus() {
    		return 300;
    	}
    }
    
    package com.tn.salary;
    
    public class Programmmer implements Employ {
    
    	public int getSalary() {
    		return 3000;
    	}
    
    }
    
    package com.tn.salary;
    
    public class Payroll {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Payroll payroll = new Payroll();
    		payroll.caculatePayRoll(new Programmmer());
    		payroll.caculatePayRoll(new Manager());
    	}
    
    	private int caculatePayRoll(Employ em) {
    		int money = em.getSalary();
    		if (em instanceof Manager) {
    			money += ((Manager) em).getBonus();
    		}
    		System.out.println("the total money is"+money);
    		return money;
    	}
    
    }
    

      

    2.脸部保持完整的例子

    package com.tn.salary.refector;
    
    public interface Employ {
       int getSalary();
       int getBonus();
    }
    
    package com.tn.salary.refector;
    
    public class Manager implements Employ {
    
    	private static final int MANAGER_MONTHLY_BONUS = 300;
    	private static final int MANAGER_MONTHLY_SALARY = 5000;
    
    	public int getSalary() {
    		return MANAGER_MONTHLY_SALARY;
    	}
    
    	public int getBonus() {
    		return MANAGER_MONTHLY_BONUS;
    	}
    }
    
    package com.tn.salary.refector;
    
    public class Programmmer implements Employ {
    
    	private static final int PROGRAMMER_MONTHLY_BONUS = 0;
    	private static final int PROGRAMMMER_MONTHLY_SALARY = 3000;
    
    	public int getSalary() {
    		return PROGRAMMMER_MONTHLY_SALARY;
    	}
    
    	public int getBonus() {
    		return PROGRAMMER_MONTHLY_BONUS;
    	}
    
    }
    
    package com.tn.salary.refector;
    
    public class Payroll {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Payroll payroll = new Payroll();
    		payroll.caculatePayRoll(new Programmmer());
    		payroll.caculatePayRoll(new Manager());
    	}
    
    	private int caculatePayRoll(Employ em) {
    		int money = em.getSalary()+em.getBonus();
    		System.out.println("the money is"+money);
    		return money;
    	}
    
    }
    
  • 相关阅读:
    leetcode Move Zeroes
    leetcode Same Tree
    leetcode range sum query
    leetcode Invert Binary Tree
    leetcode【sql】 Delete Duplicate Emails
    mac编译PHP报错 configure: error: Please reinstall the libcurl distribution
    Linux添加系统环境变量的两种方法
    Mysql获取去重后的总数
    MySQL查询order by相减select相减的Sql语句
    修改maven本地仓库路径
  • 原文地址:https://www.cnblogs.com/budoudou/p/2134888.html
Copyright © 2011-2022 走看看