有人可以帮我理解为什么这段代码片段返回"Bar-Bar-Quux"?在阅读界面后,我很难理解这一点.
interface IFoo
{
string GetName();
}
class Bar : IFoo
{
public string GetName() { return "Bar"; }
}
class Baz : Bar
{
public new string GetName() { return "Baz"; }
}
class Quux : Bar, IFoo
{
public new string GetName() { return "Quux"; }
}
class Program
{
static void Main()
{
Bar f1 = new Baz();
IFoo f2 = new Baz();
IFoo f3 = new Quux();
Console.WriteLine(f1.GetName() + "-" + f2.GetName() + "-" + f3.GetName()); …Run Code Online (Sandbox Code Playgroud) 我在"新"有意义时遇到的示例涉及维护情况,其中子类继承自库中的基类,并且该库的新版本添加了一个与已经存在的方法同名的方法.在子类中实现.(参见:脆基类)
我想知道的是,是否存在使用"新"是一个很好的设计选择(而不是维护中可能需要的东西)的情况.使用override无法完成的一件事是更改方法/属性的返回类型,同时保持名称相同,但我怀疑它是否是一个好的设计选择.
public class FruitBasket {
public int Weight { get; set;}
public List<Fruit> Fruits {get; set;}
}
public class AppleBasket : FruitBasket {
public new List<Apple> Fruits {get; set;}
}
Run Code Online (Sandbox Code Playgroud) 关于方法重写和OOP原则的有效性,我有一点混乱.我知道关于密封,阴影,覆盖,虚拟等的一切,但我遇到了一个场景,这让我很困惑.假设我有:
class classA
{
public virtual void sayhello()
{
Console.WriteLine("hello I'm A");
}
};
class classB :classA
{
public override void sayhello()
{
Console.WriteLine("hello I'm B");
}
};
class Program
{
static void Main(string[] args)
{
classB a = new classB();
a.sayhello();
}
}
Run Code Online (Sandbox Code Playgroud)
根据我到目前为止研究的所有内容,可以使用子类中的override关键字覆盖声明为virtual或abstract(在抽象类中)的方法.根据这个,上面的代码工作完美.当我删除virtual关键字,然后尝试使用override关键字覆盖该方法时,编译器会给出错误:
不能覆盖继承的成员'inheritence.classA.sayhello()',因为它没有标记为虚拟,抽象或覆盖
然后我从子类中删除了覆盖关键字,并将实现提供为:
class classB :classA
{
public void sayhello()
{
Console.WriteLine("hello I'm B");
}
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,该方法可以被覆盖.我能够覆盖不是虚拟或抽象的方法.所以,我的问题是:
1. …
我有以下课程:
namespace ConsoleApplication8
{
public abstract class Employee
{
public virtual void Show()
{
Console.WriteLine("from base.");
}
}
public class Manager:Employee
{
public void Show()
{
Console.WriteLine("from child.");
}
}
class Program
{
static void Main(string[] args)
{
var man=new Manager();
man.Show();
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里我没有override为Show()派生类中的方法使用任何关键字,Manager但代码运行正常.那么override关键字的实际用途是什么?
我试图更好地理解c#中的抽象类.
我知道在抽象类中你必须覆盖抽象方法并且可以覆盖虚方法.
我的问题是:
我可以覆盖非虚拟方法吗?(我知道通常我不能 - 但也许抽象类不同?)或者它会隐藏吗?
另外,正如我在这里读到的如何强制调用C#派生方法(第一个答案) - 在我看来,因为非虚拟方法在编译时静态链接而且无法更改 - 我无法调用方法在派生类中,从来没有?如果是这样 - 隐藏方法有什么意义?
在我更多地了解多态性的过程中,我构建了一个小测试并且它返回了意想不到的结果.
所以我的想法是用虚拟/覆盖关键字覆盖基类方法,但似乎我不需要那些?
public class Employee
{
public Employee()
{
this.firstName = "Terry";
this.lastName = "Wingfield";
}
public string firstName { get; set; }
public string lastName { get; set; }
public void writeName()
{
Console.WriteLine(this.firstName + " " + this.lastName);
Console.ReadLine();
}
}
public class PartTimeEmployee : Employee
{
public void writeName()
{
Console.WriteLine("John" + " " + "Doe");
Console.ReadLine();
}
}
public class FullTimeEmployee : Employee
{
public void writeName()
{
Console.WriteLine("Jane" + " " + "Doe");
Console.ReadLine();
} …Run Code Online (Sandbox Code Playgroud) 我在这里面临一个问题,让我们假设我有一个父类:
class ParentClass
{
public void MethodA()
{
//Do stuff
MethodB();
}
public void MethodB()
{
//Do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
继承ParentClass并重写MethodB()的子类:
class ChildClass : ParentClass
{
public new void MethodB()
{
//Do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我从ChildClass对象调用MethodA()
public static void Main()
{
ChildClass childObject = new ChildClass();
childObject.MethodA();
}
Run Code Online (Sandbox Code Playgroud)
我怎么能确定将调用MethodB()帽子来自子类?