×

java反射调用方法 java java 反射

java 反射机制的几个调用方法?java水仙花数问题

admin admin 发表于2022-07-06 12:24:34 浏览102 评论0

抢沙发发表评论

java 反射机制的几个调用方法


import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
* Created by IntelliJ IDEA.
* File: TestRef.java
* User: leizhimin
* Date: 2008-1-28 14:48:44
*/
public class TestRef {

public staticvoid main(String args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Foo foo = new Foo(“这个一个Foo对象!“);
Class clazz = foo.getClass();
Method m1 = clazz.getDeclaredMethod(“outInfo“);
Method m2 = clazz.getDeclaredMethod(“setMsg“, String.class);
Method m3 = clazz.getDeclaredMethod(“getMsg“);
m1.invoke(foo);
m2.invoke(foo, “重新设置msg信息!“);
String msg = (String) m3.invoke(foo);
System.out.println(msg);
}
}

class Foo {
private String msg;

public Foo(String msg) {
this.msg = msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public String getMsg() {
return msg;
}

public void outInfo() {
System.out.println(“这是测试Java反射的测试类“);
}
}

java水仙花数问题


/** * 两位自幂数:没有 * 三位自幂数:水仙花数 * 四位自幂数:四叶玫瑰数 * 五位自幂数:五角星数 */public class Yugi{ public static void main(String args){ String arr = {“水仙花数“,“四叶玫瑰数“,“五角星数“}; for(int i = 100; i 《 100000; i++){ String str = i + ““; int r = str.length(); int sum = 0; for(int j = 0; j 《 r; j++){ int num = Integer.parseInt(str.charAt(j)+““); sum += Math.pow(num, r); } if(sum == i){ System.out.println(arr[r - arr.length] + “: “ + i); } } }}

Java 课设


简单的五子棋程序
新建一个chess类,其中代码如下
package work;

import java.awt.Color;

public class Chess {
public static final int diamter=30;
private int x;//在网格中的索引,0~15
private int y;//在网格中的索引,0~15
private Color color;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Chess(int x, int y, Color color) {
super();
this.x = x;
this.y = y;
this.color = color;
}

}
然后在同一个包中新建FirstFrame类。代码如下
package work;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

//主函数
public class FirstFrame{
public static void main(String args) {

FirstFrame1 f=new FirstFrame1();

f.setVisible(true);

}
}

//窗体函数
class FirstFrame1 extends JFrame implements ActionListener{
JMenuBar menuBar;
JMenu sysMenu;
JMenuItem startMenuItem;
JMenuItem backMenuItem;
JMenuItem exitMenuItem;
DrawPanel myPanel=new DrawPanel();

public FirstFrame1(){
super(“娃哈哈“);

add(myPanel);
menuBar=new JMenuBar();
setJMenuBar(menuBar);
sysMenu=new JMenu(“系统(s)“);
sysMenu.setMnemonic(’s’);
menuBar.add(sysMenu);
startMenuItem=new JMenuItem(“开始“);
backMenuItem=new JMenuItem(“悔棋“);
exitMenuItem=new JMenuItem(“退出“);
sysMenu.add(startMenuItem);
sysMenu.add(backMenuItem);
sysMenu.addSeparator();
sysMenu.add(exitMenuItem);
startMenuItem.addActionListener(this);
backMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);

super.setSize(600,650);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e){
Object obj=e.getSource();
if(obj==startMenuItem){
myPanel.start(); //DrawPanel p=new DrawPanel();是错的;
}else if(obj==backMenuItem){
myPanel.back();
}else{
System.exit(0);
}

}

}
最后新建一个DrawPanel类代码如下
package work;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import sun.awt.geom.AreaOp.AddOp;

//画柄;
class DrawPanel extends JPanel implements MouseListener,MouseMotionListener{
final int MARGIN_TOP=60;
final int MARGIN_LEFT=30;
final int CELL_WIDTH=35;
final int COLS=15;
final int ROWS=15;
Chess chessList=new Chess[(COLS+1)*(ROWS+1)];
int chessCount=0;
boolean isBlack=true;
boolean gameOver=false;

public DrawPanel(){ //构造函数
Color color=new Color(200,250,200);
setBackground(color);
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
public void start(){
for(int i=0;i《chessList.length;i++)
chessList[i]=null;
isBlack=true;
gameOver=false;
chessCount=0;
repaint();
}
public void back(){
if (chessCount==0||gameOver==true)
return;
chessList[chessCount-1]=null;

chessCount--;
isBlack=!isBlack;

repaint();
}
@Override

protected void paintComponent(Graphics g) { //方法重写
// TODO Auto-generated method stub
super.paintComponent(g);
for(int i=0;i《=ROWS;i++){
g.drawLine(MARGIN_LEFT, MARGIN_TOP+i*CELL_WIDTH, MARGIN_LEFT+COLS*CELL_WIDTH, MARGIN_TOP+i*CELL_WIDTH);
}
for(int i=0;i《=COLS;i++){
g.drawLine(MARGIN_LEFT+i*CELL_WIDTH, MARGIN_TOP, MARGIN_LEFT+i*CELL_WIDTH, MARGIN_TOP+ROWS*CELL_WIDTH);
}
for(int i=0;i《chessCount;i++){ //绘制棋子数组
Chess chess=chessList[i];
int xPos=MARGIN_LEFT+CELL_WIDTH*chess.getX()-Chess.diamter/2;
int yPos=MARGIN_TOP+CELL_WIDTH*chess.getY()-Chess.diamter/2;
g.setColor(chess.getColor());
g.fillOval(xPos,yPos, Chess.diamter, Chess.diamter);
if(i==chessCount-1){
g.setColor(Color.red);
g.drawRect(xPos, yPos, Chess.diamter, Chess.diamter);
}
}
if(isBlack)
g.setColor(Color.black);
else
g.setColor(Color.white);
g.fillOval(8*CELL_WIDTH, CELL_WIDTH/2,Chess.diamter, Chess.diamter);
String colorStr=isBlack==false?“白方下“:“黑方下“;
g.setColor(Color.blue);
g.drawString(colorStr+“ 第“+chessCount/2+“步“, 9*CELL_WIDTH, CELL_WIDTH);
}

public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}

public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
super.setCursor(new Cursor(Cursor.HAND_CURSOR));
int x=(e.getX()-MARGIN_LEFT+Chess.diamter/2)/CELL_WIDTH;
int y=(e.getY()-MARGIN_TOP+Chess.diamter/2)/CELL_WIDTH;
if(x《0||y《0||x》COLS||y》ROWS) //超出棋盘边界
super.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

if(findChess(x,y)) //下过的地方不能再下
super.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
if(gameOver)
super.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}

public void mousePressed(MouseEvent e) {

int x=(e.getX()-MARGIN_LEFT+Chess.diamter/2)/CELL_WIDTH;
int y=(e.getY()-MARGIN_TOP+Chess.diamter/2)/CELL_WIDTH;
if(x《0||y《0||x》COLS||y》ROWS) //超出棋盘边界
return;
Color color=Color.black;
if(!isBlack) // 变颜色
color=Color.white;
if(findChess(x,y)) //下过的地方不能再下
return;
if(gameOver)
return;
chessList[chessCount]=new Chess(x,y,color);
repaint(); //重绘
isBlack=!isBlack;
chessCount++;
String colorStr=color==Color.white?“白方“:“黑方“;
int count={0,0,0,0};
for(int i=x-1;i》=0;i--){
if(findChess(i,y,color)){
count++;
}
else
break;
}
for(int i=x+1;i《=COLS;i++){
if(findChess(i,y,color)){
count++;
}
else
break;
}
for(int i=y-1;i》=0;i--){
if(findChess(x,i,color)){
count++;
}
else
break;
}
for(int i=y+1;i《=ROWS;i++){
if(findChess(x,i,color)){
count++;
}
else
break;
}
for(int i=x+1,j=y+1;i《=COLS&&j《=ROWS;i++,j++){
if(findChess(i,j,color)){
count++;
}
else
break;
}
for(int i=x-1,j=y-1;i》=0&&j》=0;i--,j--){
if(findChess(i,j,color)){
count++;
}
else
break;
}
for(int i=x+1,j=y-1;i《=COLS&&j》=0;i++,j--){

if(findChess(i,j,color)){
count++;
}
else
break;
}
for(int i=x-1,j=y+1;i》=0&&j《=ROWS;i--,j++){
if(findChess(i,j,color)){
count++;
}
else
break;
}
for(int i=0;i《4;i++){

if(count[i]》=4){
gameOver=true;
JOptionPane.showMessageDialog(this, “恭喜“+colorStr+“胜利“);
return;
}
}
System.out.println(e.getX()+“,“+e.getY());

}

public boolean findChess(int x,int y){
for(int i=0;i《chessCount;i++){
if(chessList[i].getX()==x && chessList[i].getY()==y)
return true;
}
return false;
}

public boolean findChess(int x,int y,Color color){
for(int i=0;i《chessCount;i++){
if(chessList[i].getX()==x && chessList[i].getY()==y && chessList[i].getColor()==color)
return true;
}
return false;
}

}
-JAVA