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 }
  • 相关阅读:
    你的行为合理吗?看看社会心理学给我们的启示。
    如何在Firefox方便的抓取网页上的Flash
    Disable anchor tag的javascript代码(兼容IE和Firefox)
    如何更改DotNetNuke(DNN)的默认语言
    The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
    一寸、两寸证件照photoshop批处理动作
    DotNetNuke(DNN)皮肤制作如何居中内容
    忘记DotNetNuke的host密码?如何恢复丢失的DNN密码
    使用DIIOP必须作的DOMINO手动设置
    Domino中Servlet实现RSS中文乱码问题解决
  • 原文地址:https://www.cnblogs.com/zyb-mini/p/anonymousClass.html
Copyright © 2011-2022 走看看