- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace test3
- {
- abstract class ShapesClass
- {
- abstract public int Area();
- }
- class Square : ShapesClass
- {
- int x, y;
- // Because ShapesClass.Area is abstract, failing to override
- // the Area method would result in a compilation error.
- public Square(int i , int j)
- {
- x = i;
- y = j;
- }
- public override int Area()
- {
- return x * y;
- }
- }
- /*
- 要扩展或修改继承的方法、属性、索引器或事件的抽象实现或虚实现,必须使用 override 修饰符。
- */
- class Program
- {
- public static void useparams(params int[] list)
- {
- for (int i = 0; i < list.Length; i++)
- {
- Console.WriteLine(list[i]);
- }
- Console.WriteLine();
- // params 关键字 用于方法参数列表长度不定的情况;
- }
- static void useref(ref int i)
- {
- i = 44;
- //ref 关键字表示使用引用类型参数。其效果是,在方法中对参数所作的任何更改都将反映在改变量中。
- // 方法的定义和调用都必须显示的使用ref关键字。
- }
- static void useout(out int i)
- {
- i = 45;
- //out 关键字 表示使用引用类型参数。和ref的效果是一样的,不同之处在于,ref要求变量在引用之间必须初始化,
- // 而out无需进行初始化。
- }
- /*
- 尽管ref和out 关键字在运行时的处理方式不同,但是在编译时的处理方式是一样的,因此,如果一个
- * 方法是用ref 参数,另一个方法是用out参数 则无法重载这两个方法。
- * 例如:
- * class cs0663_example
- * {
- * public void samplemethod(out int i ){}
- * public void samplemethod(ref int i ){}
- * //这两个方法是一样的不可以这样重载
- * }
- */
- static void Main(string[] args)
- {
- Console.WriteLine("************************* params *******************************");
- useparams(1, 2, 3, 4);
- int[] myarray = new int[3] { 10, 11, 12 };
- useparams(myarray);
- Console.WriteLine("************************* ref *******************************");
- int val = 1;
- Console.WriteLine("the val is :" + val);
- useref(ref val);
- Console.WriteLine("the val is :" + val);
- Console.WriteLine("************************ out ******************************");
- int val2;
- useout(out val2);
- Console.WriteLine("the val2 is :" + val2);
- Console.WriteLine("************************ override ******************************");
- Square s = new Square(12,23);
- Console.WriteLine(s.Area().ToString());
- }
- }
- }