.


:




:

































 

 

 

 


: , , ,




- , , . "" , , , . 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.

, . , . , - . , - , .

( ) . :

  1. : .
  2. , , , .
  3. ( ).
  4. ( ).
  5. ( , , ).
  6. ( ).
  7. ( ).
  8. ( , ).
  9. ( , ).

, . , , , . , - , , :

, 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. . :

  1. , void.
  2. .
  3. - , , - 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) , - , . . , . :

class Demo{ static int a; static int b; private Demo(){} // static Demo() // { a=10; b=2; } 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 S=new Demo(); // Demo.Print(); }}

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 :

  1. Solution Explorer Hello ( ).

  1. 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 // } }}
  1. namespace ConsoleApplication1 namespace MyProgram, Program.cs Circle.cs .
  2. Circle Program.cs Circle.cs.
  3. , .

  1. Point, :
    • :
      • int x, y;
    • , :
      • ;
      • .
    • , :
      • ;
      • ;
      • (a, b).
    • :
      • - ( );
      • ( ).
  2. Triangle, :
    • :
      • int a, b, c;
    • , .
    • , :
      • ;
      • ;
      • .
    • :
      • - ( );
      • , ( ).
  3. Rectangle, :
    • :
      • int a, b;
    • , .
    • , :
      • ;
      • ;
      • .
    • :
      • - ( );
      • , ( ).
  4. Money, :
    • :
      • int first;//
      • int second; //
    • , .
    • , :
      • ;
      • , N .
      • , n .
    • :
      • - ( );
      • ( ).
  5. . :
    • :
      • int [] IntArray;
      • int n.
    • , n.
    • , :
      • ;
      • ;
      • .
    • :
      • ( );
      • ( ).
  6. . :
    • :
      • int [,] IntArray;
      • int n.
    • , n×n.
    • , :
      • ;
      • ;
      • i- .
    • :
      • ( );
      • ( ).
  7. . :
    • :
      • double [][] DoubelArray;
      • int n, m.
    • , n×m.
    • , :
      • ;
      • ;
      • .
    • :
      • ( );
      • ( ).
  8. o . :
    • :
      • StringBuilder Line;
      • int n.
    • , n .
    • , :
      • ;
      • ;
      • .
    • :
      • ( );
      • , , ( )
  9. . :
    • :
      • Regex r;
      • string text;
    • , :
      • , , ;
      • , ;
      • , ;
    • :
      • ( )
      • , ( )
  10. . :
    • DataTime data.
    • , :
      • 1.01.2009
    • , :
      • ;
      • ;
      • .
    • :
      • ( )
      • ( )

, :

  1. : , , , .
  2. .

: , , ,

# , , .

. , , . .

, . , , , ( , . .).

:

[] [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. -, , : .

:

  1. (public static);
  2. ( ref out);
  3. ;
  4. , , , ( ).

: + -! ~ ++ --, true false. , true, false, .

:

operator _ ()

:

public static int operator + (DemoArray m)public static DemoArray operator --(DemoArray m)public static bool operator true (DemoArray m)

, , , . :

  1. +, -,!, ~ ;
  2. ++, -- , ;
  3. true false bool.

. , , , , .

DemoArray, , :

  1. , - ;
  2. , - ;
  3. , ;
  4. , -;
  5. -;
  6. ( );
  7. ( 1);
  8. true false ( true, , , false).
class DemoArray { int[] MyArray;// public DemoArray(int size)// 1 { MyArray = new int[size]; } public DemoArray(params int[] arr)// 2 { MyArray = new int[arr.Length]; for (int i = 0; i < MyArray.Length; i++) MyArray[i] = arr[i]; } public int LengthArray //, { get { return MyArray.Length; } } public int this[int i] // { get { if (i < 0 || i >= MyArray.Length) throw new Exception(" "); return MyArray[i]; } set { if (i < 0 || i >= MyArray.Length) throw new Exception(" "); else MyArray[i] = value; } } public static DemoArray operator -(DemoArray x) // { DemoArray temp = new DemoArray(x.LengthArray); for (int i = 0; i < x.LengthArray; ++i) temp[i] = -x[i]; return temp; } public static DemoArray operator ++(DemoArray x) // { DemoArray temp = new DemoArray(x.LengthArray); for (int i = 0; i < x.LengthArray; ++i) temp[i] = x[i]+1; return temp; } public static bool operator true(DemoArray a) // true { foreach (int i in a.MyArray) { if (i<0) { return false; } } return true; } public static bool operator false(DemoArray a)// false { foreach (int i in a.MyArray) { if (i>0) { return true; } } return false; } public void Print(string name) // - - { Console.WriteLine(name + ": "); for (int i = 0; i < MyArray.Length; i++) Console.Write(MyArray[i] + " "); Console.WriteLine(); } } class Program { static void Main() { try { DemoArray Mas = new DemoArray(1, -4, 3, -5, 0); // 2 Mas.Print(" "); Console.WriteLine("\n "); DemoArray newMas=-Mas; // Mas.Print("M Mas"); // , newMas.Print(" newMas"); // Console.WriteLine("\n "); DemoArray Mas1=++Mas; Mas.Print("M Mas"); Mas1.Print("M Mas1=++Mas"); Console.WriteLine("\n "); DemoArray Mas2=Mas++; Mas.Print("M Mas"); Mas2.Print("M Mas2=Mas++"); if (Mas) Console.WriteLine("\n \n"); else Console.WriteLine("\n \n"); } catch (Exception e) { Console.WriteLine(e.Message); } }

. DemoArray ( ), ( )

: + - * / % & | ^ << >> ==!= < > <= >=. , .

:

operator _ (1, 2)

:

public static DemoArray operator + (DemoArray a, DemoArray b)public static bool operator == (DemoArray a, DemoArray b)

:

  1. , , , .
  2. .
  3. . , , . , , .

DemoArray, , +:

  • 1: ;
  • 2:
class DemoArray { public static DemoArray operator +(DemoArray x, int a) // 1 { DemoArray temp = new DemoArray(x.LengthArray); for (int i = 0; i < x.LengthArray; ++i) temp[i]=x[i]+a; return temp; } public static DemoArray operator +(DemoArray x, DemoArray y) // 2 { if (x.LengthArray == y.LengthArray) { DemoArray temp = new DemoArray(x.LengthArray); for (int i = 0; i < x.LengthArray; ++i) temp[i] = x[i] + y[i]; return temp; } else throw new Exception(" "); } } class Program { static void Main() { try { DemoArray a = new DemoArray(1, -4, 3, -5, 0); a.Print(" a"); DemoArray b=a+10; b.Print("\n b"); DemoArray c = a+b; c.Print("\n c"); } catch (Exception e) { Console.WriteLine(e.Message); } } }

. DemoArray ( ) & ( , , true, false).





:


: 2017-03-18; !; : 1000 |


:

:

, , .
==> ...

1539 - | 1441 -


© 2015-2024 lektsii.org - -

: 0.062 .