相关疑难解决方法(0)

为什么在派生类中调用方法会调用基类方法?

考虑以下代码:

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# class derived-class

144
推荐指数
4
解决办法
7万
查看次数

C#中的阴影和覆盖之间的区别?

阴影覆盖 C#中的方法有什么区别?

c#

95
推荐指数
4
解决办法
8万
查看次数

使用有效示例隐藏在c#中的方法.为什么在框架中实现?什么是真实世界的优势?

任何人都可以用一个有效的例子解释隐藏在C#中的方法的实际用法吗?

如果使用new派生类中的关键字定义方法,则无法覆盖该方法.然后它与创建一个具有不同名称的新方法(除了在基类中提到的方法之外)相同.

有没有具体的理由使用new关键字?

.net c# oop polymorphism inheritance

13
推荐指数
3
解决办法
1万
查看次数

使用"方法隐藏"有什么意义?

可能重复:
使用有效示例隐藏在c#中的方法.它是否在框架中实现.?什么是真实世界的优势.

我想知道方法隐藏在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#中的方法的最佳用法是什么?

谢谢您的帮助

c# methods method-hiding

9
推荐指数
1
解决办法
4285
查看次数

C#8中的默认接口方法

请考虑以下代码示例:

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)

c# c#-8.0 default-interface-member

9
推荐指数
1
解决办法
703
查看次数

为什么我不能为重写属性添加set访问器?

在基类中我有这个属性:

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说我无法覆盖,因为它没有设置访问器.为什么这是个问题?我该怎么办?

c# inheritance properties accessor

8
推荐指数
2
解决办法
5007
查看次数

将 IEnumerable<Inherited> 转换为 IEnumerable<Base>

我正在尝试将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# linq asp.net-mvc entity-framework

3
推荐指数
1
解决办法
1286
查看次数

c#中的类转换

这是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)

我们如何得到这个输出.任何人都可以在这里解释类的铸造过程.

更新:

这如何显示阴影和覆盖之间的区别

c# asp.net oop casting

2
推荐指数
1
解决办法
139
查看次数

C#WinForms App中的字典引起空引用

我正在使用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)

c# dictionary visual-studio winforms

1
推荐指数
1
解决办法
393
查看次数

覆盖具有相同名称但属性不同的属性

我想知道是否有一种方法来覆盖派生类中具有不同类型的同名变量.有点像这样的东西(虽然这段代码不会编译):

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不同.

c# inheritance

1
推荐指数
2
解决办法
929
查看次数

C#泛型类型干扰奇怪的行为

请考虑以下代码:

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)

  • //打印(a)中
  • 一个
  • 一个
  • HelloA
  • //打印(b)中
  • 乙 …

c# generics polymorphism

0
推荐指数
1
解决办法
89
查看次数