컴파일 오류 : 인터페이스를 명시 적으로 구현하는 동안 "수정 자 'public'이이 항목에 유효하지 않습니다."
.NET Framework public
를 명시 적으로 구현하기 위해 클래스에 메서드 를 만드는 동안이 오류가 발생 합니다 interface
. 해결 PrintName
방법이 있습니다. 메서드 의 명시 적 구현을 제거하는 것입니다 . 그러나 나는이 오류가 발생하는 이유에 놀랐습니다.
누구든지 오류를 설명 할 수 있습니까?
라이브러리 코드 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test.Lib1
{
public class Customer : i1
{
public string i1.PrintName() //Error Here...
{
return this.GetType().Name + " called from interface i1";
}
}
public interface i1
{
string PrintName();
}
interface i2
{
string PrintName();
}
}
콘솔 테스트 애플리케이션 용 코드 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Test.Lib1;
namespace ca1.Test
{
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer();
Console.WriteLine(customer.PrintName());
//i1 i1o = new Customer();
//Console.WriteLine(i1o.printname());
//i2 i2o = new Customer();
//Console.WriteLine(i2o.printname());
}
}
}
인터페이스의 명시 적 구현을 사용할 때 멤버는 클래스 자체에서 private 보다 더 제한적인 것으로 강제 됩니다. 그리고 액세스 수정자가 강제되면 추가 할 수 없습니다.
Likewise, in the interface itself, all members are public. If you try to add a modifier inside an interface you will get a similar error.
Why are explicit members (very) private? Consider:
interface I1 { void M(); }
interface I2 { void M(); }
class C : I1, I2
{
void I1.M() { ... }
void I2.M() { ... }
}
C c = new C();
c.M(); // Error, otherwise: which one?
(c as I1).M(); // Ok, no ambiguity.
If those methods were public, you would have a name-clash that cannot be resolved by the normal overload rules.
For the same reason you cannot even call M()
from inside a class C
member. You will have to cast this
to a specific interface first to avoid the same ambiguity.
class C : I1, I2
{
...
void X()
{
M(); // error, which one?
((I1)this).M(); // OK
}
}
http://msdn.microsoft.com/en-us/library/aa288461(VS.71).aspx : When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface.
Customer customer = new Customer();
Console.WriteLine(customer.PrintName());
Violates this
You cannot use access modifiers when implementing interface explicitly. Member will be binded to the interface anyway, so it is no need to specify access modifier, because all interface members are always public, also all explicitly implemented members can be accessed only through member of interface type (see statichippo's answer for example).
This is an Explicit implementation, the default scope for the members of the interface is public, whereas it is private in case of a class, ergo, there is no need to use the Public modifier because when we call that method it would be called with the reference of the interface only.
According to "MSDN Explicit Interface Implementation" the functions that implement explicit interfaces are never explicitly defined public. They are public by default. It would be meaningless to define them otherwise.
When we want to go with implicit implementation for the above example, following would be the code.
interface I1 { void M(); }
interface I2 { void M(); }
class C : I1, I2
{
public void M() { ... }
}
C c = new C();
c.M(); // Ok, no ambiguity. Because both Interfaces gets implemented with one method definition.
ReferenceURL : https://stackoverflow.com/questions/2669031/compilation-error-the-modifier-public-is-not-valid-for-this-item-while-expl
'programing' 카테고리의 다른 글
상속과 함께 Swift 4에서 Decodable 사용 (0) | 2020.12.25 |
---|---|
각도 6 의존성 주입 (0) | 2020.12.25 |
@Column JPA 어노테이션에 설정된 경우 길이 속성은 무엇을합니까? (0) | 2020.12.25 |
Android의 9 패치 이미지 오류 (0) | 2020.12.25 |
Django Unique Together (외래 키 포함) (0) | 2020.12.25 |