zoukankan      html  css  js  c++  java
  • java匿名类

    匿名内部类也就是没有名字的内部类

    正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写

    但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口

    实例1:不使用匿名内部类来实现抽象方法

    abstract class Person {
    	public abstract void eat();
    }
    
    class Child extends Person {
    	public void eat() {
    		System.out.println("eat something");
    	}
    }
    
    public class Demo {
    	public static void main(String[] args) {
    		Person p = new Child();
    		p.eat();
    	}
    }
    

    运行结果: eat something

    可以看到,我们用Child继承了Person类,然后实现了Child的一个实例,将其向上转型为Person类的引用

    但是,如果此处的Child类只使用一次,那么将其编写为独立的一个类岂不是很麻烦?

    这个时候就引入了匿名内部类

    实例2:匿名内部类的基本实现

    abstract class Person {
    	public abstract void eat();
    }
    
    public class Demo {
    	public static void main(String[] args) {
    		Person p = new Person() {
    			public void eat() {
    				System.out.println("eat something");
    			}
    		};
    		p.eat();
    	}
    }
    

    运行结果: eat something

    可以看到,我们直接将抽象类Person中的方法在大括号中实现了

    这样便可以省略一个类的书写

    并且,匿名内部类还能用于接口上

     

    实例3:在接口上使用匿名内部类

    interface Person {
    	public void eat();
    }
    
    public class Demo {
    	public static void main(String[] args) {
    		Person p = new Person() {
    			public void eat() {
    				System.out.println("eat something");
    			}
    		};
    		p.eat();
    	}
    }
    

    运行结果: eat something

    由上面的例子可以看出,只要一个类是抽象的或是一个接口,那么其子类中的方法都可以使用匿名内部类来实现

    最常用的情况就是在多线程的实现上,因为要实现多线程必须继承Thread类或是继承Runnable接口

    实例4:Thread类的匿名内部类实现

    public class Demo {
    	public static void main(String[] args) {
    		Thread t = new Thread() {
    			public void run() {
    				for (int i = 1; i <= 5; i++) {
    					System.out.print(i + " ");
    				}
    			}
    		};
    		t.start();
    	}
    }
    

    运行结果: 1 2 3 4 5

    实例5:Runnable接口的匿名内部类实现

    public class Demo {
    	public static void main(String[] args) {
    		Runnable r = new Runnable() {
    			public void run() {
    				for (int i = 1; i <= 5; i++) {
    					System.out.print(i + " ");
    				}
    			}
    		};
    		Thread t = new Thread(r);
    		t.start();
    	}
    }
    

    运行结果: 1 2 3 4 5

    接受匿名类的方法

    package com;
    
    public class Demo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            test1 t2=new test1();
    
             t2.say(
                     new Itest(){
                         
                         public void say(){
                             System.out.println("this is 匿名类");
                            }
                         
                     }
                     );
    
        }
        
    
    
    }
    
    
      interface Itest{
        
        void say();
        
    }
    
    
    class test1{
        
        void  say(Itest t){
                
             t.say();
        }
        
    }

     输出结果:this is 匿名类

  • 相关阅读:
    IntelliJ IDEA 最新注册码
    tidyverse|数据分析常规操作-分组汇总(sumamrise+group_by)
    ComplexHeatmap|根据excel表绘制突变景观图(oncoplot)
    Tidyverse| XX_join :多个数据表(文件)之间的各种连接
    LDheatmap | SNP连锁不平衡图(LD)可视化,自己数据实现版!
    Tidyverse|数据列的分分合合,爱恨情仇
    R-ggpmisc|回归曲线添加回归方程,R2,方差表,香不香?
    R-rbind.fill|列数不一致的多个数据集“智能”合并,Get!
    R|tableone 快速绘制文章“表一”-基线特征三线表
    R|生存分析
  • 原文地址:https://www.cnblogs.com/tiancai/p/6971640.html
Copyright © 2011-2022 走看看