标签:ati param rri vat rac eth run generate oid
1 package InnerClass; 2 3 4 5 /** 6 * 匿名内部类 7 * @author SiyyaWu 8 */ 9 public class AnonymousInnerClass { 10 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 } 14 15 16 /** 17 * 匿名内部类简化写法 18 * @author SiyyaWu 19 * @param x 20 * @return 21 */ 22 public Contents1 contents(final int x){ 23 return new Contents1(){ 24 public int value(){ 25 return super.value()*x; 26 } 27 @Override 28 public void run() { 29 // TODO Auto-generated method stub 30 System.out.println("抽象方法"); 31 } 32 }; 33 } 34 35 /** 36 * 匿名内部类完整写法 37 * @author SiyyaWu 38 */ 39 public class MyContents extends Contents1{ 40 private int x; 41 public MyContents(int x){ 42 this.x=x; 43 } 44 public int value(){ 45 return super.value()*x; 46 } 47 @Override 48 public void run() { 49 // TODO Auto-generated method stub 50 System.out.println("抽象方法"); 51 } 52 } 53 54 public Contents1 contentsAll(int x){ 55 return new MyContents(x); 56 } 57 58 /** 59 * 匿名内部类正常写法 60 * @author SiyyaWu 61 */ 62 class Contents2Impl implements Contents2{ 63 64 @Override 65 public int value() { 66 // TODO Auto-generated method stub 67 return 5; 68 } 69 } 70 public Contents2 contents2All(){ 71 return new Contents2Impl(); 72 } 73 74 /** 75 * 匿名内部类的简化写法 76 * @author SiyyaWu 77 * @return 78 */ 79 public Contents2 contents2(){ 80 return new Contents2() { 81 82 @Override 83 public int value() { 84 // TODO Auto-generated method stub 85 return 5; 86 } 87 88 }; 89 } 90 } 91 92 /** 93 * 抽象类 94 * @author SiyyaWu 95 */ 96 abstract class Contents1{ 97 public int i=2; 98 public int value(){ 99 return i; 100 } 101 public abstract void run(); 102 } 103 104 105 /** 106 * 接口 107 * @author SiyyaWu 108 */ 109 interface Contents2{ 110 public int value(); 111 }
标签:ati param rri vat rac eth run generate oid
原文地址:https://www.cnblogs.com/siyyawu/p/11013828.html