⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shapefactory.java

📁 这是孙卫琴在"Java与面向对象编程"一书的源代码。
💻 JAVA
字号:
import java.util.*;
import java.io.*;

public class ShapeFactory {

  private static Properties shapes=new Properties();

  static{
    try{
      InputStream in=ShapeFactory.class.getResourceAsStream("panel.properties");
      shapes.load(in); //把配置信息加载到shapes对象中
    }catch(IOException e){throw new RuntimeException(e);}
  }

  public static Shape getShape(int type){
    try{
      //获得与形状类型匹配的形状类名
      String className=(String)shapes.get(String.valueOf(type));
      //运用Java反射机制构造形状对象
      return (Shape)Class.forName(className).newInstance();
    }catch(Exception e){return null;}
  }

}


/****************************************************
 * 作者:孙卫琴                                     *
 * 来源:<<Java面向对象编程>>                       *
 * 技术支持网址:www.javathinker.org                *
 ***************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -