package test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Test3 { String str1 = "111"; String str2 = "222"; String str3 = "333"; char[] c = {'a','b','c','d'}; public Test3(String str){ this.str3 = str; } public static void main(String[] args) { Test3 t = new Test3("333"); Class c = t.getClass(); Method[] m = c.getDeclaredMethods(); Field[] f = c.getDeclaredFields(); for(int i=0;i<f.length;i++){ if(f[i].getType()==char[].class) continue; try { //打印所有属性值 如果是静态属性,可以直接用Test3.class代替 System.out.println(f[i].getName() + " " + f[i].get(t).toString()); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } for(int i=0;i<m.length;i++){ if(m[i].getReturnType()==String.class){ try { //打印所有为String的方法返回值 System.out.println(m[i].getName() + " " +m[i].invoke(t)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } TriFunc tri = new TriFunc(); // 生成一块25×100的画布 Canvas canvas = new Canvas(30, 100); // 画sin曲线,周期为2 tri.drawSin(canvas, 2); canvas.printCanvas(); System.out.println(); canvas.reset(); // 画cos曲线,周期为2 tri.drawCos(canvas, 2); canvas.printCanvas(); } public String getStr1() { return str1; } public String getStr2() { return str2; } public String getStr3() { return str3; } @Override public int hashCode() { final int PRIME = 31; int result = super.hashCode(); result = PRIME * result + ((str1 == null) ? 0 : str1.hashCode()); result = PRIME * result + ((str2 == null) ? 0 : str2.hashCode()); result = PRIME * result + ((str3 == null) ? 0 : str3.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; final Test3 other = (Test3) obj; if (str1 == null) { if (other.str1 != null) return false; } else if (!str1.equals(other.str1)) return false; if (str2 == null) { if (other.str2 != null) return false; } else if (!str2.equals(other.str2)) return false; if (str3 == null) { if (other.str3 != null) return false; } else if (!str3.equals(other.str3)) return false; return true; } } class TriFunc { /** * 画sin曲线 * @param canvas 画布 * @param period 曲线周期 */ public void drawSin(Canvas canvas, double period) { char[][] chars = canvas.getCanvas(); // x 轴的比率 double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1); // y 轴的放大倍率 int yMulti = (canvas.getHeight() - 1) / 2; for(int i = 0; i < canvas.getWidth(); i++) { // 将数组索引映射为横坐标值 double k = (i - canvas.getWidth() / 2) * xRatio; // 将sin值映射为数组索引 int h = yMulti - (int)Math.round(Math.sin(k) * yMulti); chars[h][i] = Canvas.FILL_CHAR; } } /** * 画cos曲线 * @param canvas 画布 * @param period 曲线周期 */ public void drawCos(Canvas canvas, double period) { char[][] chars = canvas.getCanvas(); double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1); int yMulti = (canvas.getHeight() - 1) / 2; for(int i = 0; i < canvas.getWidth(); i++) { double k = (i - canvas.getWidth() / 2) * xRatio; int h = yMulti - (int)Math.round(Math.cos(k) * yMulti); chars[h][i] = Canvas.FILL_CHAR; } } } class Canvas { private int height; private int width; private char[][] canvas; // 填充字符 public static char FILL_CHAR = '#'; // 空白字符 public static char BLANK_CHAR = ' '; /** * 构建一块画布 * @param height * @param width */ public Canvas(int height, int width) { // 由于需要画坐标轴,所以得采用奇数 this.height = height % 2 == 0 ? height + 1 : height; this.width = width % 2 == 0 ? width + 1 : width; init(); } /** * 初始化画布 */ private void init() { this.canvas = new char[height][width]; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { canvas[i][j] = BLANK_CHAR; } } addAxis(); } /** * 添加坐标轴 */ private void addAxis() { // 添加横坐标 int y = height / 2; for(int x = 0; x < width; x++) { canvas[y][x] = '-'; } // 添加纵坐标 int xx = width / 2; for(int yy = 0; yy < height; yy++) { canvas[yy][xx] = '|'; } // 添加原点 canvas[y][xx] = '+'; } /** * 输出画布 */ public void printCanvas() { for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { System.out.print(canvas[i][j]); } System.out.println(); } } /** * 清空画布 */ public void reset() { init(); } public int getHeight() { return height; } public int getWidth() { return width; } public char[][] getCanvas() { return canvas; } }
java 反射 画Sin、Cos函数曲线
最新推荐文章于 2023-04-04 07:10:50 发布