Upload
mladen991
View
246
Download
4
Embed Size (px)
DESCRIPTION
dopu
Citation preview
VI.
. .
, . , , . , , , . , . , . class. :
[attributes] [access-modifiers-
] class identifier- [:base-class- [(i)]]
{
class-body-
}
6.1 . , . C# :
public- ;
private- X, private ;protected- X, protected ;internal- X, internal ;protected internal- X, protected internal ;
. public class, . , :
public class Computer{public string Model; public string Proizvodjac; public int BrojProcesora; public int BrojUsbPortova; }
, , 6.1.
6.1:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Parametri
{
public class KlasaParamet
{
public void ParamMetod(int prviParametar, char drugiParametar, double treciParametar)
{
Console.WriteLine("Prikaz parametara: {0}, {1}, {2}", prviParametar,drugiParametar, treciParametar);
}
}
public class primParametri
{
static void Main()
{
int brojStudenata = 180;
char primKarakter = 'A';
double doubprom = 2036/3.33;
KlasaParamet kp = new KlasaParamet();
kp.ParamMetod(brojStudenata, primKarakter, doubprom);
}
}
}
ParamMetod int, char, double Console.WriteLine. Main(), ParamMetod. .
new . .
MojTip, MojTip :
// MojTipMojTipmojaprom;
// mojaprom=newMojTip ();
, , , -.
6.2 , , . , , . , . :
using system;
public class Smer
{
private string ImeSmera;.......public string GetImeSmera(){
return ImeSmera;
}
public void SetImeSmera( string s)
{
ImeSmera=s;
}
}
.
public static int Main(string[] args)
{
Smert s = new Smer();s.SetSmer("Projektovanje");Console.WriteLine("Smer je :"+d.GetSmer());
return 0;
}6.3
, . Monitor Computer Computer . , , Mouse , Computer Mouse . : . . . , . . , . Computer Icomputers GoStart, GoShutDown. interface, :
publicinterfaceIComputers{
}
:
publicinterfaceIComputers{voidGoStart();
voidGoShutDown();
}
:publicinterfaceIComputers{
//Dodatni clanovifloatRamMemory{
get;
//Za definisanjeread-writesvojstva dodati kod na ovom mestu}}
, . . . using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Osobine
{
public class Zaposleni
{
public static int BrojZaposlenih;
private static int brojac;
private string ime;
public string Name
{
get { return ime; }
set { ime = value; }
}
// Staticka osobina samo za citanje:
public static int Brojac
{
get { return brojac; }
}
// Konstruktor:
public Zaposleni()
{
// Izracunati broj zaposlenih:
brojac = ++brojac + BrojZaposlenih;
}
}
class Program
{
static void Main()
{
Zaposleni.BrojZaposlenih = 600;
Zaposleni e1 = new Zaposleni();
e1.Name = "Milos Markovic";
System.Console.WriteLine("Broj zaposlenog: {0}", Zaposleni.Brojac);
System.Console.WriteLine("Ime zaposlenog: {0}", e1.Name);
}
}
}6.4
, . , :
6.2:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Svojstva{
class TimePeriod
{
private double seconds;
public double Hours
{
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}
class Program
{
static void Main()
{
TimePeriod t = new TimePeriod();
// Dodeljivanje vrednosti Hours
t.Hours = 24;
// Ocenjivanje Hours svojstva.
System.Console.WriteLine("Vreme u casovima: " + t.Hours);
}
}
} : , , . , , get / set :
public class Date
{
private int month = 7;
public int Month
{
get{
return month;
}
set
{
if ((value > 0) && (value < 13))
{
month = value;
}
}
}
}
Month set Month 1 12. Month . 6.4.1 get
get , . get . get :class Person
{
private string name; // name public string Name // Name {
get
{
return name;
}
}
}
get :
class Employee
{
private string name;
public string Name
{
get
{
return name != null ? name : "NA";
}
}
}
Name NA.
6.4.2 set set , void. . set Name :class Person
{
private string name; // name public string Name // Name {
get
{
return name;
}
set
{
name = value;
}}
}
set, , :
Person person = new Person();
person.Name = "Marko"; // set System.Console.Write(person.Name); // get
, .
6.3:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Svojstva
{
public class Studenti{
public static int BrojStudenata;
private static int brojac;
private string ime;
// Svojstvo za citanje-pisanje:
public string Name
{
get { return ime; }
set { ime = value; }
}
// Staticko svojstvo samo za citanje:
public static int Brojac
{
get { return brojac; }
}
// Konstruktor:
public Studenti(){
// Izracunavanje broja studenata:
brojac = ++brojac + BrojStudenata;
}
}
class Program
{
static void Main()
{
Studenti.BrojStudenata = 200;
Studenti s = new Studenti();
s.Name = "Milan Markovic";
System.Console.WriteLine("Broj studenata: {0}", Studenti.Brojac);
System.Console.WriteLine("Ime studenata: {0}", s.Name);
}
}}
6.5
.
, .
new, , :
Class1 MojaKlasa = new Class1();
:
int myInt = new int();
, . public. Kompjuter, Kompjuter, inicijalizovanje bool - true - false. Kompjuter, WriteLine Console . 6.4:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PrimerKonstruktora
{
public class Kompjuter
{
public bool inicijalizovanje;
public Kompjuter()
{
inicijalizovanje = true;
}
}
class Konstruktor
{
static void Main()
{
Kompjuter k = new Kompjuter();
Console.WriteLine(k.inicijalizovanje);
}
}
}
default- . new.
private, :class PrevInic{
// Privatni konstruktor:
private PrevInic() { }
public static double e = Math.E; //2.71828...
}
61. - ?
2. .3. ?
4. , ?
5. ?
6. ?
7. set get?
8. ?
9. ?
VII.
, . .
struct :
public struct PrimerStruktura{
// Polja, svojstva, metodi I dogadjaji se unose ovde...
}
7.1
:
7.1
// struct1.csusing System;
struct SimpleStruct
{
private int xval;
public int X
{
get
{
return xval;
}
set
{
if (value < 100)
xval = value;
}
}
public void DisplayX()
{
Console.WriteLine("Sacuvana vrednost je: {0}", xval);
}
}
class TestClass
{
public static void Main()
{
SimpleStruct ss = new SimpleStruct();
ss.X = 5;
ss.DisplayX();
}
}
Point, Rectangle, Color. , Point , : 7.2:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Strukture
{
public struct CoOrds
{
public int x, y;
public CoOrds(int p1, int p2)
{
x = p1;
y = p2;
}
}
class Program
{
static void Main()
{
{
// Inicijalizacija:
CoOrds coords1 = new CoOrds();
CoOrds coords2 = new CoOrds(10, 10);
// Prikaz rezultata:
Console.Write("CoOrds 1: ");
Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
Console.Write("CoOrds 2: ");Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
// Ostaviti prozor konzole otvoren u Debug modu.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
} :
7.3:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Strukture
{
public struct CoOrds
{
public int x, y;
public CoOrds(int p1, int p2)
{
x = p1;
y = p2;
}
}
class Program
{
static void Main()
{
{
// Deklarisanje objekta:
CoOrds coords1;
// Inicijalizacija:
coords1.x = 10;
coords1.y = 20;
// Prikaz rezultata:
Console.Write("CoOrds 1: ");
Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
// Konzolni prozor mora biti otvoren u Debug modu.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();}
}
}
}7.2 : 7.4:struct RomanNumeral
{
private int value;
public RomanNumeral(int value) //konstruktor{
this.value = value;
}
static public implicit operator RomanNumeral(int value)
{
return new RomanNumeral(value);
}
static public implicit operator RomanNumeral(BinaryNumeral binary)
{
return new RomanNumeral((int)binary);
}
static public explicit operator int(RomanNumeral roman)
{
return roman.value;
}
static public implicit operator string(RomanNumeral roman)
{
return ("Ne primenjuje se konverzija u string");
}
}
struct BinaryNumeral
{
private int value;
public BinaryNumeral(int value) //konstruktor{
this.value = value;
}
static public implicit operator BinaryNumeral(int value)
{
return new BinaryNumeral(value);
}
static public explicit operator int(BinaryNumeral binary)
{
return (binary.value);
}
static public implicit operator string(BinaryNumeral binary)
{
return ("Ne primenjuje se konverzija u string ");
}
}
class TestConversions
{
static void Main()
{
RomanNumeral roman;
BinaryNumeral binary;
roman = 10;
// Izvodi konverziju iz RomanNumeral u BinaryNumeral:
binary = (BinaryNumeral)(int)roman;
// Izvodi konverziju iz BinaryNumeral u RomanNumeral:
roman = binary;
System.Console.WriteLine((int)binary);
System.Console.WriteLine(binary);
// Ostaviti otvoren konzolni prozor.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
:
7.5:using System;
class TheClass
{
public int x;
}
struct TheStruct
{
public int x;
}
class TestClass
{
public static void structtaker(TheStruct s)
{
s.x = 5;
}
public static void classtaker(TheClass c)
{
c.x = 5;
}
public static void Main()
{
TheStruct a = new TheStruct();
TheClass b = new TheClass();
a.x = 1;
b.x = 1;
structtaker(a);
classtaker(b);
Console.WriteLine("a.x = {0}", a.x);
Console.WriteLine("b.x = {0}", b.x);
}
}
71. ?2. ?3. .
4. ?
5. ?
6. .VIII. 8.1
. : , ,
, :
Public void Greska()
Console.Write.Line(Ovo je poruka)
Console WriteLine, . Task list . , .
, Run-time . , . . , :publicfloatIzracunavanjeBrojaSekundi(floatBrojSekundi)
{
floatBrojSekundi;
BrojSekundi=24*1*360;
returnBrojSekundi;
}
8.2
. F11, Step Into Debug . Step Over Step Int, Step Over . F10 Step Over .
Start Out , Debug , Shift + F11. , , Run to cursor.
Set Next Statement next , .
Break .
. Debug Windows Breakpoints. Breakpoints .
Breakpoint : Name, Condition, Address. Condition Breakpoint Condition , . false true. , .
Hit Count New Breakpoint Breakpoint Hit Count , . Locals, Autos Watch.Locals : Name , Value , Type . Locals Value . . Value .8.3 1. C# project/solution ( project/solution Visual Studio ). 'File -> Open -> Project/Solution' ( Ctrl+Shift+O):
8.1: C# project/solution
8.2:
2. C# project/solution :
8.3:
3. setup project . 'File -> Add -> New Project...':
8.4: setup project
4. 'Project types' 'Other Projects -> Setup and Deployment' 'Setup Wizard' template:
8.5: setup wizard template-a
5. 5 .
#1:
8.6: #2:
8.7:
setup project . :
setup program :
setup Windows setup web :
merge Windows Installer CAB file
#3:
8.8:
. 'Primary output', DLL EXE.
8.9: Primary output-
8.10: HTML ReadMe . ReadMe :
8.11: ReadMe #5
8.12:
6. :
8.13:
7. setup . Setup1 'Solution Explorer' :
8. Start :
8.14:
9. :
8.15:
8.16:
10. :
8.17:
8.18: Setup 11. :
8.19:
8.20: Setup :
8.21:
12. :
8.22:
8.23: Setup
13. :
8.24:
8.25: Setup-a
14. 'Setup1', 'Build' ):
8.26: Build
81. ?2. ?
3. ?
4. .
5. Breakpoint ?
6. ?
7. .
IX. C# . (bugs) , (errors) (exceptions) .
, . . catch. , . System.Exception ArgumentNullException, OverflowException, InvalidCastException . try, . catch, :
9.1:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DeljenjeNulom{
class ExceptionTest
{
static double SafeDivision(double x, double y)
{
if (y == 0)
throw new System.DivideByZeroException();
return x / y;
}
static void Main()
{
// Unete ulazne velicine u cilju testiranja
double a = 98, b = 0;
double result = 0;
try
{
result = SafeDivision(a, b);
Console.WriteLine("{0} podeljeno sa {1} = {2}", a, b, result);
}
catch (DivideByZeroException e)
{
Console.WriteLine("Pokusaj deljenja nulom.");
}
}
}
} , throw, . switch case :switch(GodDoba)
{
case Leto:
// . . .slucaj Leto . .
return Leto;
case Prolece:
// . . .slucaj Prolece . . .
return Prolece;
case Zima:
// . . .slucaj Zima . . .
return Zima;
case Jesen:
// . . .slucaj Jesen . . .
default
} , ArgumentOutOfRangeException. .
9.2:public static void Main()
{ int z = 0;
try { int y = 20 + 3*z/z;
}
catch (ArithmeticException e)
{
Console.WriteLine("ArithmeticException Handler: {0}", e.ToString());
}}
catch, catch (Exception caught). Exception. . throw, :class CustomException : Exception{
public CustomException(string poruka){
}
}
private static void TestBacanje()
{
CustomException ex =
new CustomException("Uobicajeni izuzetak u TestBacanje ()");
throw ex;
}
, try . catch try . Catch , catch , catch , :
static void TestCatch(){
try
{
TestThrow();
}
catch (CustomException ex)
{
System.Console.WriteLine(ex.ToString());
}
}
, InvalidOperationException , :
class ProgramLog
{
System.IO.FileStream logFile = null;
void OpenLog(System.IO.FileInfo fileName, System.IO.FileMode mode) {}
void WriteLog()
{
if (!this.logFile.CanWrite)
{
throw new System.InvalidOperationException("Logfile ne moze biti samo za citanje");
}
// Upisati podatke u log datoteku.
}
}
ArgumentException . ArgumentException InnerException :static int GetValueFromArray(int[] array, int index)
{
try
{
return array[index];
}
catch (System.IndexOutOfRangeException ex)
{
System.ArgumentException argEx = new System.ArgumentException("Index is out of range", "index", ex);throw argEx;}
}
IndexOutOfRangeException , ArgumentOutOfRangeException :
class TestTryCatch
{
static int GetInt(int[] array, int index)
{
try
{
return array[index];
}
catch (System.IndexOutOfRangeException e)
{
System.Console.WriteLine(e.Message);
// Set IndexOutOfRangeException to the new exception's InnerException.
throw new System.ArgumentOutOfRangeException("index parameter is out of range.", e);
}
}
}
finally , , :
catch (exception)
{
Console.WriteLine("Izuzetak 1);
}catch (exception)
{
Console.WriteLine(Izuzetak 2);
}
catch (exception)
{
Console.WriteLine(Izuzetak 3);
}Finally{
Console.WriteLine("Izvrsavanje finalnog bloka.");
}
finally . :static void CodeWithoutCleanup()
{
System.IO.FileStream file = null;
System.IO.FileInfo fileInfo = new System.IO.FileInfo("C:\\primer.txt");
file = fileInfo.OpenWrite();
file.WriteByte(0xF);
file.Close();
} try-catch-finally :
static void CodeWithCleanup()
{
System.IO.FileStream file = null;
System.IO.FileInfo fileInfo = null;try
{
fileInfo = new System.IO.FileInfo("C:\\primer.txt");
file = fileInfo.OpenWrite();
file.WriteByte(0xF);
}
catch(System.UnauthorizedAccessException e)
{System.Console.WriteLine(e.Message);
}
finally
{
if (file != null)
{
file.Close();
}
}
}
91. ?2. ?
3. .
4. .
5. . .
6. finally.