9
Платформата Microsoft .NET и езикът на програмиране C#

NET/C#_6

Embed Size (px)

Citation preview

Page 1: NET/C#_6

Платформата Microsoft .NET иезикът на програмиране C#

Page 2: NET/C#_6

Пространство от имена - namespace

Page 3: NET/C#_6

Класовеpublic class Person {

private string name;private int age;public Person(String name, int age) {

this.name = name;this.age = age;

}public void show() { Console.WriteLine(name + “ ” + age);

}public void showAgeGroup() { if (age<14) Console.WriteLine(name + “is a child”);else if (age<18) Console.WriteLine(name + “is an adolescent”);else Console.WriteLine(name + “is an adult”);

}}

Page 4: NET/C#_6

Създаване и използване на обекти от клас

Person person1 = new Person(“John”, 15);person1.show();person1.showAgeGroup();

Page 5: NET/C#_6

Наследяване на класове

public class Employee : Person {private int salary;public Employee(String name, int age, int salary) {

this.name = name;this.age = age;this.salary = salary;

}public void showSalaryGroup() {if (salary<1000) Console.WriteLine(name + “’s salary is below average”);else Console.WriteLine(name + “’s salary is above average”);}

}

Page 6: NET/C#_6

Композиция на класове

public class Company {private string name;private List<Employee> employees;public Company(String name) {

this.name = name; employees = new List<Employee>();}public void addEmployee(Employee employee) { employees.add(employee);

}public void showEmployees() { Console.WriteLine(name + “has the following employees”);foreach (Employee employee in employees) employee.show();

}}

Page 7: NET/C#_6

Изключения

public Person(String name, int age) {if (age<0) throw new ArgumentException(“Age must be non

negative”);this.name = name;this.age = age;

}

Page 8: NET/C#_6

namespace

Групира код със сходна функционалностnamespace SampleNamespace{ class SampleClass { } interface SampleInterface { } struct SampleStruct { } enum SampleEnum { a, b } delegate void SampleDelegate(int i); namespace SampleNamespace.Nested { class SampleClass2 { } }}

Page 9: NET/C#_6

using

System.Console.WriteLine("Hello, World!");

using System; Console.WriteLine("Hello, World!");