1 / 19

S ınıflar, Nesneler ve M e todlar

S ınıflar, Nesneler ve M e todlar. Sınıflar ve Nesneler. Sınıflar (Class) nesneleri tanımlamak ve bu nesneler üzerinde çalışacak metodları sağlamak için kullanılırlar. Aynı zamanda bu nesnelerin deklere edildiği ve problemleri çözmek için işlendiği programlardır. Class.

Télécharger la présentation

S ınıflar, Nesneler ve M e todlar

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sınıflar, Nesneler ve Metodlar

  2. Sınıflar ve Nesneler • Sınıflar (Class) nesneleri tanımlamak ve bu nesneler üzerinde çalışacak metodları sağlamak için kullanılırlar. • Aynı zamanda bu nesnelerin deklere edildiği ve problemleri çözmek için işlendiği programlardır.

  3. Class • Class—Bir nesne türünün tanımlandığı program • Belirli nesneleri inşa etmek için yazılan plan gibi düşünülebilir • Örnek: Otomobil sınıfı • Otomobil tanımına uyan herhangi bir nesne bu sınıfın bir örneğidir. • Java Class o sınıfın ne tür verilere sahip olduğunu anlatır • Her nesne aynı veri elemanlarına sahiptir fakat bunların değerleri farklıdır • Java Class her nesnenin hangi metodlara sahip olacağını anlatır • Aynı sınıftaki nesneler aynı metodlara sahiptirler

  4. Sınıflar Birinci Nesne: Nesne adı: alininArabası Yakıt miktarı: 10 litre hızı: 55 km/saat Plaka no: “23 ZZ 4321” Otomobil sınıfının örnekleri olan nesneler Sınıf Tanımı İkinci Nesne: Nesne adı: ayseninArabası Yakıt miktarı: 15litre hızı:85 km/saat Plaka no: “06 AB 1234” Sınıf Adı:Otomobil Veriler: Yakıt miktarı ________ hız ________ Plaka no ________ Metodlar (yapılacak işler): hızıArttır: Nasıl:Gaz pedalına bas. dur: Nasıl:Fren pedalına bas. Üçüncü Nesne: Nesne adı: fatmanınArabası Yakıt miktarı:0.5litre hızı:15 km/saat Plaka no: “23 CD 456”

  5. Nesneler • Nesneler değişkenlerdir; bunlar bir sınıfın özel bir isimle adlandırılan örnekleridir • Nesnelerin tipleri ait oldukları sınıftır • Nesnelerin hem veri hem de metodları vardır • Veri elemanları ve metodları o nesnenin üyeleridir • Veri elemanları aynı zamanda alanlar veya değişkenler olarak da adlandırılr • Bir metodu uyarmak o metodu çağırmak veya çalıştırmak anlamına gelir • Bir nesnenin metodunu çağırmak için genel syntax: nesne_değişkeni_Ismi.metod() • nesne_değişkeni_Ismiçağıran objedir

  6. Örnek: String Class • Stringbir sınıftır • Bir dizi karakter depolar • lengthmetodu içerdiği karakter sayısını verir • Örnek: kullanıcı tarafından girilen karakterleri oku ve kaç karakter girildiğini yaz String girilen; girilen = klavye.nextLine(); System.out.println(girilen.length());

  7. Class Dosyaları • Herbir java class tanımlaması ayrı bir dosyada olmalıdır • class ve dosya için aynı isimler kullanılır, dosya ismine ".java" eklenir • Şimdilik programlarınızda gerekli bütün class dosyalarını aynı klasöre koyun.

  8. Nesne Değişkenleri public String isim; public int yas; Public String no; • publicbu değişkenlerin nasıl kullanılacağı konusunda herhangi bir kısıtlama olmadığını gösterir. • publicdeğişkenler nokta operatoru ile ulaşılabilir: Ogrenci.isim = “Musti”; • Bu değişkenler public yerine privateolmalıdır.

  9. Nesne oluşturmak • Syntax: class_Adınesne_Adı = new class_Adı(); • ÖrneğinOgrenci sınıfının bir nesnesini oluşturmak için Ogrenciveli =new Ogrenci();

  10. Metodların kullanımı • Metodlarbir nesnenin yapabileceği işlemlerdir • Bir metodu kullanmak için onu uyarır veya çağırırız metodçağrısı örneği: ogrenci1.getIsım() • İki temel metod türü: • Tek bir değer döndüren metodlar • voidmetodlar, işlem yapıp değer döndürmezler Çağıran nesne — hangi nesnenin işlemi yapacağını belirtir parametre listesi —bu örnekte herhangi bir parametre yok metod ismi—nesnenin hangi işlemi yapacağını belirtir

  11. Metodların return tipleri • Bütün metodların mutlaka return tipleri mutlaka belirtilmelidir. • Return tipleri: • primitif data tipi, char, int, double • class, String, Ogrenci, vb. • voideğer herhangi birşey döndürmüyorsa int next = klavye.nextInt();

  12. Return Statement public int sayac = 0; public int getSayac() { return sayac++; }

  13. Yerel-Global değişkenler ve Bloklar • Bir blok, karşılıklı kıvırcık parantezler arasındaki ifadelerin tamamına denir. • Blok içerisinde tanımlanan bir değişken sadece o blok içerisinde tanınır. • Bloğun çalışması bittiğinde o değişken de yokolur. • Yerel değişkenler blok dışından çağrılamazlar. • Bütün blokların dışında fakat sınıfın içinde tanımlanan değişkenler global değişkenler olarak adlandırılır.

  14. publicveprivate public • Başka class veya programlar public değişkenlere direkt olarak ulaşıp değiştirebilirler • Başka herhangi bir class veya program bir public metodu uyarabilir private • Sadece aynı class içindeki bir metod private değişkenlere ulaşabilir • Sadece aynı class içindeki bir metod bir private metodu uyarabilir Örnek değişkenler uygunsuz erişimi engellemek için privateolarak tanımlanmalıdır.

  15. Bilgi gizleme ve Sarma-Kapsülleme (Encapsulation) Bilgi Gizleme • Bir nesne içindeki veriyi gizleme • Direkt ulaşıma engel ol Encapsulation • Nesne ve Sınıflar kullan • Nesneler hem veri elemanarı hem de bu elemanlar üzerinde çalışacak metodlar içerir. • Nesne Tabanlı Programlamanın köşetaşları • Her ikisi de soyutlamanın birer türüdür Encapsulation, inheritance and polymorphism are the three fundamental principles of the OOP Sarma, Kalıtım ve çok işlevlilik

  16. Encapsulation is the ability of an object to be a container (or capsule) for related properties (ie. data variables) and methods (ie. functions). • Data hidingis the ability of objects to shield variables from external access. These private variables can only be seen or modified by use of object accessor and mutator methods. This permits validity checking at run time. Access to other object variables can be allowed but with tight control on how it is done. Methods can also be completely hidden from external use. Those that are made visible externally can only be called by using the object's front door (ie. there is no 'goto' branching concept).

  17. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An example of where this could be useful is with an employee records system. You could create a generic employee class with states and actions that are common to all employees. Then more specific classes could be defined for salaried, commissioned and hourly employees. The generic class is known as the parent (or superclass or base class) and the specific classes as children (or subclasses or derived classes). The concept of inheritance greatly enhances the ability to reuse code as well as making design a much simpler and cleaner process. • Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. Overloading and overriding are two types of polymorphism . Now we will look at the third type: dynamic method binding. • Overloaded methodsare methods with the same name signature but either a different number of parameters or different types in the parameter list. For example 'spinning' a number may mean increase it, 'spinning' an image may mean rotate it by 90 degrees. By defining a method for handling each type of parameter you achieve the effect that you want. • Overridden methodsare methods that are redefined within an inherited or subclass. They have the same signature and the subclass definition is used.

  18. SummaryPart 1 • Classes have instance variables to store data and methods to perform actions • Declare instance variables to be private so they can be accessed only within the same class • There are two kinds of methods: those that return a value and void-methods • Methods can have parameters of both primitive type and class type

  19. SummaryPart 2 • Parameters of a primitive type work differently than those of a class type • primitive type parameters are call-by-value, so the calling object's variable is protected within the called method (the called method cannot change it) • class type parameters pass the address of the calling object so it is unprotected (the called method can change it) • For similar reasons, the operators = and == do not behave the same for class types as they do for primitive types (they operate on the address of object and not its values) • Therefor you should usually define an equals method for classes you define (to allow the values of objects to be compared)

More Related