C# 如何避免在创建派生类对象时调用基类属性?
我有一个基类ProductDetails,它负责解析productstring,还有一个自制造类继承自基类ProductDetails,还有一个类-ThirdPartyProduct,它继承自自制造类,并具有特定于该类的其他详细信息,就像第三方的名字,第三方的地址 a对于自制产品,字符串应以0作为前缀 b对于第三方产品,字符串的前缀应为1 工作流程如下所示: 我使用以下代码创建第三方产品对象:C# 如何避免在创建派生类对象时调用基类属性?,c#,inheritance,properties,C#,Inheritance,Properties,我有一个基类ProductDetails,它负责解析productstring,还有一个自制造类继承自基类ProductDetails,还有一个类-ThirdPartyProduct,它继承自自制造类,并具有特定于该类的其他详细信息,就像第三方的名字,第三方的地址 a对于自制产品,字符串应以0作为前缀 b对于第三方产品,字符串的前缀应为1 工作流程如下所示: 我使用以下代码创建第三方产品对象: private void ThirdParty_Click(object sender, Routed
private void ThirdParty_Click(object sender, RoutedEventArgs e)
{
ThirdPartyProduct thirdparty = new ThirdPartyProduct ("1000", "200", "PROD1",
"XYZW", "ABCD");
string thirdpartyProductString = thirdparty.ToString();
}
如果您在下面的代码中检查变量thirdpartyProductString,它是:追加0,然后追加1。
它先附加0,然后再附加1,而我只希望1加前缀。
我想,由于在创建第三方对象时也会调用SelfManufactured类,所以它会不断追加值,所以我希望ToString在不需要时不会追加值
ProductDetails.cs
using System;
namespace product
{
public class ProductDetails
{
private readonly string _productString;
public string ProductString
{
get => ToString();
}
public ProductDetails(string productId, string productName, string productLot)
{
_productString = // somevalue here....
}
public override string MyString()
{
return _productString;
}
}
}
SelfManufactured.cs
public class SelfManufactured : ProductDetails
{
public override string MyString()
{
string str = string.Empty;
str = "additional value of 0" + // append the identifier as '0' with product string
return str;
}
}
ThirdPartyProduct.cs
public class ThirdPartyProduct : SelfManufactured
{
public string ThirdPartyName { get; set; }
public string ThirdPartyAddress { get; set; }
public override string MyString()
{
string str = string.Empty;
// append the identifier as '1' with product string and add additional details with the string.
return str;
}
}
由于您在ThirdPartyProduct类中调用base.ToString,它显然调用了基类ToString方法,并向其中添加了0,最后的字符串为101000200PROD1XYZWABCD 如果您不想这样做,那么可以直接从ThirdPartyProduct类的ToString方法引用ProductDetails类的ProductString属性。请看下面我的更改 ProductDetails.cs-我将从此处返回私有只读
public string ProductString
{
get { return _productString; }
}
然后,ThirdPartyProduct.cs类的ToString方法进行了以下更改
public override string ToString()
{
string str = string.Empty;
// append the identifier as '0' with product string and add additional details with the string.
str = (int)ProductType.ThirdParty + ProductString + ThirdPartyName + ThirdPartyAddress;
return str;
}
在下一行进行了更改-
str=intProductType.ThirdParty+base.ToString+ThirdPartyName+ThirdPartyAddress
到
str=intProductType.ThirdParty+ProductString+ThirdPartyName+ThirdPartyAddress
注意:-我希望ThirdPartyProduct类始终不应附加0,那么上面的解决方案应该可以,否则您需要考虑其他计算方法 ProductDetails类中已经有一个名为TypeOfProduct的属性。因此,在SelfManufactured.ToString中,使用属性值,而不是硬编码产品类型:
str = (int)TypeOfProduct + base.ToString();
然后,在ThirdPartyProduct中,不需要使用product类型,因为这是在基类中处理的。因此,请调用base.ToString,后跟类特定的详细信息:
str = base.ToString() + ThirdPartyName + ThirdPartyAddress;
在哪里添加主角?为什么要将整数放在一个字符串前面,该字符串将删除前导零:str=intProductType.ThirdParty。为什么需要base.ToString?看起来您正在学习一个类并转换为字符串。您不应该引用基中的字符串对象吗?我想知道这个问题中的很多细节是否与问题无关。实际的问题似乎是关于ToString的行为和继承。关于XML和构造函数的部分是否相关?如果没有,删除它们将使问题更容易理解和回答。