Manufaktur industri
Industri Internet of Things | bahan industri | Pemeliharaan dan Perbaikan Peralatan | Pemrograman industri |
home  MfgRobots >> Manufaktur industri >  >> Industrial programming >> Bahasa C

C# - Kelas

Saat Anda mendefinisikan kelas, Anda mendefinisikan cetak biru untuk tipe data. Ini sebenarnya tidak mendefinisikan data apa pun, tetapi mendefinisikan apa arti nama kelas. Yaitu, apa yang terdiri dari objek kelas dan operasi apa yang dapat dilakukan pada objek itu. Objek adalah instance dari kelas. Metode dan variabel yang membentuk kelas disebut anggota kelas.

Mendefinisikan Kelas

Definisi kelas dimulai dengan kata kunci kelas diikuti dengan nama kelas; dan badan kelas diapit oleh sepasang kurung kurawal. Berikut ini adalah bentuk umum dari definisi kelas

<access specifier> class  class_name {
   // member variables
   <access specifier> <data type> variable1;
   <access specifier> <data type> variable2;
   ...
   <access specifier> <data type> variableN;
   // member methods
   <access specifier> <return type> method1(parameter_list) {
      // method body
   }
   <access specifier> <return type> method2(parameter_list) {
      // method body
   }
   ...
   <access specifier> <return type> methodN(parameter_list) {
      // method body
   }
}

Catatan

Contoh berikut mengilustrasikan konsep yang dibahas sejauh ini

Demo Langsung
using System;

namespace BoxApplication {
   class Box {
      public double length;   // Length of a box
      public double breadth;  // Breadth of a box
      public double height;   // Height of a box
   }
   class Boxtester {
      static void Main(string[] args) {
         Box Box1 = new Box();   // Declare Box1 of type Box
         Box Box2 = new Box();   // Declare Box2 of type Box
         double volume = 0.0;    // Store the volume of a box here

         // box 1 specification
         Box1.height = 5.0;
         Box1.length = 6.0;
         Box1.breadth = 7.0;

         // box 2 specification
         Box2.height = 10.0;
         Box2.length = 12.0;
         Box2.breadth = 13.0;
           
         // volume of box 1
         volume = Box1.height * Box1.length * Box1.breadth;
         Console.WriteLine("Volume of Box1 : {0}",  volume);

         // volume of box 2
         volume = Box2.height * Box2.length * Box2.breadth;
         Console.WriteLine("Volume of Box2 : {0}", volume);
         Console.ReadKey();
      }
   }
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Volume of Box1 : 210
Volume of Box2 : 1560

Fungsi dan Enkapsulasi Anggota

Fungsi anggota kelas adalah fungsi yang memiliki definisi atau prototipe dalam definisi kelas yang mirip dengan variabel lain. Ini beroperasi pada objek apa pun dari kelas yang menjadi anggotanya, dan memiliki akses ke semua anggota kelas untuk objek itu.

Variabel anggota adalah atribut suatu objek (dari perspektif desain) dan mereka dirahasiakan untuk mengimplementasikan enkapsulasi. Variabel ini hanya dapat diakses menggunakan fungsi anggota publik.

Mari kita letakkan konsep di atas untuk mengatur dan mendapatkan nilai dari anggota kelas yang berbeda dalam sebuah kelas

Demo Langsung
using System;

namespace BoxApplication {
   class Box {
      private double length;   // Length of a box
      private double breadth;  // Breadth of a box
      private double height;   // Height of a box
      
      public void setLength( double len ) {
         length = len;
      }
      public void setBreadth( double bre ) {
         breadth = bre;
      }
      public void setHeight( double hei ) {
         height = hei;
      }
      public double getVolume() {
         return length * breadth * height;
      }
   }
   class Boxtester {
      static void Main(string[] args) {
         Box Box1 = new Box();   // Declare Box1 of type Box
         Box Box2 = new Box();
         double volume;
         
         // Declare Box2 of type Box
         // box 1 specification
         Box1.setLength(6.0);
         Box1.setBreadth(7.0);
         Box1.setHeight(5.0);
         
         // box 2 specification
         Box2.setLength(12.0);
         Box2.setBreadth(13.0);
         Box2.setHeight(10.0);
         
         // volume of box 1
         volume = Box1.getVolume();
         Console.WriteLine("Volume of Box1 : {0}" ,volume);
         
         // volume of box 2
         volume = Box2.getVolume();
         Console.WriteLine("Volume of Box2 : {0}", volume);
         
         Console.ReadKey();
      }
   }
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Volume of Box1 : 210
Volume of Box2 : 1560

Konstruktor C#

Sebuah konstruktor class kelas adalah fungsi anggota khusus dari kelas yang dieksekusi setiap kali kita membuat objek baru dari kelas tersebut.

Konstruktor memiliki nama yang persis sama dengan kelas dan tidak memiliki tipe pengembalian. Contoh berikut menjelaskan konsep konstruktor

Demo Langsung
using System;

namespace LineApplication {
   class Line {
      private double length;   // Length of a line
      
      public Line() {
         Console.WriteLine("Object is being created");
      }
      public void setLength( double len ) {
         length = len;
      }
      public double getLength() {
         return length;
      }

      static void Main(string[] args) {
         Line line = new Line();    
         
         // set line length
         line.setLength(6.0);
         Console.WriteLine("Length of line : {0}", line.getLength());
         Console.ReadKey();
      }
   }
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Object is being created
Length of line : 6

Sebuah konstruktor default tidak memiliki parameter apa pun tetapi jika Anda membutuhkannya, konstruktor dapat memiliki parameter. Konstruktor seperti itu disebut konstruktor berparameter . Teknik ini membantu Anda untuk menetapkan nilai awal ke suatu objek pada saat pembuatannya seperti yang ditunjukkan pada contoh berikut −

Demo Langsung
using System;

namespace LineApplication {
   class Line {
      private double length;   // Length of a line
      
      public Line(double len) {  //Parameterized constructor
         Console.WriteLine("Object is being created, length = {0}", len);
         length = len;
      }
      public void setLength( double len ) {
         length = len;
      }
      public double getLength() {
         return length;
      }
      static void Main(string[] args) {
         Line line = new Line(10.0);
         Console.WriteLine("Length of line : {0}", line.getLength()); 
         
         // set line length
         line.setLength(6.0);
         Console.WriteLine("Length of line : {0}", line.getLength()); 
         Console.ReadKey();
      }
   }
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Object is being created, length = 10
Length of line : 10
Length of line : 6

Penghancur C#

penghancur adalah fungsi anggota khusus dari kelas yang dieksekusi setiap kali objek kelasnya keluar dari ruang lingkup. penghancur memiliki nama yang persis sama dengan kelas dengan awalan tilde (~) dan tidak dapat mengembalikan nilai atau mengambil parameter apa pun.

Destructor bisa sangat berguna untuk melepaskan sumber daya memori sebelum keluar dari program. Penghancur tidak dapat diwariskan atau kelebihan beban.

Contoh berikut menjelaskan konsep destruktor −

Demo Langsung
using System;

namespace LineApplication {
   class Line {
      private double length;   // Length of a line
      
      public Line() {   // constructor
         Console.WriteLine("Object is being created");
      }
      ~Line() {   //destructor
         Console.WriteLine("Object is being deleted");
      }
      public void setLength( double len ) {
         length = len;
      }
      public double getLength() {
         return length;
      }
      static void Main(string[] args) {
         Line line = new Line();

         // set line length
         line.setLength(6.0);
         Console.WriteLine("Length of line : {0}", line.getLength());           
      }
   }
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Object is being created
Length of line : 6
Object is being deleted

Anggota Statis Kelas C#

Kita dapat mendefinisikan anggota kelas sebagai statis menggunakan statis kata kunci. Ketika kita mendeklarasikan anggota kelas sebagai statis, itu berarti tidak peduli berapa banyak objek kelas yang dibuat, hanya ada satu salinan anggota statis.

Kata kunci statis menyiratkan bahwa hanya satu instance dari anggota yang ada untuk suatu kelas. Variabel statis digunakan untuk mendefinisikan konstanta karena nilainya dapat diambil dengan memanggil kelas tanpa membuat turunannya. Variabel statis dapat diinisialisasi di luar fungsi anggota atau definisi kelas. Anda juga dapat menginisialisasi variabel statis di dalam definisi kelas.

Contoh berikut menunjukkan penggunaan variabel statis

Demo Langsung
using System;

namespace StaticVarApplication {
   class StaticVar {
      public static int num;
      
      public void count() {
         num++;
      }
      public int getNum() {
         return num;
      }
   }
   class StaticTester {
      static void Main(string[] args) {
         StaticVar s1 = new StaticVar();
         StaticVar s2 = new StaticVar();
         
         s1.count();
         s1.count();
         s1.count();
         
         s2.count();
         s2.count();
         s2.count();
         
         Console.WriteLine("Variable num for s1: {0}", s1.getNum());
         Console.WriteLine("Variable num for s2: {0}", s2.getNum());
         Console.ReadKey();
      }
   }
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Variable num for s1: 6
Variable num for s2: 6

Anda juga dapat mendeklarasikan fungsi anggota sebagai statis . Fungsi tersebut hanya dapat mengakses variabel statis. Fungsi statis ada bahkan sebelum objek dibuat. Contoh berikut menunjukkan penggunaan fungsi statis

Demo Langsung
using System;

namespace StaticVarApplication {
   class StaticVar {
      public static int num;
      
      public void count() {
         num++;
      }
      public static int getNum() {
         return num;
      }
   }
   class StaticTester {
      static void Main(string[] args) {
         StaticVar s = new StaticVar();
         
         s.count();
         s.count();
         s.count();
         
         Console.WriteLine("Variable num: {0}", StaticVar.getNum());
         Console.ReadKey();
      }
   }
}

Ketika kode di atas dikompilasi dan dieksekusi, menghasilkan hasil sebagai berikut

Variable num: 3

Bahasa C

  1. C# Kelas dan Objek
  2. C# Akses Pengubah
  3. C# Kata Kunci statis
  4. C# kelas abstrak dan metode
  5. C# Kelas Bersarang
  6. Kelas dan Objek C++
  7. C - Kelas Penyimpanan
  8. Kelas Penyimpanan di C++
  9. Antarmuka dalam C++ (Kelas Abstrak)
  10. C# - Struktur Program