这是我的代码.我无法弄清楚为什么这段代码给出了’由于StackOverFlowException而导致进程终止’.
@H_404_9@解决方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SadiSDAL4 { class Program { static void Main(string[] args) { PrivateVehical privateV = new PrivateVehical("001"); PrivateVehical pVehical = (PrivateVehical)privateV.Clone(); Console.WriteLine("Cloned : {0}",privateV.name); Console.WriteLine("Cloned : {0}",pVehical.name); privateV.name = "Sadia's Car"; Console.WriteLine("Cloned : {0}",pVehical.name); pVehical.name = "Waheed's Car"; Console.WriteLine("Cloned : {0}",pVehical.name); Console.WriteLine(privateV.GetHashCode().ToString()); Console.WriteLine(pVehical.GetHashCode().ToString()); PublicVehical publicV = new PublicVehical("002"); Console.WriteLine(publicV.id); PublicVehical pubVehi = (PublicVehical)publicV.Clone(); Console.WriteLine("Cloned : {0}",pubVehi.id); } } abstract class Vehical { private string vehicalId = "01"; public string name = "Car_1"; public Vehical(string id) { this.vehicalId = id; } public string id { get { return id; } set { this.vehicalId = id; } } public abstract Vehical Clone(); } class PrivateVehical : Vehical { public PrivateVehical(string id) : base(id) { } public override Vehical Clone() { return (Vehical)this.MemberwiseClone(); } } class PublicVehical : Vehical { public PublicVehical(string id) : base(id) { } public override Vehical Clone() { return (Vehical)this.MemberwiseClone(); } } }
这是输出.
有人能解释一下它的原因是什么吗?为什么它在第一部分和第一部分工作不在另一个?