相关疑难解决方法(0)

什么是虚拟方法?

为什么要将方法声明为"虚拟".

使用虚拟有什么好处?

c# virtual keyword

69
推荐指数
6
解决办法
5万
查看次数

在另一个类属性中使用类名。?

我对 C# 很陌生。我有两个班级,一个电影班级和一个流派班级。我无法理解“公共虚拟流派类型”这一属性有人可以解释一下吗?以下是两个类

 public class Genre :IEntityBase
{
    public Genre()
    {
        Movies = new List<Movie>();
    }
    public int ID { get; set; } 
    public string Name { get; set; } 
    public virtual ICollection<Movie> Movies { get; set; }
}


public class Movie:IEntityBase
{
     public Movie() 
     {                                                          
         Stocks = new List<Stock>(); 
     }
     public int ID { get; set; }          
     **public virtual Genre Genre { get; set; }** 
     public virtual ICollection<Stock> Stocks { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

c#

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

继承初始化的字段

我真的不了解C#中的一种行为:

class Animal {
        public string species = "animal";

        public void Introduce() {
            Console.WriteLine(species);
        }
    }


    class Cat: Animal {
        new public string species = "cat";

        // public void Introduce() {Console.WriteLine(species);}
    }

    class Program
    {
        static void Main(string[] args)
        {
            var cat = new Cat();
            cat.Introduce();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这段代码在执行时输出>>> animal
对我而言,由于Cat继承了Animal,因此调用cat.Introduce应该在“ cat实例的范围内”调用Animal.Introduce。我不明白为什么该程序选择species动物领域而不是猫领域...

我知道我可以使用变通办法,但是我相信我缺少有关c#设计的一些知识,有人可以解释这种现象吗?

谢谢

c# inheritance

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

标签 统计

c# ×3

inheritance ×1

keyword ×1

virtual ×1