🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.pl.comp.lang.csharp
Article #9624

Re: Zas?oni?cie metody (nie przes?oni?cie)

#9624
From: Paweł
Date: Thu, 27 Oct 2005 18:14
66 lines
2291 bytes
maluman wrote:

> Witam
> 
> Mam pytanko teoretyczne
> 
> Jest klasa bazowa, która implementuje metodę publiczną. Czy jest
> możliwość zablokowania dostępu do tej metody w klasie potomnej?
> 
> Aby to nieco upraktycznić:
> 
> Mam własny MessageBoxPL, który dziedziczy z Form (co za tym idzie z
> Control). Wśród jego metod jest bezparametrowa metoda dziedziczona:
> Control.Show(). Ja nie chcę, aby była mozliwość wywoływania tej
> metody bez parametrów i chciałbym ją przesłonić.
> Niestety zdeklarowanie 'private void Show()' w klasie MessageBoxPL nic nie
> daje. Czy jest na to jakiś sposób? Oczywiście można z tym żyć,
> dlatego pytanie jest bardziej teoretyczne.

$ cat tmp.cs
using System;

public class A
{
    public virtual void foo()
    {
        Console.WriteLine("A::foo()");
    }
}

public class B : A
{
    [Obsolete("You shouldn't use this method anymore.")]
    public new void foo()
    {
        throw new Exception();
    }
    public void foo(int arg)
    {
    }
}

public class tmp
{
    public static void Main()
    {
        B b = new B();
        b.foo();
    }
}


$ mcs -warn:4 tmp.cs
tmp.cs(28,4): warning CS0618: `B.foo()' is obsolete:
                              `You shouldn't use this method anymore.'
Compilation succeeded - 1 warning(s)

$ mono tmp.exe

Unhandled Exception: System.Exception: Exception of type System.Exception
was thrown.
in <0x0001d> B:foo ()
in <0x00024> tmp:Main ()


o takie cos ci chodzi, czy oczekujesz bledu na etapie kompilacji?
Technical Details
Message-ID: <djr5e7$5m9$1@achot.icm.edu.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news2.icm.edu.pl!not-for-mail
References: <djqepb$dqb$1@nemesis.news.tpi.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news2.icm.edu.pl!not-for-mail
From: Paweł <root@[127.0.0.1]>
Newsgroups: alt.pl.comp.lang.csharp
Subject: Re: Zas?oni?cie metody (nie przes?oni?cie)
Date: Thu, 27 Oct 2005 20:14:30 +0200
Organization: http://news.icm.edu.pl/
Lines: 67
Message-ID: <djr5e7$5m9$1@achot.icm.edu.pl>
References: <djqepb$dqb$1@nemesis.news.tpi.pl>
NNTP-Posting-Host: chello062179052145.chello.pl
Mime-Version: 1.0
X-Trace: achot.icm.edu.pl 1130436871 5833 62.179.52.145 (27 Oct 2005 18:14:31 GMT)
X-Complaints-To: abuse@icm.edu.pl
NNTP-Posting-Date: Thu, 27 Oct 2005 18:14:31 +0000 (UTC)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit