程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 基於接話柄現java靜態署理示例

基於接話柄現java靜態署理示例

編輯:關於JAVA

基於接話柄現java靜態署理示例。本站提示廣大學習愛好者:(基於接話柄現java靜態署理示例)文章只能為提供參考,不一定能成為您想要的結果。以下是基於接話柄現java靜態署理示例正文


Subject.java

package _20140416_;

import java.util.List;

public interface Subject {
   public String say(String name,int age);
   public List<Person> getAllList(String name);
}

RealSubject.java

package _20140416_;

import java.util.ArrayList;
import java.util.List;

public class RealSubject implements Subject {

 private String name;

 public RealSubject(String name) {
  this.name = name;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 @Override
 public String say(String name, int age) {
  return "名字:" + name + "_年紀:" + age;
 }

 @Override
 public List<Person> getAllList(String name) {
  List<Person> list = new ArrayList<Person>();
  list.add(new Person("A", 20));
  list.add(new Person("B", 20));
  list.add(new Person("C", 20));
  list.add(new Person("D", 20));
  System.out.println(name);
  return list;
 }

 @Override
 public int hashCode() {
  return 10010;
 }

 @Override
 public boolean equals(Object obj) {
  if(obj instanceof RealSubject){
   RealSubject real = (RealSubject)obj;
   System.out.println("getName():"+real.getName());
   System.out.println("this.name:"+this.name);
   if(real.getName()==this.name){
     System.out.println("雷同");
   }else{
    System.out.println("不雷同");
   }
  }
  return true;
 }
}

MyInvercationHander.java

package _20140416_;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyInvercationHander implements InvocationHandler{

 private Object obj;

 public Object bind(Object obj){
  this.obj=obj;
  return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),this);
 }

 @Override
 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable {
  Object temp = method.invoke(this.obj, args);
  //這裡做切面操作 好比我要加功效
  System.out.println("辦法之前!");
  return temp;
 }

}

Person.java

package _20140416_;

public class Person {
 private String name;
 private int age;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public Person(String name, int age) {
  super();
  this.name = name;
  this.age = age;
 }

}

MainTest.java

package _20140416_;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MainTest {
 public static void main(String[] args) {
      Subject subject = (Subject) new MyInvercationHander().bind(new RealSubject("中國"));
      System.out.println(subject.say("莫建鋒",22));
      System.out.println(subject.getAllList("張三"));
      Map<String,Integer> myMap = new HashMap<String, Integer>();
      myMap.put("A",1);
      myMap.put("B",2);
      myMap.put("C",3);
      myMap.put("D",4);
      myMap.put("E",5);
      Set<Map.Entry<String,Integer>> myEntrySet = myMap.entrySet();
      Iterator<Map.Entry<String,Integer>> it = myEntrySet.iterator();
      while(it.hasNext()){
       Map.Entry<String,Integer> entry = it.next();
       System.out.print(entry.getKey()+":");
       System.out.println(entry.getValue());

      }
      System.out.println(new RealSubject("中國").hashCode());
      System.out.println(new RealSubject("中國").equals(new RealSubject("中dd國")));
      String info = new String("1");
      String info1 = new String("1");
      System.out.println(new Integer('1'));
      System.out.println(info.hashCode());
      System.out.println(info1.hashCode());
      System.out.println(info==info1);
      System.out.println(info.equals(info1));
      short i = 1;
      System.out.println(i);
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved