gpt4 book ai didi

c# - 调用继承和基方法

转载 作者:行者123 更新时间:2023-11-30 20:33:36 26 4
gpt4 key购买 nike

<分区>

我有两个类(class)

public class A
{
public A(string N)
{
Name = N;
}
public string Name { get; set; }

public void GetName()
{
Console.Write(Name);
}
}

public class B : A
{
public B(string N) : base(N)
{
}

public new void GetName()
{
Console.Write(new string(Name.Reverse().ToArray()));
}
}

我创建了一个新对象 B,我想从 A 调用 GetName 并从 调用 GetName >B

B foo = new B("foo");
foo.GetName(); // output "oof"

预期输出 "foooof"

我已经尝试过 public new void GetName() : base 但无法编译

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