c\#클래스 구성원 이 순 서 를 초기 화 하 는 특수 한 상황
하위 클래스 정적 필드
자 류 정태 구조
하위 클래스 인 스 턴 스 필드
부모 클래스 정적 필드
아버지 류 정태 구조
부모 클래스 인 스 턴 스 필드
부계 실례 구조
하위 클래스 인 스 턴 스 구조
왜'보통'이 라 고 순 서 를 초기 화한 거 죠?클래스 구조 에 따라 클래스 구성원 의 초기 화 순 서 는 변 함 이 없 기 때문이다.그러나 이 순 서 는 기초 로 다른 특수 한 초기 화 순 서 를 유도 할 수 있다.다음은 두 가지 특수 한 상황 을 살 펴 보 겠 습 니 다.
static void Main(string[] args)
{
Console.WriteLine("--------------- ---------------");
var child1 = new Child1();
Console.WriteLine("
--------------- ---------------");
var child2 = new Child2();
Console.WriteLine("
--------------- ---------------");
var child3 = new Child3();
Console.ReadKey();
}
public class Child1 : Base1
{
public static Display ChildStatic = new Display("Child static filed");
private Display _childFiled = new Display("Child filed");
static Child1() => Console.WriteLine("Child static ctor");
public Child1() => Console.WriteLine("Child ctor");
}
public class Child2 : Base2
{
/// <summary>
///
/// </summary>
public static Display ChildStatic = new Display("Child static filed", () => BaseStatic);
private Display _childFiled = new Display("Child filed");
static Child2() => Console.WriteLine("Child static ctor");
public Child2() => Console.WriteLine("Child ctor");
}
public class Child3 : Base3
{
public static Display ChildStatic = new Display("Child static filed");
private Display _childFiled = new Display("Child filed");
/// <summary>
///
/// </summary>
static Child3()
{
Console.WriteLine("Child static ctor");
var baseStatic = BaseStatic;
}
public Child3() => Console.WriteLine("Child ctor");
}
/// <summary>
/// 3 Base ,
/// </summary>
public class Base1
{
public static Display BaseStatic = new Display("Base static filed");
private Display _baseFiled = new Display("Base filed");
static Base1() => Console.WriteLine("Base static ctor");
public Base1() => Console.WriteLine("Base ctor");
}
public class Base2
{
public static Display BaseStatic = new Display("Base static filed");
private Display _baseFiled = new Display("Base filed");
static Base2() => Console.WriteLine("Base static ctor");
public Base2() => Console.WriteLine("Base ctor");
}
public class Base3
{
public static Display BaseStatic = new Display("Base static filed");
private Display _baseFiled = new Display("Base filed");
static Base3() => Console.WriteLine("Base static ctor");
public Base3() => Console.WriteLine("Base ctor");
}
public class Display
{
public Display(string msg, Func<Display> displayFunc = null)
{
Console.WriteLine(msg);
var display = displayFunc?.Invoke();
}
}
보충:
1.정적 구조 함 수 는 스 레 드 가 안전 합 니 다.이 클래스 가 정의 하 는 다른 방법,속성 또는 변 수 를 처음 방문 하기 전에 실 행 됩 니 다.
2.컴 파일 러 는 모든 구조 함수(정적 과 인 스 턴 스 포함)의 시작 에 적당 한 프로그램 코드 를 넣 어서 구성원 필드 를 정의 할 때 지정 한 초기 값 을 이 변수 에 설정 합 니 다.이것 이 바로 필드 가 구조 함수 가 실행 되 기 전에 초기 화 되 는 이유 입 니 다.
3.정적 변수 든 인 스 턴 스 변수 든 그 수 치 는 성명 할 때 초기 화 되 어야 합 니 다.그러나 다음 세 가지 경우 초기 화 문 구 를 작성 해 서 는 안 됩 니 다.
4.567917.필드 를 0 또는 null 로 초기 화 합 니 다.시스템 은 개발 자가 작성 한 코드 를 실행 하기 전에 메모 리 를 제거 하기 때문에 제로 명령 을 반복 하면 불필요 해 집 니 다4.567917.필드 의 초기 값 은 서로 다른 구조 함수 에 따라 설정 해 야 한다.이 경우 필드 의 초기 화 는 구조 함수 에 넣 으 면 됩 니 다.그렇지 않 으 면 불필요 한 대상 을 만 들 수 있 습 니 다4.567917 필드 의 초기 화 과정 에서 이상 이 발생 할 수 있 습 니 다.이것 도 구조 함수 에 넣 어 초기 화 해 야 합 니 다.또한 정적 구조 함수 의 이상 이 벗 어 나 지 않도록 주의해 야 합 니 다.왜냐하면 하나의 AppDomain 은 특정한 유형의 정적 구조 함수 만 호출 할 수 있 기 때 문 입 니 다.
클래스 구성원 의 초기 화 순 서 를 알 아 보면 프로그램 이 실행 하 는 세부 사항 을 더욱 상세 하 게 알 수 있 고'구조 함수 에서 가상 함수 나 추상 함수'와 같은 코드 를 쓰 지 않도록 할 수 있 습 니 다.
이상 은 c\#클래스 구성원 이 순 서 를 초기 화 하 는 특수 한 상황 에 대한 상세 한 내용 입 니 다.C\#클래스 초기 화 순서 에 관 한 자 료 는 다른 관련 글 을 주목 하 십시오!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C#Task를 사용하여 비동기식 작업을 수행하는 방법라인이 완성된 후에 이 라인을 다시 시작할 수 없습니다.반대로 조인(Join)만 결합할 수 있습니다 (프로세스가 현재 라인을 막습니다). 임무는 조합할 수 있는 것이다. 연장을 사용하여 그것들을 한데 연결시키는 것이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.