java中匿名类_Java中的匿名类

java中匿名类

Anonymous class in Java is a class without name. Its name is decided by the compiler. It enables us to declare and instantiate the class at the same time. It is like local class except that it does not have any name. We can use it only once.

Anonymous class is also known as anonymous inner class in java. It is called anonymous inner class because it is defined inside another class.

How to Create Anonymous Class in Java

Anonymous class can be created in following two ways.

1. Using Class
2. Using Interface

Java Anonymous Class Example Using Class


Output

Anonymous Class in Java

When the above code is compiled, an anonymous class is created and its name is decided by the compiler. This anonymous class extends Parent class and overrides show() method.

In above example an object of anonymous class is created and its reference is stored in reference variable p of Parent class.

I have decompiled the .class file generated by compiler for the anonymous class. It contains the following code.

Java Anonymous Class Example Using Interface

interface Parent
{
 void show();
}
 
class Demo
{
 public static void main(String...s)
 {
  //creating anonymous inner class
  Parent p=new Parent() {
   public void show()
   {
    System.out.println("Anonymous Class");
   }
  };
 
  p.show();
 }
}


When the above code is compiled, an anonymous class is created and its name is decided by the compiler. This anonymous class implements Parent interface and overrides show() method.

In above example an object of anonymous class is created and its reference is stored in reference variable p of Parent interface.

I have decompiled the .class file generated by compiler for the anonymous class. It contains the following code.

What is the purpose of anonymous class in Java?

You may be thinking that above work can also be done by creating a separate class and then extending Parent class. Why we have created anonymous class? Actually it is quicker to create an anonymous class then creating a separate class. Anonymous inner class is useful when we want to override a small amount of functionality (like one method) of parent class. It also makes our code more concise.

If you find anything missing or incorrect in above tutorial then please mention it by commenting below. 

Anonymous class in Java is a class without name. Its name is decided by the compiler. It enables us to declare and instantiate the class at the same time. It is like local class except that it does not have any name. We can use it only once.

Anonymous class is also known as anonymous inner class in java. It is called anonymous inner class because it is defined inside another class.

How to Create Anonymous Class in Java

Anonymous class can be created in following two ways.

1. Using Class
2. Using Interface

Java Anonymous Class Example Using Class


Output

Anonymous Class in Java

When the above code is compiled, an anonymous class is created and its name is decided by the compiler. This anonymous class extends Parent class and overrides show() method.

In above example an object of anonymous class is created and its reference is stored in reference variable p of Parent class.

I have decompiled the .class file generated by compiler for the anonymous class. It contains the following code.

Java Anonymous Class Example Using Interface

interface Parent
 {
  void show();
 }
  
 class Demo
 {
  public static void main(String...s)
  {
   //creating anonymous inner class
   Parent p=new Parent() {
    public void show()
    {
     System.out.println("Anonymous Class");
    }
   };
  
   p.show();
  }
 }


When the above code is compiled, an anonymous class is created and its name is decided by the compiler. This anonymous class implements Parent interface and overrides show() method.

In above example an object of anonymous class is created and its reference is stored in reference variable p of Parent interface.

I have decompiled the .class file generated by compiler for the anonymous class. It contains the following code.

What is the purpose of anonymous class in Java?

You may be thinking that above work can also be done by creating a separate class and then extending Parent class. Why we have created anonymous class? Actually it is quicker to create an anonymous class then creating a separate class. Anonymous inner class is useful when we want to override a small amount of functionality (like one method) of parent class. It also makes our code more concise.

If you find anything missing or incorrect in above tutorial then please mention it by commenting below. 

翻译自: https://www.thecrazyprogrammer.com/2015/07/anonymous-class-in-java.html

java中匿名类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值