密封类 [英] Sealed classes

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

问题描述

请帮助我解决此问题:

如果要声明一个密封的类,则意味着我不能继承它.
但是可能会出现密封类包含带有公共访问说明符的函数的情况.
我只想知道此功能是否可以继承?????
如果不是,那么需要将其声明为public,如果是,那么又有可能吗?

Kindly help me to solve it out this problem:

If am declaring a sealed class,it means that I cannot inherit it.
But there may be a case that a sealed class is containing a function with public access specifier.
I just want to know that this function can be inherited or not?????
If not then what is the need to declare it as public and if yes then how is it possible?

推荐答案

无法从密封类派生,但是如果它也是使用公共方法public可以使用该类的实例通过任何代码调用它们.
Sealed classes cannot be derived from, but if it is also public with public methods they can be called by any code using an instance of that class.


您不能继承"单个方法.就像戴维说的那样,如果它是公共的,则可以从类外部访问它.
You can''t "inherit" individual methods. Like Davey said, if it''s public, you can access it from outside the class.


sweety2010写道:
sweety2010 wrote:

如果要声明一个密封的类,则意味着我不能继承它.

If am declaring a sealed class,it means that i cannot inherit it.



这是100%真实的



which is 100% true

sweety2010写道:
sweety2010 wrote:

一个密封的类包含一个带有公共访问说明符的函数.

a sealed class is containing a function with public access specifier.





sealed class radix
    {
        public void fun()
        {
            Console.WriteLine("Hello");
        }
    }



好吧...



OK now...

sweety2010写道:
sweety2010 wrote:

我只想知道此函数是否可以继承????

I just want to know that this function can be inherited or not?????



没有密封的类,并且fun()函数位于密封的类基数内,因此您无法继承该类.



No the class is sealed and the function fun() is inside the sealed class radix so u cannot Inherit that

sweety2010写道:
sweety2010 wrote:

如果是,那我怎么可能?

if yes then how is ii possible?



是的,有可能借助下面给出的代码



Yes it is possible with the help of the code given below

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    sealed class radix
    {
        public void fun()
        {
            Console.WriteLine("Hello");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            radix obj = new radix();
            obj.fun();
        }
    }
}



通过创建密封类Radix的对象,任何继承基数的尝试都将导致错误,并且编译器将不允许它.

一旦找到有用的答案,就对我的答案进行评分
谢谢&问候
基数:)



By creating the object of the sealed class Radix any attempt to inherit radix will cause an error and the compiler will not allow it.

Do rate my answer once you find it useful
Thanks & Regards
Radix :)


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

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