Sunteți pe pagina 1din 10

ACTIVITĂȚI 3

METODE

1. Specificatorii de acces
2. Parametri formali și parametri actuali
3. Transferul prametrilor prin valoare și prin referință

1. Constructor/Destructor

1.

Consultați Cursul 05+Cursul 06.

1
Notițe METODE ACTIVITĂȚI 3

Specificatorii de acces

a) Introduceți și rulați programul de mai jos. Explicați specificatorii de


1 acces utilizați.

sing System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cursul04.ex3
{

class Persoana
{
protected string nume;
protected string prenume;
public Persoana(string nm, string pnm)
{
nume = nm;
prenume = pnm;
}
}

class Angajat : Persoana


{
private int anulAngajarii;
public Angajat(string nm, string pnm, int anang)
: base(nm, pnm)
{
anulAngajarii = anang;
}
public void AfiseazaNumeleIntregSiAnulAngajarii()
{
Console.WriteLine("Angajat: {0} {1} {2}", nume,
prenume, anulAngajarii);
}
}

class Program
{
static void Main(string[] args)
{
Angajat el = new Angajat("Popescu", "Ion", 1983);

2
ACTIVITĂȚI 3 METODE Notițe

el.AfiseazaNumeleIntregSiAnulAngajarii();

Console.WriteLine("Apasati orice tasta pentru a


terminare...");
Console.ReadKey();

}
}
}

b) Explicați definiția clasei Angajat


class Angajat : Persoana

c) Explicați sintaxa constructorului clasei Angajat


public Angajat(string nm, string pnm, int anang)
: base(nm, pnm)

d) Căutați prin bibliografia recomandată semnificația specificatorului static .

e) Adăugați linia de program prin care programul afișează un titlu sugestiv la


începutul execuției. Introduceți comentariile informative la început de program
sursa. Adăugați programul la Portofoliul de programe.

Parametri formali și parametri actuali

În programul următor se definește clasa class Dreptunghi, care are


2 metodele public bool Congruent(Dreptunghi obiect) și public
bool Echivalent(Dreptunghi obiect). Introduceți și executați
programul.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cursul04.ex4
{
class Dreptunghi
{
int l, L;
int arie;
public Dreptunghi(int i, int j)
{
l = i;

3
Notițe METODE ACTIVITĂȚI 3

L = j;
arie = l * L;
}
public bool Congruent(Dreptunghi obiect)
{
if ((obiect.l == l) & (obiect.L == L))
return true;
else
return false;
}
public bool Echivalent(Dreptunghi obiect)
{
if (obiect.arie == arie)
return true;
else
return false;
}

class Program
{
static void Main(string[] args)
{

Dreptunghi obiect1 = new Dreptunghi(2, 3);


Dreptunghi obiect2 = new Dreptunghi(2, 3);
Dreptunghi obiect3 = new Dreptunghi(1, 6);

Console.WriteLine("Afirmatia: <<obiectul1 este


congruent cu obiectul2>> este {0}", obiect1.Congruent(obiect2));
Console.WriteLine("Afirmatia << obiectul1 este
congruent cu obiectul3>> este {0}", obiect1.Congruent(obiect3));
Console.WriteLine("Afirmatia <<obiectul1 este
echivalent cu obiectul3>> este {0}", obiect1.Echivalent(obiect3));

Console.WriteLine("Apasati orice tasta pentru a


terminare...");
Console.ReadKey();

}
}
}
}

a) Care sunt și ce efecte au apelurile metodelor public bool


Congruent(Dreptunghi obiect) și public bool Echivalent(Dreptunghi
obiect). Care este tipul parametrilor actuali de apel ?

b) Care este efectul apelului constructorului public Dreptunghi(int i, int j)


? Ce tip au parametri formali respectiv de apel al constructorului ?

c) Adăugați linia de program prin care programul afișează un titlu sugestiv la

4
ACTIVITĂȚI 3 METODE Notițe

începutul execuției. Introduceți comentariile informative la început de program


sursa. Adăugați programul la Portofoliul de programe.

Transferul prametrilor prin valoare și prin referință

3 a) Introduceți și executați programul.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cursul05.ex1
{
class Point
{
public double x, y;
public Point(double a, double b)
{
x = a;
y = b;
}
}

class DemoTransferParametri
{
public void PrinValoare(double a, double b)
{
a = a + b;
b = -b;
}
public void PrinReferinta(Point punct)
{
punct.x = punct.x + punct.y;
punct.y = -punct.y;
}
}

class Program
{
static void Main(string[] args)
{

Point punct1 = new Point(10.0, 20.0);

DemoTransferParametri dtp = new

5
Notițe METODE ACTIVITĂȚI 3

DemoTransferParametri();

Console.WriteLine("\nx={0} si y={1} inainte de apelul


metodei PrinValoare",punct1.x,punct1.y);

dtp.PrinValoare(punct1.x, punct1.y);
Console.WriteLine("\nx={0} si y={1} dupa apelul metodei
PrinValoare", punct1.x, punct1.y);

dtp.PrinReferinta(punct1);
Console.WriteLine("\nx={0} si y={1} dupa apelul metodei
PrinRefeinta", punct1.x, punct1.y);

Console.WriteLine("\n\nApasati orice tasta pentru a


terminare...");
Console.ReadKey();
}
}
}

b) Care tipul parametrilor metodei public void PrinValoare(double a,


double b) ? Identificați în program apelul metodei și parametri de apel ai
acesteia.

c) Care tipul parametrilor metodei public void PrinReferinta(Point punct)


? Identificați în program apelul metodei și parametri de apel ai acesteia.

d) Explicați efectele celor două moduri de transfer ai parametrilor unei metode.

e) Adăugați linia de program prin care programul afișează un titlu sugestiv la


începutul execuției. Introduceți comentariile informative la început de program
sursa. Adăugați programul la Portofoliul de programe.

Constructor/Destructor

1 a) Introduceți și rulați programul următor. Explicați mecanismul de


colectare și eliberare automată a memoriei alocate obiectelor care nu
mai sunt necesare programului.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

6
ACTIVITĂȚI 3 METODE Notițe

using System.Threading.Tasks;

namespace cursul04.ex5
{

class Point
{
public double x;
public double y;
public Point(double a, double b)
{
x = a;
y = b;
}
~Point()
{
Console.WriteLine("\nDeleting point x={0} y={1}", x,
y);
}
public void GeneratePoint(double i, double j)
{
Point p = new Point(i, j);
}

class Program
{
static void Main(string[] args)
{
int n, m;
int maxPoints = 100;

Point pnm = new Point(0,0);

for (n=1;n<=maxPoints;n++)
for (m=1;m<=maxPoints;m++)
pnm.GeneratePoint(n,m);

Console.WriteLine("\nS-au generat {0} obiecte de clasa


Point!",maxPoints*maxPoints);
Console.WriteLine("\nLa iesirea din program acestea se
sterg automat si se executa destructorul ~Point()!");
Console.WriteLine("\nApasati orice tasta pentru a
iesire...");
Console.ReadKey();

}
}
}

7
Notițe METODE ACTIVITĂȚI 3

b) Ce rol are constructorul public Point(double a, double b)? Ce efect are


apelul metodei public void GeneratePoint(double i, double j) ? Unde
este apelată în programul principal ?

c) Ce rol are destructorul ~Point()? Când se declașează mecanismul de eliberare


automată a memoriei neutilizate ?

d) Adăugați linia de program prin care programul afișează un titlu sugestiv la


începutul execuției. Introduceți comentariile informative la început de program
sursa. Adăugați programul la Portofoliul de programe.

Ap1

1

8
ACTIVITĂȚI 3 METODE Notițe

Declaring Methods with Parameters


To pass information necessary for our method we use the parameters list. As
was already mentioned, we must place it between the brackets following the
method name, in method the declaration:

static <return_type> <method_name>(<parameters_list>)


{
// Method's body
}

The parameters list <parameters_list> is a list with zero or more declarations


of variables, separated by a comma, so that they will be used for the
implementation of the method’s logic:

<parameters_list> = [<type1> <name1>[, <typei> <namei>]],


where i = 2, 3, …

When we create a method, and we need certain information to develop the


particular algorithm, we choose that variable from the list, which is of type
<typei> and so we use it by its name <namei>.
The parameters from the list can be of any type. They can be primitive types
(int, double, …) or object types (for example string or array – int[],
double[], string[], …).

Rules to Name a Method


It is recommended, when declare a method, to follow the rules for method naming
suggested by Microsoft:
- The name of a method must start with capital letter.
- The PascalCase rule must be applied, i.e. each new word, that concatenates
so to form the method name, must start with capital letter.
- It is recommended that the method name must consist of verb, or verb and
noun.
Note that these rules are not mandatory, but recommendable. If we aim our C# code
to follow the style of all good programmers over the globe, we must use Microsoft’s
code convention.
Here some examples for well named methods:

Print
GetName
PlayMusic
SetUserName
And some examples for bad named methods:

Abc11

9
Notițe METODE ACTIVITĂȚI 3

Yellow___Black
foo
_Bar
It is very important that the method name describes the method’s purpose. All behind
this idea is that when a person that is not familiar with our program reads the method
name, they can easily understand what that method does, without the need to look at
the method’s source code.

To name a method it is good to follow these rules:


- Method name must describe the method’s purpose.
- Method name must begin with capital letter.
- The PascalCase rule must be applied.
- The method name must consist of verb, or verb and
noun.

10

S-ar putea să vă placă și