?? area.java.zip.java
字號(hào):
import java.util.Vector;
interface Shape{
public abstract double area();
}
class Circle implements Shape{
int r;
Circle(int r){
this.r=r;
}
Circle(){
this.r=0;
}
public double area(){
return 3.14*r*r;
}
}
class Rectangle implements Shape{
int l ,w;;
Rectangle(int l ,int w){
this.l=l;
this.w=w;
}
Rectangle(){
this.l=0;
this.w=0;
}
public double area(){
return l*w;
}
}
class Star implements Shape{
public double area(){
return 0;
}
}
public class Area{
public static void main(String[] args){
Vector v=new Vector();
for(int i=0;i<3;i++){
int s= (int)(10*Math.random());
if(s%3==0){
v.add(new Circle(i));
}
else if(s%3==1){
v.add(new Rectangle(i,i+1));
}
else if(s%3==2){
v.add(new Star());
}
}
for(int j=0;j<v.size();j++){
Shape s=(Shape)v.get(j);
System.out.println(s.area());
}
}
}
package lighter.javaeye.com;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//注意這里的@Target與@Description里的不同,參數(shù)成員也不同
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Name {
String originate();
String community();
}
3、JavaEyer.java
代碼
package lighter.javaeye.com;
@Description("javaeye,做最棒的軟件開發(fā)交流社區(qū)")
public class JavaEyer {
@Name(originate="創(chuàng)始人:robbin",community="javaEye")
public String getName()
{
return null;
}
@Name(originate="創(chuàng)始人:江南白衣",community="springside")
public String getName2()
{
return "借用兩位的id一用,寫這一個(gè)例子,請(qǐng)見諒!";
}
}
4、最后,寫一個(gè)可以運(yùn)行提取JavaEyer信息的類TestAnnotation
代碼
package lighter.javaeye.com;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
public class TestAnnotation {
/**
* author lighter
* 說明:具體關(guān)天Annotation的API的用法請(qǐng)參見javaDoc文檔
*/
public static void main(String[] args) throws Exception {
String CLASS_NAME = "lighter.javaeye.com.JavaEyer";
Class test = Class.forName(CLASS_NAME);
Method[] method = test.getMethods();
boolean flag = test.isAnnotationPresent(Description.class);
if(flag)
{
Description des = (Description)test.getAnnotation(Description.class);
System.out.println("描述:"+des.value());
System.out.println("-----------------");
}
//把JavaEyer這一類有利用到@Name的全部方法保存到Set中去
Set set = new HashSet();
for(int i=0;i {
boolean otherFlag = method[i].isAnnotationPresent(Name.class);
if(otherFlag) set.add(method[i]);
}
for(Method m: set)
{
Name name = m.getAnnotation(Name.class);
System.out.println(name.originate());
System.out.println("創(chuàng)建的社區(qū):"+name.community());
}
}
}
private void initialize() {
frame = new JFrame();
frame.getContentPane ().setLayout (null);
frame.setBounds (100, 100, 247, 165);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle ("事件驅(qū)動(dòng)程序");
//btnPress就是這次點(diǎn)擊操作中的事件源
btnPress = new JButton();
btnPress.setText ("Press");
btnPress.setName ("Press");
btnPress.setBounds (63, 98, 99, 23);
//向事件源btnPress植入偵聽器對(duì)象ButtonEventHandler
btnPress.addActionListener (new ButtonEventHandler(this));
frame.getContentPane ().add(btnPress);
frame.getContentPane ().add(txtMessage);
}
偵聽器創(chuàng)建的代碼片斷:
//偵聽器對(duì)象ButtonEventHandler(用來偵聽按鈕的點(diǎn)擊操作)
class ButtonEventHandler implements ActionListener {
//窗體對(duì)象
private EventDemo form = null;
//通過構(gòu)造體傳入窗體對(duì)象,
//作用在于讓偵聽器對(duì)象明白事件源處于
//哪個(gè)窗體容器中
public ButtonEventHandler(EventDemo form) {
this.form = form;
}
//委托方法
public void actionPerformed(ActionEvent e) {
//該方法將會(huì)把事件的處理權(quán)交給窗體容器類的
//btnPress_Click方法處理。
this.form.btnPress_Click(e);
}
}
真正的事件處理代碼片斷:
/**
* 按鈕btnPress的事件處理方法。
*
* @param e 事件參數(shù)
*/
private void btnPress_Click(ActionEvent e) {
String message = "你點(diǎn)擊的按鈕名叫:"
+ ((JButton) e.getSource()).getName();
this.txtMessage.setText(message);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -