2010-04-06 4 views
1
public LocalizedDisplayNameAttribute(string displayNameKey) 
      : base(displayNameKey) 
     { 

     } 

클래스에 함수 뒤에 :base을 넣으면 그 의미는 무엇입니까?퍼팅 : 기능 후 기본?

답변

3

base은 '이 클래스의 생성자에서 생성자'라는 기본 클래스를 호출하고 있음을 나타냅니다.

일반적인 방법이 아닌 생성자에만 유효합니다. 귀하의 경우 LocalizedDisplayNameAttribute이 생성되면 displayNameKey 매개 변수가 기본 클래스의 생성자에 전달됩니다.

+0

언젠가 옵션이 아닙니다 ... 기본 클래스의 생성자에 인수가 필요할 수 있습니다 ... – tanascius

3

즉, 클래스의 기본 클래스의 생성자를 호출하게됩니다. 이 여전히 이해가되지 않는 경우, 당신은 아마 예를 here를 들어, 객체 지향 언어에서 생성자에 대해 좀 더 읽어

public class A { 
    public A() { 
    Console.WriteLine("You called the first constructor"); 
    } 
    public A(int x) { 
    Console.WriteLine("You called the second constructor with " + x); 
    } 
} 

public class B : A { 
    public B() : base() { } // Calls the A() constructor 
} 

public class C : A { 
    public C() : base(10) { } // Calls the A(int x) constructor 
} 

public class D : A { 
    public D() { } // No explicit base call; Will call the A() constructor 
} 


... 
new B(); // Will print "You called the first constructor" 
new C(); // Will print "You called the second constructor with 10" 
new D(); // Will print "You called the first constructor" 

:

이를 생각해 보자.

0

거기에 가지고있는 것은 Constructor이며 함수 나 메서드는 아닙니다 (생성자는 클래스가 인스턴스화 될 때 자동으로 호출되는 특수 메서드입니다). 생성자 다음에 :base (매개 변수)을 추가하면 클래스 생성자에 전달 된 매개 변수로 기본 클래스 (클래스가 상속받은 클래스)의 생성자를 호출 할 수 있습니다.

생성자에 대한 좋은 자습서는 http://www.yoda.arachsys.com/csharp/constructors.html에서 찾을 수 있습니다.

관련 문제