C# 4
Bu haftaki dersimizde nesneye yönelik programlamaya geciyoruz.Program içinde ayrı class lar acıyoruz ve bu classlar yolu ile programımızı calıştırıyoruz..
Ana classımız:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ders4
{
class Program
{
static void Main(string[] args) // Ana class içinde yapıılcak işlemleri cagırıyoruz
{
while(true){
rehber.nesneekle(); // rehberimize yeni nesne ekleme
rehber.nesneleriyazdir(); // rehberimizdeki nesneleri ekrana yazdırma
rehber.settelfon(5);
}
}
}
}
Diger classımız:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// pencere classımızda telefon rehberi haricinde örnek yapıyoruz.yeni nesneler oluşturup,toplam nesne sayısını ekrana yazdırıyoruz.Ayrıca verdigimiz kordinatların dönüşümünü yaptırıp ekrana yazdırıyoruz.
namespace ders4
{
class pencere
{
private int koordinatx;
private int koordinaty;
public static int nesnesayisi = 0;
public static void nesnesayisiniyazdir(){
Console.WriteLine(nesnesayisi)
public pencere(){
Console.WriteLine(“yeni nesne oluştu”);
nesnesayisi++;
}
public pencere(int koordinatx,int koordinaty){
this.koordinatx = koordinatx;
this.koordinaty = koordinaty;
nesnesayisi++;
}
public int Koordinatx{
get{return koordinatx;}
set{
if (value >= 0)
{
koordinatx = value;
}
}
}
public int Koordinaty
{
get { return koordinaty; }
set
{
if (value >= 0)
{
koordinaty = value;
}
}
}
public void ekranayaz()
{
Console.WriteLine(this.koordinatx + ” ” + this.koordinaty);
}
public void ekranayaz(int x){
for (int i = 0; i < x;i++ )
{
Console.WriteLine(this.koordinatx + ” ” + this.koordinaty);
}
}
}
}
Telefon rehberi classımız:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ders4
{
class rehber
{
private int id;
public int Id
{
get { return id; }
}
private string isim;
public string Isim
{
get { return isim; } // string halinde girilen ismi döndürür
}
private string telefon;
public string Telefon
{
get { return telefon; } // string halinde girilen telefon numarasını döndürür
set {
telefon = value;
}
}
private rehber next;
private static int nesnesayisi =0;
private rehber(string isim , string telefon){
nesnesayisi++;
id = nesnesayisi;
next = null;
this.isim = isim;
this.telefon = telefon;
}
private rehber()
{
nesnesayisi++;
id = nesnesayisi;
next = null;
Console.WriteLine(“İsim giriniz.”);
this.isim = Console.ReadLine();
Console.WriteLine(“Telefon Giriniz”);
this.telefon = Console.ReadLine();
}
public static rehber ilknesne = null;
public static void nesneekle(){
rehber temp;
if (ilknesne == null)
{
ilknesne = new rehber();
}else{
temp = ilknesne;
while(temp.next != null){
temp = temp.next;
}
temp.next = new rehber();
}
}
public static void nesneleriyazdir(){
rehber temp = ilknesne;
while(temp != null){
Console.WriteLine(temp.id + ” ” + temp.isim + ” ” + temp.telefon);
temp = temp.next;
}
}
public static void settelfon(int id){
rehber temp = ilknesne;
while (temp != null)
{
if (temp.id == id)
{
Console.WriteLine(“Telefon giriniz”);
temp.telefon = Console.ReadLine();
}
temp = temp.next;
}
}
}
}
C# 3
3.hafta yine console uygulamalarıyla devam ediyoruz..Ayrıca haftaya vize dönemimiz oldugu için kurs olmayacagından,kodlar bir hafta gecikecek
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ders3
{
class Program
{
public static int ekrandanintoku(){ // fonksyon ile ekrandan okuma
return Convert.ToInt32(Console.ReadLine());
}
public static void yazdir(){ // fonksyon ile ekrana yazdırma
Console.WriteLine(“merhaba”);
return;
}
public static int xkare(int x){ // fonksyon ile kare alma
return x * x;
}
public static void fonksyon(out int x){
x = 5;
}
public static int faktoriyel(int x){ //faktoriyel alma
if (x==0)
{
return 1;
}else{
return x * faktoriyel(x-1);
}
}
//param fonksyonun sınırsız parametre alınmasını saglar
public static void ekranayaz(string parametreler,params Object[] x){
int tamsayi;
double realsayi;
char karakter;
string katar;
int parametreindex = 0;
while (parametreler != “”)
{
if (parametreler.Substring(0, 1) == “%”)
{
parametreler = parametreler.Substring(1);
if (parametreler.Substring(0, 1) == “d”)
{
tamsayi = (int)x[parametreindex];
Console.Write(tamsayi);
parametreindex++;
parametreler = parametreler.Substring(1);
}
else if (parametreler.Substring(0, 1) == “c”)
{
karakter = (char)x[parametreindex];
Console.Write(karakter);
parametreindex++;
parametreler = parametreler.Substring(1);
}
else if (parametreler.Substring(0, 2) == “lf”)
{
realsayi = (double)x[parametreindex];
Console.Write(realsayi);
parametreindex++;
parametreler = parametreler.Substring(2);
}
else if (parametreler.Substring(0, 1) == “s”)
{
katar = (string)x[parametreindex];
Console.Write(katar);
parametreindex++;
parametreler = parametreler.Substring(1);
}
}
else if (parametreler.Substring(0, 1) == “\\”)
{
parametreler = parametreler.Substring(1);
if (parametreler.Substring(0, 1) == “n”)
{
Console.WriteLine();
parametreler = parametreler.Substring(1);
}
}
else
{
Console.Write(parametreler.Substring(0, 1));
parametreler = parametreler.Substring(1);
}
}
}
static void main(String[] args)
{
}
}
}