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

📄 area.java.zip.java

📁 计算面积
💻 JAVA
字号:
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里的不同,参数成员也不同 
@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface Name { 
 String originate(); 
 String community(); 
}  

  3、JavaEyer.java 

  代码

package lighter.javaeye.com; 

@Description("javaeye,做最棒的软件开发交流社区") 
public class JavaEyer { 
 @Name(originate="创始人:robbin",community="javaEye") 
 public String getName() 
 { 
  return null; 
 } 

 @Name(originate="创始人:江南白衣",community="springside") 
 public String getName2() 
 { 
  return "借用两位的id一用,写这一个例子,请见谅!"; 
 } 
}  

  4、最后,写一个可以运行提取JavaEyer信息的类TestAnnotation 

  代码

package lighter.javaeye.com; 

import java.lang.reflect.Method; 
import java.util.HashSet; 
import java.util.Set; 

public class TestAnnotation { 
 /** 
 * author lighter 
 * 说明:具体关天Annotation的API的用法请参见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("创建的社区:"+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 ("事件驱动程序");

        //btnPress就是这次点击操作中的事件源

        btnPress = new JButton();

        btnPress.setText ("Press");

        btnPress.setName ("Press");

        btnPress.setBounds (63, 98, 99, 23);

        //向事件源btnPress植入侦听器对象ButtonEventHandler

        btnPress.addActionListener (new ButtonEventHandler(this));

        frame.getContentPane ().add(btnPress);

        frame.getContentPane ().add(txtMessage);

    }
 

  侦听器创建的代码片断:

//侦听器对象ButtonEventHandler(用来侦听按钮的点击操作)

    class ButtonEventHandler implements ActionListener {

        //窗体对象

        private EventDemo form = null;

        //通过构造体传入窗体对象,

        //作用在于让侦听器对象明白事件源处于

        //哪个窗体容器中

        public ButtonEventHandler(EventDemo form) {

            this.form = form;

        }

 

        //委托方法

        public void actionPerformed(ActionEvent e) {

           //该方法将会把事件的处理权交给窗体容器类的

//btnPress_Click方法处理。

            this.form.btnPress_Click(e);

        }

    }

 

 真正的事件处理代码片断:

     /**

     * 按钮btnPress的事件处理方法。

     * 

     * @param e 事件参数

     */

    private void btnPress_Click(ActionEvent e) {

        

        String message = "你点击的按钮名叫:" 

            + ((JButton) e.getSource()).getName();

        

        this.txtMessage.setText(message);

    }

 

⌨️ 快捷键说明

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