Documente Academic
Documente Profesional
Documente Cultură
LIMBAJE DE PROGRAMARE II
Obiective curs
Bibliografie obligatorie:
1
D.A.Crisan. Limbajul C#. Vol II. Programarea orientată obiect
ATENTIE
2
CAPITOLUL 1. CLASE ȘI OBIECTE
Cuprins:
CLASE ȘI OBIECTE ..........................................................................4
SUPRAÎNCĂRCAREA OPERATORILOR. CONVERSII CE
IMPLICĂ OBIECTE ALE CLASELOR. INDEXATORI ................................................14
DERIVAREA CLASELOR...............................................................18
POLIMORFISM ................................................................................22
CLASE ABSTRACTE. INTERFEȚE ...............................................24
CLASE GENERICE ..........................................................................26
3
D.A.Crisan. Limbajul C#. Vol II. Programarea orientată obiect
CLASE ȘI OBIECTE
1.1 Noțiuni generale
class Student
{
//câmpuri
string nume;
int gr;
double nota;
//metode
double MarireNota(double bonus){... }
int MutareGrupa (int grupa_noua){... }
}
4
CAPITOLUL 1. CLASE ȘI OBIECTE
1.2 Încapsularea
Ex2.
class Produs
{ private double _pret;
5
D.A.Crisan. Limbajul C#. Vol II. Programarea orientată obiect
Proprietăți auto-implementate
class Complex
{
public double Re { get; set; }
public double Im { get; se t; }
}
6
CAPITOLUL 1. CLASE ȘI OBIECTE
class Complex
{
public double re, im;
public Complex(double re = 0, double im = 0)
7
D.A.Crisan. Limbajul C#. Vol II. Programarea orientată obiect
{
this.re = re; this.im = im;
Console.WriteLine("constructor de instanta: {0}", this);
}
~Complex()
{
Console.WriteLine("destructor: {0}", this);
}
public override string ToString()
{
return string.Format("{0:N}+i*({1:N})", re, im);
}
}
static Student()
{
_contor = 0;
}
this.nr_matricol = nr_matricol;
this._nume = nume;
}
}
int[] V2 = { 1, 2 };
Console.WriteLine("Medie = " + V2.Medie());
Console.ReadKey();
}
}
return m / v.Length;
} } }
Evenimentele permit unei clase sau unor obiecte să notifice alte clase sau
obiecte atunci când are loc ceva care prezintă interes pentru ele.
TextBox textBox1;
textBox1.TextChanged+=new System.EventHandler(textBox1_TextChanged);
class Profesor
{
public string numeProfesor;
}
class Student
11
D.A.Crisan. Limbajul C#. Vol II. Programarea orientată obiect
{
string numeStudent; int nota;
Profesor profesorCoordonatorLicenta;
}
using System;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
12
CAPITOLUL 1. CLASE ȘI OBIECTE
{
public Form1()
{
InitializeComponent();
}
13
D.A.Crisan. Limbajul C#. Vol II. Programarea orientată obiect
SUPRAÎNCĂRCAREA OPERATORILOR.
CONVERSII CE IMPLICĂ OBIECTE ALE CLASELOR.
INDEXATORI
15
D.A.Crisan. Limbajul C#. Vol II. Programarea orientată obiect
2.3 Indexatori
class Student
{
private string _nume;
private int _grupa;
private List<double> _note;
17
D.A.Crisan. Limbajul C#. Vol II. Programarea orientată obiect
DERIVAREA CLASELOR
class Student
{
string nume;
int grupa;
public Student(Student s)
{
this.nume = s.nume;
this.grupa = s.grupa;
18
CAPITOLUL 3. DERIVAREA CLASELOR
~Student()
{
Console.WriteLine("Destructor clasa Student:" + this.nume + "," +
this.grupa);
}
}
~Bursier()
{
Console.WriteLine("Destructor clasa Bursier: + " + this.bursa);
}
}
{
Bursier b = new Bursier("ionel", 616, 200);
}
Aplicația afișează:
20
CAPITOLUL 3. DERIVAREA CLASELOR
B b; D d;
d=b; //conversie de la B la D
21
POLIMORFISM
4.1 Metode virtuale
class Student
{
string nume;
int grupa;
22
0
class Program
{
static void Main(string[] args)
{
Bursier b = new Bursier("ionel", 616, 200);
Student s = b;
s.Afisare();
Console.ReadKey();
}
}
Se va afișa:
Studentul ionel este in grupa 616 si are bursa in cuantum de 200 lei
23
CLASE ABSTRACTE. INTERFEȚE
5.1 Metode abstracte
O metodă abstractă –
24
0
class Student:IComparable
Grupul de programatori
25
26
CLASE GENERICE
6.1 Programarea generică
Clasa generică Mulțime
27