1
. .
#
:
- #: ;
- , , : , , ;
- # ;
- .
1. 1-6, . . . .
2. . . , . . - .
3. , -. .
4. , .
5. .
:
1. : , , , .
2. : , , , .
3. : , , , .
4. : , , , .
5. : , , , (, )
6. : , , - , .
7. : , , , .
8. : , , , .
9. : , , , .
10. : , , , .
11. : , , , .
12. : , , , - .
13. : , , - , .
C# , . Visual C#, . - ConsoleHello, , .
, , ConcoleHello.cs . 1.1. (Error List, Output), .
, , "", , ". .
|
|
1.1 - Visual Studio NET
:
namespace ConsoleHello { //
class Program { //
static void Main(string[] args) { // ,
//
Console.WriteLine(" ");
string name;
name = Console.ReadLine(); //
if (name == "") Console.WriteLine(", !");
else Console.WriteLine(", " + name + "!");
}
} }
#
- . , . : , :
[][]enum _
[: ] {__}
:
;
. ;
;
( long).
, VP, Manager, Grunt Contractor.
: | : |
nm m { Manager, // = 0 Grunt, // = 1 Contractor, // = 2 VP // = 3 } | enum EmpType { Manager = 102, Grunt, // = 103 Contractor, // = 104 VP // = 105 } |
# . ( ), . :
[][]struct _ [:_] {_}
1. C#
enum EmpType: byte {
Manager = 10, Grunt = 1,
Contractor = 100, VP = 9
}
struct Employee {
public EmpType title; //
public string name;
public short deptID:
}
class StructTester {
public static int Main(string[] args) {
Employee fred; //
fred.deptID = 40;
fred.name = "Fred";
fred.title = EmpType.Grunt;
return 0;
}}
:
struct Employee {
...
//
public Employee (EmpType et, string n, short d) {
title = et; name = n; deptID = d;
}}
:
class StructTester {
public static int Main(string[] args) {
Employee ma =new Employee (EmpType.VP, "Mary", 10);
return 0;
}}
. :
[][]class _ [:_] {_}
:
public - .
protected - , .
|
|
private - .
internal - .
public, . private protected .
: , , , , , , (, , ).
class Employee {
private string fullName;
private int empID;
private float currPay;
public Employee() {}
public Employee(string fullName, int empID, float currPay) {
this. fullName = fullName; this.empID = empID;
this. currPay = currPay;
}
//
public void GiveBonus(float amount)
{ currPay += amount; }
//
public virtual void DisplayStats() {
Console. WriteLine("Name: {0}", fullName):
Console.WriteLine(Pay: {0}", currPay):
Console.WriteLine(ID: {0}", empID);
}
}
public static void Main() {
Employee e = new Employee(Joe", 80, 30000);
e.GiveBonus(200):
Employee e2:
e2 = new Employee("Beth", 81, 50000);
e2.GiveBonus(1000);
e2.DisplayStats():
}
-. C# , . -. .
:
, (Read, Write);
, (Read, Write-once);
(Read-only);
(Write-only);
, (Not Read, Not Write).
Person, : fam, status, salary, age, health, , , , . .
2. C#
public class Person {
string fam="", status="", health="";
int age=0, salary=0;
public string Fam { //: Read,Write-once
set {if (fam == "") fam = value;}
get {return(fam);}
}
public string Status { //: Read-only
get {return(status);}
}
public int Age { //: Read,Write
set {
age = value;
if(age < 7) status ="";
else if(age <17) status ="";
else if (age < 22) status = "";
else status = "";
}
get {return(age);}
}
public int Salary { //: Write-only
set {salary = value;}
}
}
public void TestPersonProps(){
Person pers1 = new Person();
pers1.Fam = ""; pers1.Age = 21; pers1.Salary = 1000;
Console.WriteLine ("={0}, ={1}, ={2}", pers1.Fam, pers1.Age, pers1.Status);
pers1.Fam = ""; pers1.Age += 1;
Console.WriteLine ("={0}, ={1}, ={2}", pers1.Fam, pers1.Age, pers1.Status);
}
. , . . this.
Person children, , , :
3. C#
const int Child_Max = 10; //
Person[] children = new Person[Child_Max];
int count_children=0; //
public Person this[int i] { //
get {
if (i>=0 && i< count_children) return(children[i]);
else return(children[0]);
}
set {
if (i==count_children && i< Child_Max) {
|
|
children[i] = value; count_children++;
}
}}
public void TestPersonChildren(){
Person pers1 = new Person(), pers2 = new Person();
pers1.Fam = ""; pers1.Age = 42;
pers1.Salary = 10000; pers1[pers1.Count_children] = pers2;
pers2.Fam =""; pers2.Age = 21; pers2.Salary = 1000;
Console.WriteLine ("={0}, ={1}, ={2}", pers1.Fam, pers1.Age, pers1.Status);
Console.WriteLine ("={0}, ={1}, ={2}", pers1[0].Fam, pers1[0].Age, pers1[0].Status);
}
. , , . static. . , , .
, , static. , .
. - . - - , . , - . . C# , - .
Found, c:
4. Found
public class Found{
protected string name;
protected int credit;
public Found() { }
public Found(string name, int sum) {
this.name = name; credit = sum;
}
public virtual void VirtMethod() { //
Console.WriteLine (": " + this.ToString());
}
// Object
public override string ToString() {
return(String.Format(": name = {0}, credit = {1}", name, credit));
}
public void NonVirtMethod() {
Console.WriteLine (": " + this.ToString());
}
public void Analysis() {
Console.WriteLine (" ");
}
public void Work() {
VirtMethod();
NonVirtMethod();
Analysis();
}
}
Found Object ToString(), . override.
Derived - Found. - . , , , .
Derived protected int debet.
, , , , . , , . . , , , , .
public Derived(String name, int cred, int deb): base (name,cred)
{}
, , .