单体等级Vs密封等级Vs静态等级 [英] Singleton Class Vs Sealed Class Vs Static Class

查看:109
本文介绍了单体等级Vs密封等级Vs静态等级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑任何人都可以告诉我这三个单词之间的确切差异答案是否足够





谢谢advance

i am getting confusion can any one tell me what is exact difference between these three one word answer is enough


Thanks in advance

推荐答案

Signleton不是语言类或类特定属性。它是一种设计模式,通过它可以在整个应用程序生命周期内维护类对象的对象生命周期。

密封类是一个无法扩展/继承的类。

Static Class是一个类,你需要在该类下创建所有成员应该是静态的。您无法创建该类的实例。

根据其特征,您可以根据问题上下文声明/创建该类型。
Signleton is not a language class or class specific attribute. It is a design pattern by which can can maintain a object lifetime of a class object for the whole application life.
Sealed class is a class that you can not extended/inherit.
Static Class is a class you need to create all member under that class should be static. You can not create instance of that class.
The based on their characteristics, you can declare/create that type of class based on your problem context.


< b> Singleton 是设计模式,在这种设计模式中你将构造函数设为私有,这样它的对象就不能直接声明,你需要创建类的实例而不是你可以使用它。



Singleton is Design Pattern in which you make the constructor private so that its object cannot be declare directly, you will need to create Instance of the class and than you can use it.

public class Singleton {
   private static final Singleton instance;   
 
   private Singleton(){}
 
   public static Singleton getInstance() {
     if (instance == null)
       instance = new Singleton();
     return instance;
   }
 }










public Singleton singleton = Singleton.getInstance();







密封类:

只需通过这些积分就可以了解密封课程



1.为安全原因限制继承的类被声明,密封类。

2.密封类是层次结构中的最后一类。

3.密封类可以是派生类但不能是基类。

4.密封类也不能是抽象类。因为抽象类必须提供功能,所以我们限制它继承。



静态类:

静态类用于项目中有一些实用程序库,你可以创建公共类,可以设置为静态类

静态类只包含静态成员。

静态类无法实例化。它们不能包含实例构造函数




Sealed Class:
Just go through this points and you will get idea of sealed class

1. A class, which restricts inheritance for security reason is declared, sealed class.
2. Sealed class is the last class in the hierarchy.
3. Sealed class can be a derived class but can''t be a base class.
4. A sealed class cannot also be an abstract class. Because abstract class has to provide functionality and here we are restricting it to inherit.

Static Class:
Static class is used where you have some utilities lib in you project and you can create common class and can set as Static class
Static classes only contain static members.
Static classes can not be instantiated. They cannot contain Instance Constructors


这篇关于单体等级Vs密封等级Vs静态等级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆