Lecture 2 Introduction to C# - Object Oriented Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1
class Mahasiswa { public String NRP {get; set; } public String Nama {get; set; } public int energy {get; set; } public Mahasiswa(){ } public void Kuliah(){ energy--; } Mahasiswa mhs = new Mahasiswa(); 2 © Sekolah Tinggi Teknik Surabaya Constructor Property Method
3 © Sekolah Tinggi Teknik Surabaya » A class defined within another class is called nested. class Container { class Nested { // Add code here. } Container.Nested nestedInstance = new Container.Nested();
class Bangun{ public int Keliling {get; set;} public Bangun(int sisi){ //persegi Keliling=sisi*4; } public Bangun(int panjang, int lebar){ //balok Keliling=2*(panjang+lebar); } public Bangun(int a,int b,int c){ //segitiga Keliling=a+b+c; } 4 © Sekolah Tinggi Teknik Surabaya Same Name, Different Parameter
struct Mahasiswa { public string NRP; public string Nama; public int energy; public Mahasiswa(string a, string b, int c){ NRP = a; Nama = b; energy = c; } Mahasiswa mhs; mhs = new Mahasiswa(); mhs = new Mahasiswa("123", "456", 10); mhs.energy = 96; 5 © Sekolah Tinggi Teknik Surabaya
» Fields cannot be initialized within struct (except const or static ). » Structs can declare constructors that have parameters, but not default constructor (a constructor without parameters) or a destructor. » Structs are value types and classes are reference types. » Unlike classes, structs can be instantiated without using a new operator. 6 © Sekolah Tinggi Teknik Surabaya
» A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object. » A struct can implement interfaces. » A struct can be used as a nullable type and can be assigned a null value. 7 © Sekolah Tinggi Teknik Surabaya
8 ModifierDescription privatecan be accessed only by code in the same class or struct protected can be accessed only by code in the same class or struct, or in a class that is derived from that class internal can be accessed by any code in the same assembly, but not from another assembly protected internal can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly public can be accessed by any other code in the same assembly or another assembly that references it
» Classes and structs that are declared directly within a namespace is internal if no access modifier is specified » The access level for class members and struct members, including nested classes and structs, is private by default 9 © Sekolah Tinggi Teknik Surabaya
» Struct Instances vs. Class Instances ˃Struct: value type ˃Class: reference type » Object Identity vs. Value Equality ˃ Equals method 10 © Sekolah Tinggi Teknik Surabaya
public struct Person { internal string Name; internal int Age; internal Person(string name, int age) { Name = name; Age = age; } Person p1 = new Person("Him", 25); Person p2 = new Person("Him", 25); if (p1.Equals(p2)) { Console.WriteLine("Equal"); // < } Else { Console.WriteLine("Not Equal"); } Console.ReadKey(); 11 © Sekolah Tinggi Teknik Surabaya
public class _Person { internal string Name { get; set; }; internal int Age { get; set; }; internal _Person(string name, int age) { Name = name; Age = age; } _Person p1 = new _Person("Him", 25); _Person p2 = new _Person("Him", 25); if (p1.Equals(p2)) { Console.WriteLine("Equal"); } Else { Console.WriteLine("Not Equal"); // < } Console.ReadKey(); 12 © Sekolah Tinggi Teknik Surabaya
13 © Sekolah Tinggi Teknik Surabaya
14 © Sekolah Tinggi Teknik Surabaya class Manusia { } class Mahasiswa:Manusia { }
class Manusia { } class Mahasiswa:Manusia { } class Asisten:Manusia { } Manusia m = new Mahasiswa(); m = new Asisten(); 15 © Sekolah Tinggi Teknik Surabaya
» Mark method to be overridden with virtual keyword ˃Can only be placed on methods and properties » Mark method that override base class with override keyword ˃Can only be placed on methods and properties » If you want to hide (ignore) base class method, use new keyword ˃Can be placed on class members 16 © Sekolah Tinggi Teknik Surabaya
public class BaseClass { public virtual void DoWork() { } public virtual int WorkProperty { get { return 0; } } public class DerivedClass : BaseClass { public override void DoWork() { } public override int WorkProperty { get { return 0; } } DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method. BaseClass A = (BaseClass)B; A.DoWork(); // Also calls the new method. 17 © Sekolah Tinggi Teknik Surabaya
public class BaseClass { public void DoWork() { WorkField+=10; } public int WorkField; } public class DerivedClass : BaseClass { public new void DoWork() { WorkField+=100; } public new int WorkField; } DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method. BaseClass A = (BaseClass)B; A.DoWork(); // Calls the old method. 18 © Sekolah Tinggi Teknik Surabaya
public class A { public virtual void DoWork() { } } public class B : A { public override void DoWork() { } } public class C : B { public sealed override void DoWork() { } } public class D : C { public new void DoWork() { } } 19 © Sekolah Tinggi Teknik Surabaya
» Interface Statement interface ISampleInterface { void doSomething(); } » Class implement interface class SampleClass : ISampleInterface { void doSomething() { // Method implementation. } 20 © Sekolah Tinggi Teknik Surabaya
» Classes, structures, interfaces and methods in the.NET Framework can type parameters that define types of objects that they can store or use. » The most common example of generics is a collection, where you can specify the type of objects to be stored in a collection. 21 © Sekolah Tinggi Teknik Surabaya
» To define a generic class: Public class ClassKu { public T isi; } » To create an instance of a generic class: ClassKu mine = new ClassKu (); mine.isi = "Hello World"; ClassKu mine = new ClassKu (); mine.isi = 123; 22 © Sekolah Tinggi Teknik Surabaya
» Refer to the The Elven Shrine Problem 23 © Sekolah Tinggi Teknik Surabaya
» Andrew Troelsen, Pro C# and The.Net 4.5 Framework (Sixth Edition), Apress, 2012 » Object-Oriented Programming (C# and Visual Basic), us/library/dd aspx, accessed March 16 th, us/library/dd aspx » Classes and Structs (C# Programming Guide), us/library/ms aspx, accessed March 16 th, 2014http://msdn.microsoft.com/en- us/library/ms aspx 24 © Sekolah Tinggi Teknik Surabaya