考虑以下代码:
class Program
{
static void Main(string[] args)
{
Person person = new Teacher();
person.ShowInfo();
Console.ReadLine();
}
}
public class Person
{
public void ShowInfo()
{
Console.WriteLine("I am Person");
}
}
public class Teacher : Person
{
public new void ShowInfo()
{
Console.WriteLine("I am Teacher");
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,输出以下内容:
我是人
但是,你可以看到它是一个实例Teacher,而不是Person.代码为什么这样做?
任何人都可以用一个有效的例子解释隐藏在C#中的方法的实际用法吗?
如果使用new派生类中的关键字定义方法,则无法覆盖该方法.然后它与创建一个具有不同名称的新方法(除了在基类中提到的方法之外)相同.
有没有具体的理由使用new关键字?
我想知道方法隐藏在c#中有用吗?如果你深入研究这个概念,你会发现隐藏的方法令人困惑,并驱使我们采用意大利面条代码风格.有一些原因:
1-如果在Derived类中需要一个新方法,则可以在派生类中使用新名称定义新方法.
2 - 在使用upcasting时以多态方式使用,将无法实现预期的行为,这可能会令人困惑.
(特别是如果Base类和Derived Class位于不同的库中并且您希望在代码中的某个位置使用它们的多态用途,这种奇怪的行为可能会非常明显和明显)
class Base
{
public void Method() { Console.WriteLine("I am Base Class"); }
}
class Derived: Base
{
public new void Method() { Console.WriteLine("I am Derived Class");}
}
class Program
{
static void Main(string[] args)
{
Base C = new Derived();
C.Method(); //output:"I am Base Class"
}
}
Run Code Online (Sandbox Code Playgroud)
这样的行为可能是混乱的,但如果我们使用" 虚拟的"," 基本 "和" 覆盖在"" 派生 "(我的意思是多态的使用)的输出是:"我在派生类".这个输出是我认为我们大多数人所期望的.
换句话说,我相信在层次结构中,大多数开发人员期望在派生类中使用重写方法而不是新方法,而是希望隐藏基类实现.
我不明白为什么这样一个没有优势的坏东西有c#语言?至少我想知道隐藏在c#中的方法的最佳用法是什么?
谢谢您的帮助
请考虑以下代码示例:
public interface IPlayer
{
int Attack(int amount);
}
public interface IPowerPlayer: IPlayer
{
int IPlayer.Attack(int amount)
{
return amount + 50;
}
}
public interface ILimitedPlayer: IPlayer
{
new int Attack(int amount)
{
return amount + 10;
}
}
public class Player : IPowerPlayer, ILimitedPlayer
{
}
Run Code Online (Sandbox Code Playgroud)
我的问题在于代码:
IPlayer player = new Player();
Console.WriteLine(player.Attack(5)); // Output 55, --> im not sure from this output. I can compile the code but not execute it!
IPowerPlayer powerPlayer = new Player();
Console.WriteLine(powerPlayer.Attack(5)); …Run Code Online (Sandbox Code Playgroud) 在基类中我有这个属性:
public virtual string Text
{
get { return text; }
}
Run Code Online (Sandbox Code Playgroud)
我想覆盖它并返回一个不同的文本,但我也希望能够设置文本,所以我这样做:
public override string Text
{
get { return differentText; }
set { differentText = value; }
}
Run Code Online (Sandbox Code Playgroud)
然而,这不起作用.我得到一个红色的波浪形,set说我无法覆盖,因为它没有设置访问器.为什么这是个问题?我该怎么办?
我正在尝试将IEnumerable继承类型的类型转换IEnumerable为基类。
曾尝试以下:
var test = resultFromDb.Cast<BookedResource>();
return test.ToList();
Run Code Online (Sandbox Code Playgroud)
但得到错误:
您不能转换这些类型。Linq to Entities 仅支持转换原始 EDM 类型。
涉及的类如下所示:
public partial class HistoryBookedResource : BookedResource
{
}
public partial class HistoryBookedResource
{
public int ResourceId { get; set; }
public string DateFrom { get; set; }
public string TimeFrom { get; set; }
public string TimeTo { get; set; }
}
public partial class BookedResource
{
public int ResourceId { get; set; }
public string DateFrom { get; set; …Run Code Online (Sandbox Code Playgroud) 这是c#代码
class A {
public int Foo(){ return 5;}
public virtual int Bar(){return 5;}
}
class B : A{
public new int Foo() { return 1;} //shadow
public override int Bar() {return 1;} //override
}
Run Code Online (Sandbox Code Playgroud)
输出
Console.WriteLine(((A)clB).Foo()); // output 5 <<<--
Console.WriteLine(((A)clB).Bar()); // output 1
Run Code Online (Sandbox Code Playgroud)
我们如何得到这个输出.任何人都可以在这里解释类的铸造过程.
更新:
这如何显示阴影和覆盖之间的区别
我正在使用Visual Studio 2013来创建Visual C#Windows窗体应用程序,而我没有使用Designer来设置窗体.
我正在尝试使用Dictionary来存储位图,以便我可以稍后通过名称调用它们.但是当我调试脚本时,我收到错误:
An unhandled exception of type 'System.NullReferenceException' occurred in SimpleForm.exe
Additional information: Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
从行:
width = imgLetters["a"].Width;
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
减少仍会产生错误的代码版本:
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace SimpleForm
{
public class Test : Form
{
static Bitmap bmpLetterA;
static Bitmap bmpLetterB;
static Bitmap bmpLetterC;
private Dictionary<string, Bitmap> imgLetters;
public Test()
{
ImgInitialize();
ImgWidth();
}
private void ImgInitialize()
{
Dictionary<string, Bitmap> …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种方法来覆盖派生类中具有不同类型的同名变量.有点像这样的东西(虽然这段代码不会编译):
public class A
{
public virtual Point2D Point; // Represent a 2D point
}
public class B : A
{
public override Point3D Point; // Represent a 3D point
}
Run Code Online (Sandbox Code Playgroud)
这样做的原因是,A并且B可能共享相似的属性,只是它的维度Point不同.
请考虑以下代码:
class Program
{
static void Main(string[] args)
{
var a = new A();
var b = new B();
Print(a);
Print(b);
Console.WriteLine(b.Hello);
Console.ReadLine();
}
static void Print<T>(T t) where T : A
{
Console.WriteLine(typeof(T));
Console.WriteLine(t.GetType());
Console.WriteLine(t.Hello);
}
}
public class A
{
public string Hello { get { return "HelloA"; } }
}
public class B : A
{
public new string Hello { get { return "HelloB"; } }
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出(.NET FW 4.5)
c# ×11
inheritance ×3
oop ×2
polymorphism ×2
.net ×1
accessor ×1
asp.net ×1
asp.net-mvc ×1
c#-8.0 ×1
casting ×1
class ×1
dictionary ×1
generics ×1
linq ×1
methods ×1
properties ×1
winforms ×1