hjy2099 发表于 2013-1-27 05:03:33

多态的运用 实现java 数据类型判断

package javaBasic;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif/** *//**
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif * 用多态实现数据类型的判断
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif * 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif * @author <a href="mailto:jiangtuanming@geong.com">jiangtuanming</a><br>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif *         or <a href="mailto:xiao_jiang51@163.com">蒋团明</a>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif * @version 1.0
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif */
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic class Polymorphism ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private final static String INT_TYPE = "int";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private final static String LONG_TYPE = "long";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private final static String DOUBLE_TYPE = "double";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private final static String FLOAT_TYPE = "float";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private final static String CHAR_TYPE = "char";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private final static String BYTE_TYPE = "byte";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private final static String SHORT_TYPE = "short";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    private final static String BOOLAEN_TYPE = "boolean";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static String getType(int i) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return INT_TYPE;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static String getType(long l) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return LONG_TYPE;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static String getType(double d) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return DOUBLE_TYPE;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static String getType(float f) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return FLOAT_TYPE;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static String getType(char c) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return CHAR_TYPE;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static String getType(byte by) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return BYTE_TYPE;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static String getType(short s) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return SHORT_TYPE;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static String getType(boolean bo) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return BOOLAEN_TYPE;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    /** *//**
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * ie:javaBasic.Polymorphism@de6ced to:javaBasic.Polymorphism
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param obj
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @return
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif     */
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static String getType(Object obj) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        return obj != null ? obj.toString().split("@")[0] : null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    /** *//**
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * test
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * @param args
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif     */
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    public static void main(String[] args) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        int i = 1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        double d = 1.8;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        long l = 76;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        short s = 1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        char c = ',';
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        float f = 1.1f;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        boolean bo = false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        byte bt = 1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Polymorphism poly = new Polymorphism();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(Polymorphism.getType(i));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(Polymorphism.getType(l));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(Polymorphism.getType(d));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(Polymorphism.getType(s));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(Polymorphism.getType(c));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(Polymorphism.getType(f));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(Polymorphism.getType(bo));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(Polymorphism.getType(bt));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        System.out.println(Polymorphism.getType(poly));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
 
输出:
int
long
double
short
char
float
boolean
byte
javaBasic.Polymorphism
 
转自:http://blog.csdn.net/xiao_jiang51/archive/2008/03/21/2202720.aspx
页: [1]
查看完整版本: 多态的运用 实现java 数据类型判断