zoukankan      html  css  js  c++  java
  • 匿名实现类&匿名对象

    学习过程中经常搞不清匿名类&匿名对象怎么用,今天就把常用的方式总结一遍。

    1.创建了非匿名实现类的非匿名对象
     1 //定义USB接口
     2 interface USB{
     3     void inputInofo();
     4     void outputInfo();
     5 }
     6 //USB接口的实现类
     7 class Computer implements USB{
     8 
     9     public void inputInofo() {
    10         System.out.println("MyComputer输入信息[^L^]。。。。。。");
    11     }
    12 
    13     public void outputInfo() {
    14         System.out.println("MyComputer输出信息[^_^]。。。。。。");
    15     }
    16 }
    17 
    18 public class mainTest {
    19     @Test
    20     public void show(){
    21         //1.创建了非匿名实现类的非匿名对象(有实现类名,有对象名)
    22         Computer computer = new Computer(); // ==> USB usbImpl = new Computer();
    23         streamData(computer);
    24     }
    25 
    26     public void streamData(USB usbImpl){
    27         usbImpl.inputInofo();
    28         usbImpl.outputInfo();
    29     }
    30 }
    2.创建了非匿名实现类的匿名对象
     1 //定义USB接口
     2 interface USB{
     3     void inputInofo();
     4     void outputInfo();
     5 }
     6 //USB接口的实现类
     7 class Computer implements USB{
     8 
     9     public void inputInofo() {
    10         System.out.println("MyComputer输入信息[^L^]。。。。。。");
    11     }
    12 
    13     public void outputInfo() {
    14         System.out.println("MyComputer输出信息[^_^]。。。。。。");
    15     }
    16 }
    17 
    18 public class mainTest {
    19     @Test
    20     public void show(){
    21         //2.创建了非匿名实现类的匿名对象(有实现类名,没有对象名),通常作为参数,不用定义变量名了
    22         streamData(new Computer());
    23     }
    24 
    25     public void streamData(USB usbImpl){
    26         usbImpl.inputInofo();
    27         usbImpl.outputInfo();
    28     }
    29 }
    3.创建了匿名实现类的非匿名对象
     1 //定义USB接口
     2 interface USB{
     3     void inputInofo();
     4     void outputInfo();
     5 }
     6 //USB接口的实现类
     7 class Computer implements USB{
     8 
     9     public void inputInofo() {
    10         System.out.println("MyComputer输入信息[^L^]。。。。。。");
    11     }
    12 
    13     public void outputInfo() {
    14         System.out.println("MyComputer输出信息[^_^]。。。。。。");
    15     }
    16 }
    17 
    18 public class mainTest {
    19     @Test
    20     public void show(){
    21         //3.创建了匿名实现类的非匿名对象(没有实现类名,有对象名)
    22         USB usbImpl = new USB() {
    23             public void inputInofo() {
    24                 System.out.println("[匿名实现类,非匿名对象]输入。。。。");
    25             }
    26 
    27             public void outputInfo() {
    28                 System.out.println("[匿名实现类,非匿名对象]输出。。。。");
    29             }
    30         };
    31 
    32         streamData(usbImpl);
    33     }
    34 
    35     public void streamData(USB usbImpl){
    36         usbImpl.inputInofo();
    37         usbImpl.outputInfo();
    38     }
    39 }
    4.创建了匿名实现类的匿名对象
     1 //定义USB接口
     2 interface USB{
     3     void inputInofo();
     4     void outputInfo();
     5 }
     6 //USB接口的实现类
     7 class Computer implements USB{
     8 
     9     public void inputInofo() {
    10         System.out.println("MyComputer输入信息[^L^]。。。。。。");
    11     }
    12 
    13     public void outputInfo() {
    14         System.out.println("MyComputer输出信息[^_^]。。。。。。");
    15     }
    16 }
    17 
    18 public class mainTest {
    19     @Test
    20     public void show(){
    21         //4.创建了匿名实现类的匿名对象(没有实现类名,没有对象名),通常作为参数,不用定义变量名了
    22         streamData(
    23                 new USB()
    24                 {
    25                     public void inputInofo() {
    26                         System.out.println("[匿名实现类,匿名对象]输入。。。。");
    27                     }
    28 
    29                     public void outputInfo() {
    30                         System.out.println("[匿名实现类,匿名对象]输出。。。。");
    31                     }
    32                 }
    33         );
    34     }
    35 
    36     public void streamData(USB usbImpl){
    37         usbImpl.inputInofo();
    38         usbImpl.outputInfo();
    39     }
    40 }
  • 相关阅读:
    最大流最小割——bzoj1001狼抓兔子,洛谷P2598
    求最小公因数和最大公倍数
    归并排序
    Splay
    Tarjan判断为什么不能把dfn写成low
    2-SAT问题
    离散数学-传递闭包(POJ3275)
    POJ3190
    安装环境gcc;
    二分折半排序
  • 原文地址:https://www.cnblogs.com/zyb-mini/p/anonymousClass.html
Copyright © 2011-2022 走看看