- 时间:2017-08-24 整理:byzqy
题目:写出下列程序的输出结果:
//原题程序如下: class Class1 { private static int count = 0; static Class1() { count++; } public Class1() { count++; } } Class o1 = new Class1(); Class o2 = new Class2(); //请问,o1.count的值是多少?
将代码录入到 IDE(VisualStudio2015)中,将 o1.count 输出:
文件:Class1.cs
using System; namespace Interview5 { class Class1 { static void Main(string[] args) { Class1 o1 = new Class1(); //Console.WriteLine(o1.count); //编译报错:无法使用实例引用来访问成员"Class1.count";请改用类型名来限定它 Console.WriteLine("o1:count= " + count); //输出结果为:2 Console.WriteLine("o1:Class1.count= " + Class1.count); //输出结果为:2 Class1 o2 = new Class1(); Console.WriteLine(count); //输出结果为:3 Class1 o3 = new Class1(); Console.WriteLine(count); //输出结果为:4 Class1 o4 = new Class1(); Console.WriteLine(count); //输出结果为:5 Console.ReadKey(); } private static int count = 0; static Class1() { count++; } public Class1() { count++; } } }
输出结果如下:
考题意图猜测:
1、分别对 public 和 static 修饰的构造方法,判断实例化时使用的是哪一个创建的对象,那么这样的话,如果将其中一个的 count++ 改为 count-- 会怎么样?
2、考实例化多次,对 count 值的影响 o2 的 count 值会覆盖掉 o1 的 count 值?
验证猜想1:
将 static Class1 构造方法中的 count++ 改为 count-- :
using System; namespace Interview5 { class Class1 { static void Main(string[] args) { Class1 o1 = new Class1(); Console.WriteLine("o1: " + count); //输出结果为:0 Class1 o2 = new Class1(); Console.WriteLine("o2: " + count); //输出结果为:1 Class1 o3 = new Class1(); Console.WriteLine("o3: " + count); //输出结果为:2 Class1 o4 = new Class1(); Console.WriteLine("o4: " + count); //输出结果为:3 Console.ReadKey(); } private static int count = 0; static Class1() { count--; //修改为:count-- } public Class1() { count++; } } }
将 public Class1 构造方法中的 count++ 改为 count-- :
using System; namespace Interview5 { class Class1 { static void Main(string[] args) { Class1 o1 = new Class1(); Console.WriteLine("o1: " + count); //输出结果为:0 Class1 o2 = new Class1(); Console.WriteLine("o2: " + count); //输出结果为:-1 Class1 o3 = new Class1(); Console.WriteLine("o3: " + count); //输出结果为:-2 Class1 o4 = new Class1(); Console.WriteLine("o4: " + count); //输出结果为:-3 Console.ReadKey(); } private static int count = 0; static Class1() { count++; } public Class1() { count--; //修改为:count-- } } }
实验1:
using System; namespace Interview5 { class Class1 { static void Main(string[] args) { Class1 o1 = new Class1(); Console.WriteLine("o1: " + count_a); //输出:1 Console.WriteLine("o1: " + count_b); //输出:1 Class1 o2 = new Class1(); Console.WriteLine("o2: " + count_a); //输出:1 Console.WriteLine("o2: " + count_b); //输出:2 Class1 o3 = new Class1(); Console.WriteLine("o3: " + count_a); //输出:1 Console.WriteLine("o3: " + count_b); //输出:3 Class1 o4 = new Class1(); Console.WriteLine("o4: " + count_a); //输出:1 Console.WriteLine("o4: " + count_b); //输出:4 Console.ReadKey(); } private static int count_a = 0; private static int count_b = 0; static Class1() { count_a++; } public Class1() { count_b++; } } }
实验2:
using System; namespace Interview5 { class Class1 { static void Main(string[] args) { Class1 o1 = new Class1(); Console.WriteLine("o1: " + count_a); //输出:-1 Console.WriteLine("o1: " + count_b); //输出:-1 Class1 o2 = new Class1(); Console.WriteLine("o2: " + count_a); //输出:-1 Console.WriteLine("o2: " + count_b); //输出:-2 Class1 o3 = new Class1(); Console.WriteLine("o3: " + count_a); //输出:-1 Console.WriteLine("o3: " + count_b); //输出:-3 Class1 o4 = new Class1(); Console.WriteLine("o4: " + count_a); //输出:-1 Console.WriteLine("o4: " + count_b); //输出:-4 Console.ReadKey(); } private static int count_a = 0; private static int count_b = 0; static Class1() { count_a--; } public Class1() { count_b--; } } }
实验3: