一、多态
1.Abstract_Music.java(抽象)
enum Note1{
Low,Middle,High
}
abstract class Instrument1{
abstract void play(Note n);
abstract void adjust();
}
class Wind1 extends Instrument1{
void play(Note n){System.out.println("Wind1 play()");}
void adjust(){System.out.println("Wind1 adjust()");}
}
class Percussion1 extends Instrument1{
void play(Note n){System.out.println("Percussion1 play()");}
void adjust(){System.out.println("Percussion1 adjust()");}
}
class Stringed1 extends Instrument1{
void play(Note n){System.out.println("Stringed1 play()");}
void adjust(){System.out.println("Stringed1 adjust()");}
}
class Brass1 extends Instrument1{
void play(Note n){System.out.println("Brass1 play()");}
void adjust(){System.out.println("Brass1 adjust()");}
}
class Woodwind1 extends Instrument1{
void play(Note n){System.out.println("Woodwind1 play()");}
void adjust(){System.out.println("Woodwind1 adjust()");}
}
public class Abstract_Music {
public static void tune(Instrument1 i){i.play(Note.Low);}
public static void tuneall(Instrument1[] e){
for(Instrument1 i:e)
tune(i);
}
public static void main(String[] args){
System.out.println("===========single orchestra===========");
Instrument1 orchestra4=new Brass1();
orchestra4.adjust();
System.out.println("===========many orchestra===========");
Instrument1[] orchestra3={new Woodwind1(),new Stringed1(),new Wind1(),new Percussion1()};
Abstract_Music m=new Abstract_Music();
m.tuneall(orchestra3);
}
}
run:
===========single orchestra===========
Brass1 adjust()
===========many orchestra===========
Woodwind1 play()
Stringed1 play()
Wind1 play()
Percussion1 play()
BUILD SUCCESSFUL (total time: 0 seconds)
2.Interface_Music.java(接口)
enum Note2{
Low,Middle,High
}
interface Instrument2{
void play(Note n);
void adjust();
}
class Wind2 implements Instrument2{
public void play(Note n){System.out.println("Wind2 play()"+n);}
public void adjust(){System.out.println("Wind2 adjust()");}
}
class Percussion2 implements Instrument2{
public void play(Note n){System.out.println("Percussion2 play()"+n);}
public void adjust(){System.out.println("Percussion2 adjust()");}
}
class Stringed2 implements Instrument2{
public void play(Note n){System.out.println("Stringed2 play()"+n);}
public void adjust(){System.out.println("Stringed2 adjust()");}
}
class Brass2 implements Instrument2{
public void play(Note n){System.out.println("Brass2 play()"+n);}
public void adjust(){System.out.println("Brass2 adjust()");}
}
class Woodwind2 implements Instrument2{
public void play(Note n){System.out.println("Woodwind2 play()"+n);}
public void adjust(){System.out.println("Woodwind2 adjust()");}
}
public class Interface_Music {
public static void tune(Instrument2 i){i.play(Note.High);}
public static void tuneall(Instrument2[] e){
for(Instrument2 i:e)
tune(i);
}
public static void main(String[] args){
System.out.println("===========single orchestra===========");
Instrument2 orchestra7=new Brass2();
orchestra7.adjust();
System.out.println("===========many orchestra===========");
Instrument2[] orchestra8={new Woodwind2(),new Stringed2(),new Wind2(),new Percussion2()};
Interface_Music n=new Interface_Music();
n.tune(orchestra7);
n.tuneall(orchestra8);
}
}
run:
===========single orchestra===========
Brass2 adjust()
===========many orchestra===========
Brass2 play()High
Woodwind2 play()High
Stringed2 play()High
Wind2 play()High
Percussion2 play()High
BUILD SUCCESSFUL (total time: 0 seconds)
3.Music.java(继承)
enum Note{
Low,Middle,High
}
class Instrument{
void play(Note n){System.out.println("Instrument play()");}
void adjust(){System.out.println("Instrument adjust()");}
}
class Wind extends Instrument{
void play(Note n){System.out.println("Wind play()");}
void adjust(){System.out.println("Wind adjust()");}
}
class Percussion extends Instrument{
void play(Note n){System.out.println("Percussion play()");}
void adjust(){System.out.println("Percussion adjust()");}
}
class Stringed extends Instrument{
void play(Note n){System.out.println("Stringed play()");}
void adjust(){System.out.println("Stringed adjust()");}
}
class Brass extends Instrument{
void play(Note n){System.out.println("Brass play()");}
void adjust(){System.out.println("Brass adjust()");}
}
class Woodwind extends Instrument{
void play(Note n){System.out.println("Woodwind play()");}
void adjust(){System.out.println("Woodwind adjust()");}
}
public class Music {
public static void tune(Instrument i){i.play(Note.Low);}
public static void tuneall(Instrument[] e){
for(Instrument i:e)
tune(i);
}
public static void main(String[] args){
System.out.println("==========orchestra============");
Instrument orchestra=new Brass();
orchestra.adjust();
System.out.println("===========orchestra1===========");
Instrument[] orchestra1={new Instrument(),new Woodwind(),new Stringed(),new Wind(),new Percussion()};
Music m=new Music();
m.tuneall(orchestra1);
}
}
run:
==========orchestra============
Brass adjust()
===========orchestra1===========
Instrument play()
Woodwind play()
Stringed play()
Wind play()
Percussion play()
BUILD SUCCESSFUL (total time: 0 seconds)
二、多重继承
1.Adventure.java
interface CanFight{
void fight();
}
interface CanSwim{
void swim();
}
interface CanFly{
void fly();
}
class AcionCharacter{
public void fight(){System.out.println("AcionCharacter can fight.");}
}
class Hero extends AcionCharacter implements CanFight,CanSwim,CanFly{
@Override
public void swim() {
System.out.println("Hero AcionCharacter can swim.");
}
@Override
public void fly() {
System.out.println("Hero AcionCharacter can fly.");
}
}
public class Adventure {
public static void t(CanFight x){x.fight();}
public static void u(CanSwim x){x.swim();}
public static void v(CanFly x){x.fly();}
public static void w(AcionCharacter x){x.fight();}
public static void main(String[] args){
Hero h=new Hero();
t(h);
u(h);
v(h);
w(h);
}
}
run:
AcionCharacter can fight.
Hero AcionCharacter can swim.
Hero AcionCharacter can fly.
AcionCharacter can fight.
BUILD SUCCESSFUL (total time: 0 seconds)
2.HorrorShow.java
interface Monster{
void menace();
}
interface DangerousMonster extends Monster{
void destroy();
}
interface Lethal{
void kill();
}
interface Vampire extends DangerousMonster,Lethal{
void drinkblood();
}
class BadVampire implements Vampire{
@Override
public void drinkblood() {
System.out.println("BadVampire can menace.");
}
@Override
public void destroy() {
System.out.println("BadVampire can menace.");
}
@Override
public void menace() {
System.out.println("BadVampire can menace.");
}
@Override
public void kill() {
System.out.println("BadVampire can menace.");
}
}
class Dragon implements DangerousMonster{
@Override
public void destroy() {
System.out.println("Dragon can destroy.");
}
@Override
public void menace() {
System.out.println("Dragon can menace.");
}
}
public class HorroShow {
static void u(Monster x){x.menace();}
static void v(DangerousMonster x){
x.menace();
x.destroy();
}
static void w(Lethal x){x.kill();}
public static void main(String[] args){
DangerousMonster barney=new Dragon();
u(barney);
v(barney);
Vampire vlad=new BadVampire();
u(vlad);
v(vlad);
w(vlad);
}
}
run:
Dragon can menace.
Dragon can menace.
Dragon can destroy.
BadVampire can menace.
BadVampire can menace.
BadVampire can menace.
BadVampire can menace.
BUILD SUCCESSFUL (total time: 0 seconds)
三、适配接口
1.Randomdoubles.java
import java.util.*;
public class Randomdoubles {
private static Random rand=new Random(47);
public double next(){return rand.nextDouble();}
public static void main(String[] args){
Randomdoubles rd=new Randomdoubles();
for(int i=0;i<7;i++){
System.out.println(rd.next()+" ");
}
}
}
run:
0.7271157860730044
0.5309454508634242
0.16020656493302599
0.18847866977771732
0.5166020801268457
0.2678662084200585
0.2613610344283964
BUILD SUCCESSFUL (total time: 0 seconds)
2.Randomwords.java
import java.io.IOException;
import java.nio.*;
import java.util.*;
public class Randomwords implements Readable{
private static Random rand=new Random(47);
private static final char[] capitals="ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static final char[] lowers="abcdefghijklmnopqrstuvwxyz".toCharArray();
private static final char[] vowels="aeiou".toCharArray();
private int count;
public Randomwords(int count){this.count=count;}
@Override
public int read(CharBuffer cb) throws IOException {
if(count--==0){ return -1;}
cb.append(capitals[rand.nextInt(capitals.length)]);
for(int i=0;i<4;i++){
cb.append(lowers[rand.nextInt(lowers.length)]);
cb.append(vowels[rand.nextInt(vowels.length)]);
}
cb.append(" ");
return 10;
}
public static void main(String[] args){
Scanner s=new Scanner(new Randomwords(10));
while(s.hasNext()){
System.out.println(s.next());
}
}
}
run:
Ynobenogi
Foozutoqe
Gsegemuje
Roisueeue
Neueolome
Hlieehoca
Reeuebiki
Naieobuwa
Kjirokope
Wsupodoco
BUILD SUCCESSFUL (total time: 0 seconds)
3.AdaptedRandomDoubles.java
import java.io.IOException;
import java.nio.*;
import java.util.*;
public class AdaptedRandomDoubles extends Randomdoubles implements Readable{
private int count;
public AdaptedRandomDoubles(int count){this.count=count;}
@Override
public int read(CharBuffer cb) throws IOException {
if(count--==0){ return -1;}
String result=Double.toString(next())+" ";
cb.append(result);
return cb.length();
}
public static void main(String[] args){
Scanner s=new Scanner(new AdaptedRandomDoubles(7));
while(s.hasNext()){
System.out.println(s.nextDouble()+" ");
}
}
}
run:
0.7271157860730044
0.5309454508634242
0.16020656493302599
0.18847866977771732
0.5166020801268457
0.2678662084200585
0.2613610344283964
BUILD SUCCESSFUL (total time: 0 seconds)
四、嵌套接口
1.Factories.java
interface Service{
void method1();
void method2();
}
interface Servicefactories{
Service getservice();
}
class Implemention1 implements Service{
public void method1(){System.out.println("Implemention1 method1");}
public void method2(){System.out.println("Implemention1 method2");}
}
class Implemention2 implements Service{
public void method1(){System.out.println("Implemention2 method1");}
public void method2(){System.out.println("Implemention2 method2");}
}
class Implemention1Factoy implements Servicefactories{
public Service getservice(){
return new Implemention1();
}
}
class Implemention2Factoy implements Servicefactories{
public Service getservice(){
return new Implemention2();
}
}
public class Factories {
public static void servicecomsumer(Servicefactories f){
Service s=f.getservice();
s.method1();
s.method2();
}
public static void main(String[] args){
servicecomsumer(new Implemention1Factoy());
servicecomsumer(new Implemention2Factoy());
}
}
run:
Implemention1 method1
Implemention1 method2
Implemention2 method1
Implemention2 method2
BUILD SUCCESSFUL (total time: 0 seconds)
2.Games.java
interface Game{
boolean move();
}
interface GameFactor{
Game getgame();
}
class Checkers implements Game{
private int moves=0;
private static final int Moves=3;
public boolean move(){
System.out.println("Checkers move "+moves);
return ++moves!=Moves;
}
}
class CheckersFactory implements GameFactor{
public Game getgame(){return new Checkers();}
}
class Chess implements Game{
private int moves=0;
private static final int Moves=4;
public boolean move(){
System.out.println("Chess move "+moves);
return ++moves!=Moves;
}
}
class ChessFactory implements GameFactor{
public Game getgame(){return new Chess();}
}
public class Games {
public static void playgame(GameFactor f){
Game s=f.getgame();
while(s.move());
}
public static void main(String[] args){
playgame(new ChessFactory());
playgame(new CheckersFactory());
}
}
run:
Chess move 0
Chess move 1
Chess move 2
Chess move 3
Checkers move 0
Checkers move 1
Checkers move 2
BUILD SUCCESSFUL (total time: 0 seconds)
3. Nestinginterfaces.java
class A{
interface B{void f();}
public class Bimp implements B{
public void f(){System.out.println("A public class Bimp f()");}
}
private class Bimp2 implements B{
public void f(){System.out.println("A private class Bimp2 f()");}
}
interface C{void f();}
class Cimp implements C{
public void f(){System.out.println("A class Cimp f()");}
}
private class Cimp2 implements C{
public void f(){System.out.println("A private class Cimp2 f()");}
}
private interface D{void f();}
private class Dimp implements D{
public void f(){System.out.println("A private class Dimp f()");}
}
public class Dimp2 implements D{
public void f(){System.out.println("A public class Dimp2 f()");}
}
public D getD(){return new Dimp2();}
private D dref;
public void receiveD(D d){
dref=d;
dref.f();
}
}
interface E{
interface G{void f();}
public interface H{void f();}
void g();
}
public class Nestinginterfaces {
public class Bimp implements A.B{
@Override
public void f(){}
}
public class Cimp implements A.C{
@Override
public void f(){}
}
class Eimp implements E{
@Override
public void g() { }
}
class EGimp implements E.G{
public void f() {}
}
public static void main(String[] args){
A a=new A();
A a2=new A();
a2.receiveD(a.getD());
}
}
run:
A public class Dimp2 f()
BUILD SUCCESSFUL (total time: 0 seconds)
五、持有对象
1.AsListInference.java
import java.util.*;
class Snow{public void f(){System.out.print("Snow");}}
class Powder extends Snow{public void f(){System.out.print("Powder ");}}
class Crusty extends Snow{public void f(){System.out.print("Crusty ");}}
class Slush extends Snow{public void f(){System.out.print("Slush ");}}
class Light extends Snow{public void f(){System.out.print("Light ");}}
class Heavy extends Snow{public void f(){System.out.print("Heavy ");}}
public class AsListInference {
public static void main(String[] args){
List<Snow> s1=Arrays.asList(new Crusty(),new Slush(),new Powder());
System.out.print("s1: ");//s1
for(Snow s:s1)s.f();
System.out.println();
// s1.add(new Light());
// System.out.print("s1.add: ");//s1
// for(Snow s:s1)s.f();
// System.out.println();
List<Snow> s2=new ArrayList<Snow>();
Collections.addAll(s2, new Light(),new Heavy());
System.out.print("s2.addAll: ");//s2
for(Snow s:s2)s.f();
System.out.println();
// List<Snow> s3=new ArrayList<Snow>(s1);
// Snow[] sa=new Snow[s2.size()];
// Collections.addAll(s3, s2.toArray(sa));
// List<Snow> s4=Arrays.<Snow>asList(new Crusty(),new Slush(),new Powder());
//
// for(Snow s:s2)System.out.print(s+" ");
// System.out.println();
// for(Snow s:s3)System.out.print(s+" ");
// System.out.println();
// for(Snow s:s4)System.out.print(s+" ");
// System.out.println();
}
}
run:
s1: Crusty Slush Powder
s2.addAll: Light Heavy
BUILD SUCCESSFUL (total time: 0 seconds)
2.Print.java
import java.util.*;
public class Print {
public static void main(String[] args){
System.out.println(fill(new ArrayList<String>()));
System.out.println(fill(new LinkedList<String>()));
System.out.println(fill(new HashSet<String>()));
System.out.println(fill(new TreeSet<String>()));
System.out.println(fill(new LinkedHashSet<String>()));
System.out.println(fill(new HashMap<Integer,String>()));
System.out.println(fill(new TreeMap<Integer,String>()));
System.out.println(fill(new LinkedHashMap<Integer,String>()));
}
static Collection fill(Collection<String> cs){
cs.add("cat");
cs.add("dog");
cs.add("rat");
cs.add("cat");
return cs;
}
static Map fill(Map<Integer,String> map){
map.put(1,"dog");
map.put(4,"cat");
map.put(3,"rat");
map.put(3,"bird");
return map;
}
}
run:
[cat, dog, rat, cat]
[cat, dog, rat, cat]
[rat, cat, dog]
[cat, dog, rat]
[cat, dog, rat]
{1=dog, 3=bird, 4=cat}
{1=dog, 3=bird, 4=cat}
{1=dog, 4=cat, 3=bird}
BUILD SUCCESSFUL (total time: 0 seconds)
3.PriorityQueueTest.java
import java.util.*;
public class PriorityQueueTest {
public static void main(String[] args){
PriorityQueue<Integer> q=new PriorityQueue<Integer>();
Random r=new Random(47);
for(int i=0;i<10;i++){
q.offer(r.nextInt(i+10));
}
QueueTest.print(q);
List<Integer> l=Arrays.asList(23,31,45,54,65,78,46,65);
q=new PriorityQueue<Integer>(l);
QueueTest.print(q);
q=new PriorityQueue<Integer>(l.size(),Collections.reverseOrder());
q.addAll(l);
QueueTest.print(q);
List<String> list=new ArrayList<String>();
for(String s:"my name is huo yu ying".split(" ")){
list.add(s);
}
PriorityQueue<String> q1=new PriorityQueue<String>(list);
QueueTest.print(q1);
PriorityQueue<String> q2=new PriorityQueue<String>(list.size(),Collections.reverseOrder());
q2.addAll(list);
QueueTest.print(q2);
Set<Character> s=new HashSet<Character>();
for(Character c:"Huoyuying is beautiful girl".toCharArray()){
s.add(c);
}
PriorityQueue<Character> s1=new PriorityQueue<Character>(s);
QueueTest.print(s1);
PriorityQueue<Character> s2=new PriorityQueue<Character>(s.size(),Collections.reverseOrder());
s2.addAll(s);
QueueTest.print(s2);
}
}
run:
0 1 1 1 1 1 3 5 8 14
23 31 45 46 54 65 65 78
78 65 65 54 46 45 31 23
huo is my name ying yu
yu ying name my is huo
H a b e f g i l n o r s t u y
y u t s r o n l i g f e b a H
BUILD SUCCESSFUL (total time: 0 seconds)
4.QueueTest.java
import java.util.*;
public class QueueTest {
public static void main(String[] args){
Queue<Integer> queue=new LinkedList<Integer>();
Random r=new Random(47);
for(int i=0;i<10;i++){
queue.offer(r.nextInt(i+10));
}
print(queue);
Queue<Character> queue1=new LinkedList<Character>();
for(Character c:"Huoyuying".toCharArray()){
queue1.offer(c);
}
print(queue1);
}
public static void print(Queue q){
while(q.peek()!=null){
System.out.print(q.remove()+" ");
}
System.out.println();
}
}
run:
8 1 1 1 5 14 3 1 0 1
H u o y u y i n g
BUILD SUCCESSFUL (total time: 0 seconds)
5.SimpleCollection.java
import java.util.*;
public class SimpleColletion {
public static void main(String[] args){
System.out.println("========add single==========");
Collection<Integer> c=new ArrayList<Integer>();
for(int i=0;i<10;i++)
c.add(i);
for(Integer i:c)
System.out.print(i+" , ");
System.out.println();
System.out.println("========add groups==========");
Collection<Integer> c1=new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
Integer[] moreints={6,7,8,9,10};
c1.addAll(Arrays.asList(moreints));
Collections.addAll(c1, 11,12,13);
for(Integer i:c1)System.out.print(i+" , ");
System.out.println();
List<Integer> list=Arrays.asList(1,2,3,4,5);
list.set(1, 99);
for(Integer i:list)System.out.print(i+" , ");
System.out.println();
}
}
run:
========add single==========
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,
========add groups==========
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 ,
1 , 99 , 3 , 4 , 5 ,
BUILD SUCCESSFUL (total time: 0 seconds)
6.StackTest.java
import java.util.Stack;
public class StackTest {
public static void main(String[] args){
Stack<String> stack=new Stack<String>();
for(String s:"my name is huoyuying".split(" "))
stack.push(s);
while(!stack.empty())
System.out.print(stack.pop()+" ");
System.out.println();
}
}
run:
huoyuying is name my
BUILD SUCCESSFUL (total time: 0 seconds)
六、迭代器
1.ForeachTest.java
import java.util.*;
public class ForeachTest {
public static void main(String[] args){
Collection<String> cs=new LinkedList<String>();
Collections.addAll(cs, "my name is huoyuying".split(" "));
for(String s:cs)
System.out.print("'"+s+"' " );
System.out.println();
}
}
run:
'my' 'name' 'is' 'huoyuying'
BUILD SUCCESSFUL (total time: 0 seconds)
2. List1.java
import java.util.*;
class Snow{}
class Powder extends Snow{}
class Crusty extends Snow{}
class Slush extends Snow{}
class Light extends Snow{}
class Heavy extends Snow{}
public class List1 {
public static void main(String[] args){
List<Snow> s1=Arrays.asList(new Powder(),new Crusty(),new Slush());
Iterator<Snow> it=s1.iterator();
System.out.println("Method 1:");
while(it.hasNext()){
Snow s=it.next();
System.out.print(s+" ");
}
System.out.println();
System.out.println("Method 2:");
for(Snow ss:s1)
System.out.print(ss+" ");
System.out.println();
}
}
run:
Method 1:
Powder@15db9742 Crusty@6d06d69c Slush@7852e922
Method 2:
Powder@15db9742 Crusty@6d06d69c Slush@7852e922
BUILD SUCCESSFUL (total time: 0 seconds)
七、异常处理
例子:Exceptions.java
class SimpleException extends Exception{}
public class Exceptions {
public void f()throws SimpleException{
System.out.println("Throw SimpleException from f()");
throw new SimpleException();
}
public static void main(String[] args){
Exceptions ted=new Exceptions();
try{
ted.f();
}catch(SimpleException e){
System.out.println("catch SimpleException from f()");
}
}
}
run:
Throw SimpleException from f()
catch SimpleException from f()
BUILD SUCCESSFUL (total time: 0 seconds)
八、String
1.SimpleFormat.java
public class SimpleFormat {
public static void main(String[] args){
int x=6;
double y=5.434553;
System.out.println("Row 1:[ "+x+" "+y+"]");
System.out.format("Row 1:[ %d %f]
", x,y);
System.out.printf("Row 1:[ %d %f]
", x,y);
}
}
run:
Row 1:[ 6 5.434553]
Row 1:[ 6 5.434553]
Row 1:[ 6 5.434553]
BUILD SUCCESSFUL (total time: 0 seconds)
2.Turtle.java
import java.io.*;
import java.util.*;
public class Turtle {
private String name;
private Formatter f;
public Turtle(String name,Formatter f){
this.name=name;
this.f=f;
}
public void move(int x,int y){
f.format("%s the turtle is at (%d,%d)
",name,x,y);
}
public static void main(String[] args){
PrintStream out=System.out;
Turtle Sam=new Turtle("Sam",new Formatter(out));
Turtle Holly=new Turtle("Holly",new Formatter(out));
Sam.move(3, 5);
Holly.move(4, 5);
}
}
run:
Sam the turtle is at (3,5)
Holly the turtle is at (4,5)
BUILD SUCCESSFUL (total time: 0 seconds)
3.UsingStringBuilder.java
import java.util.*;
public class UsingStringBuilder {
public static Random r=new Random(47);
public String toString(){
StringBuilder result=new StringBuilder("[");
for(int i=0;i<25;i++){
result.append(r.nextInt(100));
result.append(",");
}
result.delete(result.length()-1, result.length());
result.append("]");
return result.toString();
}
public static void main(String[] args){
UsingStringBuilder u=new UsingStringBuilder();
System.out.println(u);
}
}
run:
[58,55,93,61,61,29,68,0,22,7,88,28,51,89,9,78,98,61,20,58,16,40,11,22,4]
BUILD SUCCESSFUL (total time: 0 seconds)
九、动态代理
1.Demo.java
interface A{
public void method1();
public void method2(String s);
}
class RealObject implements A{
public void method1() {
System.out.println("RealObject.method1()");
}
public void method2(String s) {
System.out.println("RealObject.method2(): "+s);
}
}
class SimpleProxy implements A{
private A a;
public SimpleProxy(A a){this.a=a;}
public void method1() {
System.out.println("SimpleProxy.method1()");
a.method1();
}
public void method2(String s) {
System.out.println("SimpleProxy.method2()");
a.method2(s);
}
}
public class Demo {
public static void Show(A a){
a.method1();
a.method2("hello");
}
public static void main(String[] args){
Show(new RealObject());
System.out.println("=======================");
Show(new SimpleProxy(new RealObject()));
}
}
run:
RealObject.method1()
RealObject.method2(): hello
=======================
SimpleProxy.method1()
RealObject.method1()
SimpleProxy.method2()
RealObject.method2(): hello
BUILD SUCCESSFUL (total time: 0 seconds)
2.SimpleDynamicProxy.java
import java.lang.reflect.*;
public class SimpleDynamicProxy implements InvocationHandler {
private Object proxied;
public SimpleDynamicProxy(Object proxied){this.proxied=proxied;}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("===proxy:"+proxy.getClass()+" method: "+method+" args: "+args);
if(args!=null)
for(Object arg:args)System.out.println(" "+arg);
return method.invoke(proxied, args);
}
public static void main(String[] args){
}
}
run:
BUILD SUCCESSFUL (total time: 0 seconds)
十、
(未完待续,感谢支持♥♥♥)