gpt4 book ai didi

c# 为什么我们不能覆盖静态属性? (高级类如何使用高级数据调用基类方法)

转载 作者:太空狗 更新时间:2023-10-29 23:09:50 24 4
gpt4 key购买 nike

我的问题可能措辞不当,可能是个骗局,但我开始了

class person {
protected static string request = "select * from person where gender in ('male','female')";
public string sharedmethod()
{
return "the request is" + request;
}
}

class man:person
{
override protected static string request = "select person.*,man.* from person,men where mytype in ('male') ";
DateTime dateOfFirstCar;
}

class woman:person
{
override protected static string request = "select person.*,woman.* from person,women where mytype in ('female') ";
DateTime dateOfFirstITBAG;
}

class Program
{
static void Main(string[] args)
{
if (new person().sharedmethod() == new man().sharedmethod())
Console.Write("too bad query is the same in base and dervide class");
}
}

人就是人
女人是一个人
Person,man,woman 存在于我的数据库中,但需要不同的查询

我不想重复这些查询,所以我认为将它们存储在每个类的静态属性中是个好主意。

我在基类中得到了一些低级的东西(那里没有图)(因为我不想重复),我希望继承类在继承类的上下文中调用基类方法

我希望 man.[inherited]somemethod() 执行 person.somemethod() 但变量来自 man

谢谢

最佳答案

添加一个覆盖静态字符串的非静态属性,并改为引用该属性:

class person {
private const string request = "select * from person where gender in ('male','female')";
protected virtual string Request {get {return request;}}
public string sharedmethod() {
return "the request is" + Request;
}
}

class man:person {
private const string request = "select person.*,man.* from person,men where mytype in ('male') ";
protected override string Request {get {return request;}}
DateTime dateOfFirstCar;
}

class woman:person {
private const string request = "select person.*,woman.* from person,women where mytype in ('female') ";
protected override string Request {get {return request;}}
DateTime dateOfFirstITBAG;
}

关于c# 为什么我们不能覆盖静态属性? (高级类如何使用高级数据调用基类方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9821903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com