코드 다음 사항을 고려 존재하는 경우, 고아입니다왜 객체는 작성자
public class City
{
public string Name { get { return "New York"; } }
Building empEstate;
Building nyTimes;
public void Init()
{
// I hate passing "this" to all object
empEstate = new EmpEstate(this);
setSomeProperty(empEstate);
// any one can create new object of some other city
// and pass to the building
nyTimes = new NYTimes(this);
...
other = new OtherBuildings(this)
}
public void PrintAddresses()
{
empEstate.Print();
nyTimes.Print();
...
other.Print();
}
}
public abstract class Building {
City _city;
public Building(City city){
this._city = city;
}
public abstract string Name { get;}
public void Print(){
Console.WriteLine(this.Name);
Console.Write(",");
Console.WriteLine(this._city.Name);
}
}
나는이 방법으로 더 나은 솔루션을 원하는 우선. 인쇄는 단지 하나의 예일뿐입니다. 실제로 각 빌딩 객체는 City 객체에 이벤트를 발생시킵니다. 도시에 여러 건물이있을 수 있으므로 각 건물에 처리기를 추가하고 싶지 않습니다. 또한 각 건물에 대해 두 가지 작업 (하나의 초기화 및 두 번째 목록에 추가, 하나는 새 건물 작성시 목록에 추가하는 것을 잊어 버림)을 각 목록에 추가하고 싶지 않습니다. 이를 위해 호출자는 컨트롤의 Parent 속성과 같이 호출자가 자동으로 사용할 수있게하려고합니다. (컨트롤에 this.Controls가 추가되었지만)
메모리를 사용하여 누가 현재 개체의 부모인지 알 수 있습니까? GC는 개체가 참조되지 않고 있음을 어떻게 알 수 있습니까 (작성자 포함). 메모리를 사용하여 호출자 객체를 식별하는 방법 (안전 또는 안전하지 않은)을 만들 수 있습니까? StackTrace를 사용하여 호출 hirarchy를 볼 수 있습니다. 새 객체가 생성 될 때 여기에서 인터셉트 할 수 있습니까?
'부모'란 무엇을 의미합니까? – CodesInChaos
부모는 개체가 선언되거나 생성 된 개체를 말할 수 있습니다. – hungryMind