Java的面向?qū)ο缶幊?/a>">Java的面向?qū)ο缶幊?/a>
687
2022-05-29
說明:
因為上個代碼,總是要輸入完整的絕對路徑,比較麻煩,于是,就寫了這個小程序,直接進入文件對話框選擇需要運行的class文件。
只需要提前輸入完整的類名。
注意:加的MyTest必須打個包,加上:
import cn.hncu.myJuniitApp.vo.MyTest;
不然不是同一個注解呢。
測試的類:
package cn.hncu.myJuniitApp; import cn.hncu.myJuniitApp.vo.MyTest; public class MyJunitTest { public void run1(){ System.out.println("run1()....."); } @MyTest public void run2(){ System.out.println("run2().....含有MyTest"); } public void run3(){ System.out.println("run3()....."); } @MyTest public void run4(){ System.out.println("run4().....含有MyTest"); } public void run5(){ System.out.println("run5()....."); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
然后是注解類:
package cn.hncu.myJuniitApp.vo; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //運行時也存在的,如果想看到運行結(jié)果,必須定義這個注解的保持性為運行時 @Retention (RetentionPolicy.RUNTIME) @Target (ElementType.METHOD)//限制這個注解只能用在方法上面 public @interface MyTest { }
1
2
3
4
5
6
7
8
9
10
11
12
13
數(shù)據(jù)層:
接口:
package cn.hncu.myJuniitApp.dao.dao; /** * 數(shù)據(jù)層接口 * @author 陳浩翔 * * @version 1.0 2016-5-6 */ public interface JunitDao { public Class> findClass(String name,String className); }
1
2
3
4
5
6
7
8
9
10
11
12
實現(xiàn)類:
package cn.hncu.myJuniitApp.dao.impl; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import cn.hncu.myJuniitApp.dao.dao.JunitDao; /** * 數(shù)據(jù)層的實現(xiàn)類 * @author 陳浩翔 * * @version 1.0 2016-5-6 */ public class JunitDaoImpl extends ClassLoader implements JunitDao{ @Override public Class> findClass(String name, String className) { byte buf[] = loadClassData(name); Class c = defineClass(className, buf, 0, buf.length); return c; } private byte[] loadClassData(String name) { byte buf[]=null; try { FileInputStream in = new FileInputStream(name); ByteArrayOutputStream bout = new ByteArrayOutputStream(); int len=0; byte[] b = new byte[1024]; while((len=in.read(b))!=-1){ bout.write(b, 0, len); } in.close(); bout.close(); buf=bout.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buf; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
工廠方法:
package cn.hncu.myJuniitApp.dao.factory; import cn.hncu.myJuniitApp.dao.dao.JunitDao; import cn.hncu.myJuniitApp.dao.impl.JunitDaoImpl; /** * 數(shù)據(jù)層的工廠方法 * @author 陳浩翔 * * @version 1.0 2016-5-6 */ public class JunitDaoFactory { public static JunitDao getJunitDao(){ return new JunitDaoImpl(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
邏輯層:
接口:
package cn.hncu.myJuniitApp.business.ebi; /** * 邏輯層接口 * @author 陳浩翔 * * @version 1.0 2016-5-6 */ public interface JunitEbi { public void run(String name,String className); }
1
2
3
4
5
6
7
8
9
10
11
12
實現(xiàn)類:
package cn.hncu.myJuniitApp.business.ebo; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import cn.hncu.myJuniitApp.business.ebi.JunitEbi; import cn.hncu.myJuniitApp.dao.factory.JunitDaoFactory; import cn.hncu.myJuniitApp.vo.MyTest; /** * 邏輯層的實現(xiàn)類 * @author 陳浩翔 * * @version 1.0 2016-5-6 */ public class JunitEbo implements JunitEbi{ //通過數(shù)據(jù)層獲得對象的Class @Override public void run(String name, String className) { Class c = JunitDaoFactory.getJunitDao().findClass(name, className); try { Object obj = c.newInstance(); Method ms[] = c.getDeclaredMethods();//獲得當前類的所有聲明方法,包括私有的 for(Method m : ms){//增強for循環(huán)遍歷 if(m.isAnnotationPresent(MyTest.class)){//如果這個方法有這個注解,就運行 m.invoke(obj, null);//現(xiàn)在知道為什么要無參了吧,這樣方便很多,直接就可以調(diào)用了。 } } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
工廠方法:
package cn.hncu.myJuniitApp.business.factory; import cn.hncu.myJuniitApp.business.ebi.JunitEbi; import cn.hncu.myJuniitApp.business.ebo.JunitEbo; /** * 邏輯層的工廠方法 * @author 陳浩翔 * * @version 1.0 2016-5-6 */ public class JunitEbiFactory { public static JunitEbi getJunitEbi(){ return new JunitEbo(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
界面和main方法:
package cn.hncu.myJuniitApp; import java.awt.Dimension; import java.awt.Toolkit; import java.io.File; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import cn.hncu.myJuniitApp.business.factory.JunitEbiFactory; /** * * @author 陳浩翔 * @version 1.0 2016-5-6 */ public class myJunit extends javax.swing.JFrame { public myJunit() { initComponents(); } private void initComponents() { jLabel1 = new javax.swing.JLabel(); tfdClassName = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); btnFileName = new javax.swing.JButton(); btnRun = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); //獲得系統(tǒng)屏幕分辨率 Toolkit t = Toolkit.getDefaultToolkit() ; Dimension size=t.getScreenSize(); setBounds((int)size.getWidth()/4,(int)size.getHeight()/4,400,400); getContentPane().setLayout(null); jLabel1.setFont(new java.awt.Font("Dialog", 1, 30)); jLabel1.setForeground(new java.awt.Color(204, 0, 0)); jLabel1.setText("MyJunit-\u8fd0\u884c\u4efb\u610f\u7a7a\u53c2\u65b9\u6cd5"); getContentPane().add(jLabel1); jLabel1.setBounds(10, 20, 380, 80); tfdClassName.setFont(new java.awt.Font("Dialog", 1, 18)); tfdClassName.setForeground(new java.awt.Color(255, 0, 0)); getContentPane().add(tfdClassName); tfdClassName.setBounds(30, 180, 350, 40); jLabel2.setFont(new java.awt.Font("Dialog", 1, 18)); jLabel2.setForeground(new java.awt.Color(0, 0, 255)); jLabel2.setText("請先輸入完整類名(再選擇文件)"); getContentPane().add(jLabel2); jLabel2.setBounds(30, 120, 310, 80); btnFileName.setFont(new java.awt.Font("Dialog", 1, 18)); btnFileName.setText("\u9009\u62e9\u6587\u4ef6"); btnFileName.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnFileNameActionPerformed(evt); } }); getContentPane().add(btnFileName); btnFileName.setBounds(40, 270, 110, 50); btnRun.setFont(new java.awt.Font("Dialog", 1, 18)); btnRun.setText("\u8fd0\u884c"); btnRun.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnRunActionPerformed(evt); } }); getContentPane().add(btnRun); btnRun.setBounds(240, 270, 110, 50); } private void btnRunActionPerformed(java.awt.event.ActionEvent evt) { if(fileName==null){ JOptionPane.showMessageDialog(this, "請先選擇文件!"); } JunitEbiFactory.getJunitEbi().run(fileName, className); } private void btnFileNameActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser jfc = new JFileChooser(); //打開文件選擇對話框 int result = jfc.showOpenDialog(this); File file=null; if(result==JFileChooser.APPROVE_OPTION){//選擇了文件 file=jfc.getSelectedFile();//獲得選擇的文件 //System.out.println(file.getParent()); //System.out.println(file.getName()); fileName = file.getParent()+"\"+file.getName(); className = tfdClassName.getText().trim(); if("".equals(className)){ JOptionPane.showMessageDialog(this, "請輸入正確的完整類名!??!"); return ; } //System.out.println(fileName); //System.out.println(className); } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new myJunit().setVisible(true); } }); } private javax.swing.JButton btnFileName;//選擇文件的按鈕 private javax.swing.JButton btnRun;//運行含有@MyTest的空參方法 private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JTextField tfdClassName; private String fileName =null; private String className =null; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
下面看看一些程序的圖片:
先看下運行結(jié)果吧:我把那個測試類移到了d盤去了的。
run2().....含有MyTest run4().....含有MyTest
1
2
3
也許有人認為一個類完全就可以解決這個問題了,你還寫這么多的包,這么多類,這不是麻煩嘛。
我想說,雖然麻煩,但是可以讓我們看起來結(jié)構(gòu)清楚,誰調(diào)用誰也很明白,而且是面向接口編程。
這個寫得更加規(guī)范,特別是以后到公司工作,基本上都是幾個人開發(fā)同一個項目的,這個人寫這里的,那個人寫那里的,如果沒有規(guī)范,怎么行呢。怎么合作開發(fā)同一個項目呢。
所以,分包是必須的。分邏輯層和數(shù)據(jù)層也是需要的。特別是一個層的鐵三角,必須都要有。
接口,工廠方法,實現(xiàn)類,缺一不可?。。?/p>
Java
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。