带有示例的黑白私人班级与密封班级的区别 [英] Difference b/w private class vs sealed class with example

查看:98
本文介绍了带有示例的黑白私人班级与密封班级的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



谁能告诉我有关示例的区别黑白私有类与密封类.

Hi,

Can anybody tell me about the Difference b/w private class vs sealed class with example.

推荐答案

私有 :私有limits the visiblity to a scope.在一个类中声明一个私有类意味着从该类的外部看到"sub-class" can''t.
方法和属性也是如此-它们可以在类中看到,但对任何使用者或继承者都不可见.
Private关键字用于声明类.

密封的:如果将一个类声明为密封的,则表示you cannot inherit from the class.当某个类在库,该类或该类的操作的内部是内部的,而您不希望该类被覆盖(因为它可能影响功能)时,可以使用密封类.
Sealed 关键字用于声明类

请参考:
密封类和私有类之间的区别 [密封阶级与私人阶级之间的区别 [
Private: Private limits the visiblity to a scope. Declaring a private class within a class means that "sub-class" can''t be seen from outside of the class.
This is also true for methods and properties - they can be seen within the class, but not to any consumers or inheritors.
Private keyword is used for declaring class.

Sealed: If a class is declared as sealed, that means that you cannot inherit from the class. Sealed class can be used when a class is internal to the operation of the library, class or whwn you donot want that class to be overridden because it may affect the functionality.
Sealed keyword is used for declaring class

Pleae refer:
diff between sealed class and private class[^]
difference between Sealed Class and Private Class[^]


我给您链接,希望对您有所帮助

C#调整–为什么在类? [公共抽象密封类MyFundooClass {} [ C#.NET中的面向对象编程-第4部分 [ ^ ]

私人的:只有班级成员可以访问.但可以在任何类中继承.
如果在C#中将类定义为密封",在VB.NET中将类定义为不可继承",则不能
进一步继承该类.

现在,我将通过简单的示例来更清楚

i give you link i hope it will help you

C# Tweaks – Why to use the sealed keyword on classes?[^]

public abstract sealed class MyFundooClass {}[^]

Object-Oriented Programming in C# .NET - Part 4[^]

Private : Only members of class have access. but it can be inherit in any class.
If you define a class as "Sealed" in C# and "NotInheritable" in VB.NET you can not
inherit the class any further.

now i will more clear with simple example

private class abc // private class it
   {
       void abc()
       {
       }
       public  void show()
       {
           string abc = "";
       }
   }

   sealed class efg // Sealed class it
   {
       void efg()
       {
       }
       public void showefg()
       {
           string abc = "";
       }
   }

   public class cde : abc // in this class we inherit private class abc
   {
       void cde()
       {
           show(); // private class method show
       }
   }

   public class ghi : efg // we cannot inherit sealed class and it method
   {
       void ghi()
       {
           // not showing sealed class method  showefg() even it is public
       }
   }


protected void Page_Load(object sender, EventArgs e)
    {
        cde c = new cde();// calling private class method by inheritance
        c.show();
   }


私有类只能由其定义并包含在内部的类访问-外部类完全无法访问.
Sealed类可以由任何类访问,但不能派生自该类.
A Private class can only be accessed by the class it is defined and contain within - it is completely inaccessible to outside classes.
A Sealed class can be accessed by any class, but can not be derived from.
public class A
  {
  private class B
     {
     }
  B b = new B();
  }
public class C
  {
  A.B b = new A.B(); // ERROR
  }





public sealed class A
   {
   }
public class B : A //ERROR
   {
   }


这篇关于带有示例的黑白私人班级与密封班级的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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