如果父类声明了一个静态变量,那么子类继承后是共享同一个地址还是另外复制一份?不明白,写代码验证一下~

 Father
    {
        public static string test = "父类的静态变量!";
    }
    
public class SonA : Father
    {
        
public SonA(string str)
        {
            test 
= str;
        }
    }
    
public class SonB : Father
    {
        
public SonB(string str)
        {
            test 
= str;
        }
    }
    
class Program
    {
        
static void Main(string[] args)
        {
            SonA s 
= new SonA("SonA的静态变量");
            SonB s1 
= new SonB("SonB的静态变量");
            Console.WriteLine(Father.test);
            Console.WriteLine(SonA.test);
            Console.WriteLine(SonB.test);
            Console.Read();
        }
运行结果:

静态变量的继承

不言而喻,子类和父类是共享同一个地址的。

相关文章:

  • 2021-10-02
  • 2022-01-10
  • 2021-12-27
  • 2021-12-24
  • 2022-12-23
  • 2022-12-23
  • 2021-06-17
  • 2021-10-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2022-01-29
相关资源
相似解决方案