When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions

As mentioned in the section  Nested Classes, nested classes enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code. Local classes, anonymous classes, and lambda expressions also impart these advantages; however, they are intended to be used for more specific situations:

?Local class: Use it if you need to create more than one instance of a class, access its constructor, or introduce a new, named type (because, for example, you need to invoke additional methods later).

?Anonymous class: Use it if you need to declare fields or additional methods.

?Lambda expression:

?Use it if you are encapsulating a single unit of behavior that you want to pass to other code. For example, you would use a lambda expression if you want a certain action performed on each element of a collection, when a process is completed, or when a process encounters an error.

?Use it if you need a simple instance of a functional interface and none of the preceding criteria apply (for example, you do not need a constructor, a named type, fields, or additional methods).

?Nested class: Use it if your requirements are similar to those of a local class, you want to make the type more widely available, and you don‘t require access to local variables or method parameters.

?Use a non-static nested class (or inner class) if you require access to an enclosing instance‘s non-public fields and methods. Use a static nested class if you don‘t require this access.

时间: 2024-10-11 18:32:07

When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions的相关文章

JAVA匿名内部类(Anonymous Classes)

匿名内部类在我们JAVA程序员的日常工作中经常要用到,但是很多时候也只是照本宣科地用,虽然也在用,但往往忽略了以下几点:为什么能这么用?匿名内部类的语法是怎样的?有哪些限制?因此,最近,我在完成了手头的开发任务后,查阅了一下JAVA官方文档,将匿名内部类的使用进行了一下总结,案例也摘自官方文档.感兴趣的可以查阅官方文档(https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html). 2.匿名内部类 匿名内部类

Starting Threads and Using Anonymous Classes

As we all know,a thread is a separate process on your computer.you can run multiple threads all at the same time. multi-threaded code has the disadvantage of becoming quite complex very quickly,although java has some great classes for dealing with mu

500 OOPS: vsftpd: both local and anonymous access

配置ftp服务器,有如下报错 C:\netos74\bin>ftp 10.20.100.252Connected to 10.20.100.252.500 OOPS: vsftpd: both local and anonymous access disabled!Connection closed by remote host. 解决办法 [[email protected] etc]# vi /etc/vsftpd/vsftpd.conf //修改vsftpd.conf文件 local_en

java基础之 Advanced Class Design

 java基础之 Advanced Class Design Abstract Classes In many programming situations, you want to specify an abstraction without specifying implementation-level details. In such cases, you can use either abstract classes or interfaces. Abstract classes are

java内部类学习(二)

Nested Classes The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here: class OuterClass { ... class NestedClass { ... } } Terminology: Nested classes are divided

关于符号Symbol2

来看一下继承自Symbol的具体实现类. 1.TypeSymbol /** A class for type symbols. * Type variables are represented by instances of this class, // 类型变量用这个类来表示 * classes and packages by instances of subclasses. // 类和包用子类来表示 */ public class TypeSymbol extends Symbol impl

Jetpack核心组件,ViewModel的使用及原理解析

前言 ViewModel旨在以生命周期意识的方式存储和管理用户界面相关的数据,它可以用来管理Activity和Fragment中的数据.还可以拿来处理Fragment与Fragment之间的通信等等. 当Activity或者Fragment创建了关联的ViewModel,那么该Activity或Fragment只要处于活动状态,那么该ViewModel就不会被销毁,即使是该Activity屏幕旋转时重建了.所以也可以拿来做数据的暂存. ViewModel主要是拿来获取或者保留Activity/F

2.1.2.Architecture components_ViewModel

参考 https://developer.android.com/topic/libraries/architecture/viewmodel 官方例子 https://github.com/android/architecture-components-samples/tree/master/BasicSample ViewModel ViewModel类旨在以生命周期感知的方式存储和管理与UI相关的数据. ViewModel类允许数据幸免于配置更改(例如屏幕旋转). ViewModel其实算

(转)C#中的委托,匿名方法和Lambda表达式

简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个First不仅被编译,并都获得正确答案,且他们的结果一样.如果你对此感到困惑,那么请继续看这篇文章. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Customer {     public int ID { get; set; }     p