JAVA | 15

来源:互联网 发布:网络包工头在哪找活 编辑:程序博客网 时间:2024/05/16 05:41
package com.company;class Sex{    private String title;    private static final Sex MALE = new Sex("male");    private static final Sex FEMALE = new Sex("female");    private Sex(String title){        this.title = title;    }    public String toString(){        return this.title;    }    public static Sex getSex(String sex){        if("male".equals(sex)){            return MALE;        }        if("female".equals(sex)){            return FEMALE;        }        else return null;    }}public class Main {    public static void main(String[] args) {        Sex sexA = Sex.getSex("female");        Sex sexB = Sex.getSex("male");        System.out.println(sexA);        System.out.println(sexB);    }}
原创粉丝点击