Introduction to C# - Sekolah Tinggi Teknik Surabaya

Download Report

Transcript Introduction to C# - Sekolah Tinggi Teknik Surabaya

1
Lecture 2
Introduction to C# - Object Oriented
Sandy Ardianto & Erick Pranata
© Sekolah Tinggi Teknik Surabaya
class Mahasiswa {
public String NRP {get; set; }
public String Nama {get; set; }
public int energy {get; set; }
public Mahasiswa(){
Property
Constructor
}
public void Kuliah(){
energy--;
}
Method
}
Mahasiswa mhs = new Mahasiswa();
© Sekolah Tinggi Teknik Surabaya
2
» A class defined within another class is
called nested.
class Container
{
class Nested
{
// Add code here.
}
}
Container.Nested nestedInstance =
new Container.Nested();
3
© Sekolah Tinggi Teknik Surabaya
Same Name, Different Parameter
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;
}
}
© Sekolah Tinggi Teknik Surabaya
4
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;
© Sekolah Tinggi Teknik Surabaya
5
» 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.
© Sekolah Tinggi Teknik Surabaya
6
» 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
Modifier
Description
private
can 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
8
© Sekolah Tinggi Teknik Surabaya
» 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();
© Sekolah Tinggi Teknik Surabaya
11
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();
© Sekolah Tinggi Teknik Surabaya
12
13
© Sekolah Tinggi Teknik Surabaya
class Manusia
{
}
class Mahasiswa:Manusia
{
}
14
© Sekolah Tinggi Teknik Surabaya
class Manusia
{
}
class Mahasiswa:Manusia
{
}
class Asisten:Manusia
{
}
Manusia m = new Mahasiswa();
m = new Asisten();
© Sekolah Tinggi Teknik Surabaya
15
» 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
© Sekolah Tinggi Teknik Surabaya
16
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.
© Sekolah Tinggi Teknik Surabaya
17
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() { }
}
© Sekolah Tinggi Teknik Surabaya
19
» Interface Statement
interface ISampleInterface
{
void doSomething();
}
» Class implement interface
class SampleClass : ISampleInterface
{
void doSomething()
{
// Method implementation.
}
}
© Sekolah Tinggi Teknik Surabaya
20
» 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.
© Sekolah Tinggi Teknik Surabaya
21
» To define a generic class:
Public class ClassKu<T>
{
public T isi;
}
» To create an instance of a generic class:
ClassKu<string> mine = new ClassKu<string>();
mine.isi = "Hello World";
ClassKu<int> mine = new ClassKu<int>();
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),
http://msdn.microsoft.com/enus/library/dd460654.aspx , accessed
March 16th, 2014
» Classes and Structs (C# Programming
Guide), http://msdn.microsoft.com/enus/library/ms173109.aspx, accessed
March 16th, 2014
© Sekolah Tinggi Teknik Surabaya
24