📄 ch2_03.cs
字号:
using System;
class TestConstructor
{
int[] anArray;
// This is the default constructor (no arguments)
public TestConstructor()
{
anArray = new int[6];
for( int i=0; i<6; ++i )
anArray[i] = i;
}
// This is the "normal" constructor for the class
public TestConstructor(int arraySize)
{
anArray = new int[arraySize];
for( int i=0; i<arraySize; ++i )
anArray[i] = i;
}
public int getElement(int index)
{
if ( index < 0 || index > anArray.Length-1 )
return -1;
return anArray[ index ];
}
}
class CH2_3 {
public static void Main()
{
// Create an object with 10 elements
TestConstructor tc = new TestConstructor(10);
// print out every other element to test
for ( int i=0; i<12; i+=2)
Console.WriteLine("Element {0} = {1}", i, tc.getElement(i));
// Create an object with no set # of arguments
TestConstructor tc1 = new TestConstructor();
// print out every other element to test
for ( int i=0; i<12; i+=2)
Console.WriteLine("Element {0} = {1}", i, tc1.getElement(i));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -