- , , . "" , , , . Windows- - , . .Net, , .Net, - object.
, , Main . "" .
class, , - . , () , :
[ ] [ ] class _ [: ] {_}:
class Demo{}, . :
new | . ( ). | |
public | ||
protected | . . | |
internal | (). | |
protected internal | (). | |
private | , . . | |
static | . | |
sealed | . . . | |
abstract | . . |
2-6 . , . .
. .
. . . , . : public internal. , , internal.
|
|
, , . new, :
Demo a = new Demo (); // Demo, OutOfMemoryException.
, . , . , - . , - , .
( ) . :
- : .
- , , , .
- ( ).
- ( ).
- ( , , ).
- ( ).
- ( ).
- ( , ).
- ( , ).
, . , , , . , - , , :
, b , b = . b . b .
. , . , . , b , .. . b .
:
, , , "". , . :
[] [] [const] [ = _ ]:
new | , | |
public | ||
protected | ||
internal | ||
protected internal | ||
private | ||
static | ||
readonly | ( , ) | |
volatile |
. .
|
|
1-6.
private. , , . .
, static, , , . (). , - . Demo .
class Circle{ public int x=0; public int y=0; public int radius=3; public const double pi = 3.14; public static string name = ""; double p; double s;} class Program{ static void Main() { Circle cr = new Circle(); // Console.WriteLine("pi=" + Circle.pi);// Console.Write(Circle.name);// // Console.WriteLine(" ({0},{1}) {2}", cr.x, cr.y, cr.radius); // Console.WriteLine(cr.p); - , .. p private Console.Write(" = "); int kof = int.Parse(Console.ReadLine()); cr.x -= kof; cr.y += kof; cr.radius *= kof; Console.WriteLine(" ({0},{1}) {2}", cr.x, cr.y, cr.radius); //cr.s = 2 * Circle.pi * cr.radius; - , .. s private }}
. . .
, , . this, .
this , , , , :
class Circle{ public int x=0; public int y=0; public int radius=3; public const double pi = 3.14; public static string name = ""; public Circle T() // { return this; } public void Set(int x, int y, int r) { this.x = x; this.y = y; radius=r; }} class Program{ static void Main() { Circle cr = new Circle(); // Console.WriteLine("pi=" + Circle.pi);// Console.Write(Circle.name);// // Console.WriteLine(" ({0},{1}) {2}", cr.x, cr.y, cr.radius); cr.Set(1, 1, 10); Console.WriteLine(" ({0},{1}) {2}", cr.x, cr.y, cr.radius); Circle b=cr.T();// cr, b=c Console.WriteLine(" ({0},{1}) {2}", b.x, b.y, b.radius); }}
|
|
. ( ) ( ).
new. . :
- , void.
- .
- - , , - null.
. , . , . Print :
class Circle { public int x; public int y; public int radius; public const double pi = 3.14; public static string name = ""; public Circle(int x, int y, int r)// { this.x = x; this.y = y; radius = r; } public void Print() { Console.Write(name); Console.WriteLine(" ({0},{1}) {2}", x, y, radius); Console.WriteLine(); } } class Program { static void Main() { Circle a = new Circle(0, 0, 1); // a.Print(); Circle b=new Circle(10, 10, 5);// b.Print(); } }, . . :
class Circle { public int x; public int y; public int radius; public const double pi = 3.14; public static string name = ""; public Circle(int x, int y, int r)// 1 { this.x = x; this.y = y; radius = r; } public Circle(int r)// 2 { radius = r; } public void Print() { Console.Write(name); Console.WriteLine(" ({0},{1}) {2}", x, y, radius); Console.WriteLine(); } } class Program { static void Main() { Circle a = new Circle(0, 0, 1); // 1 a.Print(); Circle b=new Circle(5);// 2 b.Print(); } }, 2 x, y, 0.
- , -, . this , :
public Circle(int x, int y, int r):this(r) // 1{ this.x = x; this.y = y;} public Circle(int r) // 2{ radius = r;}, , , , . 1 2.
, , . .
, # , - (private) , - , . . , . :
|
|
2.0 , static. , , . static ( ). .
static class Demo{ static int a=20; static int b=10; public static void Print () { Console.WriteLine("{0}+{1}={2}",a,b,a+b); Console.WriteLine("{0}*{1}={2}",a,b,a*b); Console.WriteLine("{0}-{1}={2}",a,b,a-b); } } class Program{ static void Main() { Demo.Print(); }}
, , , , - . , , - . .
:
[] [] _{ [get _] [set _]}. ( public).
, (get) (set) . get, set, . set, . get, .
, , . get return, , .
class Circle { // int x; int y; int radius; public static string name = ""; public Circle(int x, int y, int r):this(r)// 1 { this.x = x; this.y = y; } public Circle(int r)// 2 { radius = r; } public void Print() { Console.Write(name); Console.WriteLine(" ({0},{1}) {2}", x, y, radius); Console.WriteLine(); } public int X // x { get { return x; } set { x = value; } } public int Y // y { get { return y; } set { y = value; } } public int R // radius { get { return radius; } set { radius = value; } } public double P // { get { return 2* Math.PI *radius; } } public double S // { get { return Math.PI *radius*radius; } } } class Program { static void Main() { Circle a = new Circle(0, 0, 1); // a.Print(); // a.X=1; a.Y=1; a.R=10; //a.S=100; // - Console.WriteLine("=({0},{1}) ={2} ={3:f2} ={4:f2}", a.X, a.Y, a.R, a.P, a.S); } }13.1.
" - ",
, . " - ". Circle :
- Solution Explorer Hello ( ).
- Add/Add Class
Name ircle.cs Add.
Solution Explorer :
Circle.cs :
using System; namespace ConsoleApplication1{ public class Circle { public Circle() { // // TODO: Add constructor logic here // } }}- namespace ConsoleApplication1 namespace MyProgram, Program.cs Circle.cs .
- Circle Program.cs Circle.cs.
- , .
|
|
- Point, :
- :
- int x, y;
- , :
- ;
- .
- , :
- ;
- ;
- (a, b).
- :
- - ( );
- ( ).
- Triangle, :
- :
- int a, b, c;
- , .
- , :
- ;
- ;
- .
- :
- - ( );
- , ( ).
- Rectangle, :
- :
- int a, b;
- , .
- , :
- ;
- ;
- .
- :
- - ( );
- , ( ).
- Money, :
- :
- int first;//
- int second; //
- , .
- , :
- ;
- , N .
- , n .
- :
- - ( );
- ( ).
- . :
- :
- int [] IntArray;
- int n.
- , n.
- , :
- ;
- ;
- .
- :
- ( );
- ( ).
- . :
- :
- int [,] IntArray;
- int n.
- , n×n.
- , :
- ;
- ;
- i- .
- :
- ( );
- ( ).
- . :
- :
- double [][] DoubelArray;
- int n, m.
- , n×m.
- , :
- ;
- ;
- .
- :
- ( );
- ( ).
- o . :
- :
- StringBuilder Line;
- int n.
- , n .
- , :
- ;
- ;
- .
- :
- ( );
- , , ( )
- . :
- :
- Regex r;
- string text;
- , :
- , , ;
- , ;
- , ;
- :
- ( )
- , ( )
- . :
- DataTime data.
- , :
- 1.01.2009
- , :
- ;
- ;
- .
- :
- ( )
- ( )
, :
- : , , , .
- .
: , , ,
# , , .
. , , . .
, . , , , ( , . .).
:
[] [extern] ~_(){_}, . (~), . . , extern. :
class DemoArray { int[] MyArray;// string name; // public DemoArray(int size,int x, string name)// { MyArray = new int[size]; this.name = name; for (int i=0;i<size; ++i) MyArray[i]=x; } public void Print ()// { Console.Write(name+ ": "); foreach (int a in MyArray) Console.Write(a+" "); Console.WriteLine(); } public int LengthN // { get { return MyArray.Length; } } ~DemoArray()// { Console.WriteLine(" "+this.name); } } class Program { static void Main() { DemoArray a= new DemoArray(5,2, ""); a.Print(); DemoArray b = new DemoArray(6,1, ""); b.Print(); a = b; a.Print(); } }. , . , .
. , - .
, , , . :
[] [] this [ ] // [ ] { [get _] [set _]}. public, . .
, (get) (set) . get, set, . set, , get, .
, . .
, n- :
class DemoFib { public int this[int i] //, { get { if (i <=0) throw new Exception(" "); else if (i==1 || i==2) return 1; else {int a=1, b=1, c; for (int j=3; j<=i; ++j) { c=a+b; a=b; b=c; } return b; } } } } class Program { static void Main() { Console.Write("n="); int n=int.Parse(Console.ReadLine()); DemoFib a=new DemoFib(); try { Console.WriteLine("a[{0}]={1}",n,a[n]); } catch (Exception e) { Console.WriteLine(e.Message); } } }, - . -, [0, 100]. , , .
class DemoArray { int[] MyArray;// public DemoArray(int size)// { MyArray = new int[size]; } public int LengthArray //, { get { return MyArray.Length; } } public int this[int i] // { get { if (i <0 || i >= MyArray.Length) throw new Exception(" "); else return MyArray[i]; } set { if (i <0 || i >= MyArray.Length) throw new Exception(" "); else if (value >= 0 && value <= 100) MyArray[i] = value; else throw new Exception(" "); } } } class Program { static void Main() { DemoArray a = new DemoArray(10); for (int i=0; i<a.LengthArray; i++) { a[i] = i * i; // Console.Write(a[i]+" ");// } Console.WriteLine(); try { //a[10]=100; //a[0]=200; } catch (Exception e) { Console.WriteLine(e.Message); } } }. try . , , : , .
# . . , .
class DemoArray { int[,] MyArray;// int n, m;// : public DemoArray(int sizeN, int sizeM)// { MyArray = new int[sizeN, sizeM]; this.n = sizeN; this.m = sizeM; } public int LengthN //, { get { return n; } } public int LengthM //, { get { return m; } } public int this[int i, int j] // { get { if (i < 0 || i >= n || j < 0 || j >= m) throw new Exception(" "); else return MyArray[i, j]; } set { if (i < 0 || i >= n || j < 0 || j >= m) throw new Exception(" "); else if (value >= 0 && value <= 100) MyArray[i, j] = value; else throw new Exception(" "); } } } class Program { static void Main() { DemoArray a = new DemoArray(3, 3); for (int i = 0; i < a.LengthN; i++,Console.WriteLine()) { for (int j = 0; j < a.LengthM; j++) { a[i, j] = i *j; // Console.Write("{0,5}", a[i, j]);// } } Console.WriteLine(); try { //Console.WriteLine(a[3,3]); //a[0,0]=200; } catch (Exception e) { Console.WriteLine(e.Message); } } }. try . , - , .
# , , . , :
newObject x, y, z;z = x+y; // , newObject. , . , .
, :
[ ] _{}public static. , - extern. -, , : .
:
- (public static);
- ( ref out);
- ;
- , , , ( ).
: + -! ~ ++ --, true false. , true, false, .
:
operator _ ():
public static int operator + (DemoArray m)public static DemoArray operator --(DemoArray m)public static bool operator true (DemoArray m), , , . :
- +, -,!, ~ ;
- ++, -- , ;
- true false bool.
. , , , , .
DemoArray, , :
- , - ;
- , - ;
- , ;
- , -;
- -;
- ( );
- ( 1);
- true false ( true, , , false).
. DemoArray ( ), ( )
: + - * / % & | ^ << >> ==!= < > <= >=. , .
:
operator _ (1, 2):
public static DemoArray operator + (DemoArray a, DemoArray b)public static bool operator == (DemoArray a, DemoArray b):
- , , , .
- .
- . , , . , , .
DemoArray, , +:
- 1: ;
- 2:
. DemoArray ( ) & ( , , true, false).