欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一段输出明清皇帝年号的代码

程序员文章站 2022-07-15 15:26:46
...
package org.wx.dev.wx.api;

import java.lang.reflect.InvocationTargetException;

//清朝
public class QingDynasty{

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        try {
            StringBuffer name = new StringBuffer("洪武建文永乐洪熙宣德正统景泰天顺成化弘治正德嘉靖隆庆万历泰昌天启崇祯弘光隆武绍武永历天命天聪顺治康熙雍正乾隆嘉庆道光咸丰同治光绪宣统");
            for (int i = 0; i < name.length(); i= i+2) {
                String emperorName = name.substring(i,i+2);
                Emperor emperor = (Emperor) Class.forName("org.wx.dev.wx.api.Emperor").getConstructor(String.class).newInstance(emperorName);
                System.out.println(emperor.toString());
                Thread.sleep(300);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
//皇帝类
class Emperor{
    //年号
    private String name;

    public Emperor(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "皇帝年号--"+ name + '\'' ;
    }
}

相关标签: java se 杂谈