CLASS, STRUCT, INTERFACE - ThS. Nguyễn Hà Giang pdf

61 227 0
CLASS, STRUCT, INTERFACE - ThS. Nguyễn Hà Giang pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CLASS, STRUCT, INTERFACE ThS. Nguyễn Giang 1Nguyen Ha Giang -2009 Content 1. Class definition 2. Constructor & destructor 3. Property 4. Indexer 5. Polymorphism 6. Downcast –Upcast 7. Abstract class –Sealed class 8. Struct 9. Interface 2Nguyen Ha Giang -2009 Class declaration Nguyen Ha Giang -2009 3 [access modifier] class <class_name> [:base class, interfaces…] { // class body } Access modifier public protected internal protected internal private (default) Base class: only one class or not Interface: implement some interfaces Class declaration Nguyen Ha Giang -2009 4 public class Student { // data member … // function member … } Access modifier public No base class Default derive from object (System.Object) Class member Nguyen Ha Giang -2009 5 Class Constructor Properties Indexer Delegate, event Method Field, const, readonly Nested type Constructor • Called when create object • Same class name • Class has default constructor which no parameter • Class has multiple constructor (overloading constructor) Nguyen Ha Giang -2009 6 Constructor • Default constructor •No parameter •Create object when has no any information • Copy constructor •Input parameter is another object •Create new object like input object • Other constructors •One or more parameter •Create new object with some information Nguyen Ha Giang -2009 7 Constructor • Ex Nguyen Ha Giang -2009 8 public class Student { // data member string name; int year; float mark; // function member … } public Student() { name = “Ha Giang"; year = 1978; mark = 5; } public Student(Student st) { name = st.name; year = st.year; mark = st.mark; } public Student(string sname) { name = sname; } Constructor • The private constructor prevents the class from being created Nguyen Ha Giang -2009 9 class Student { // private Student() { } } class Program { static void Main(string[] args) { Student s = new Student(); } } Constructor • Constructor chaining •Call one constructor from another constructor •Usage •base(parameter): call constructor of base class •this(parameter): call a constructor in the current class Nguyen Ha Giang -2009 10 <constructor of class A> : base(list of parameter) <constructor of class A> : this(list of parameter) [...]... { set { } } Write-only property Nguyen Ha Giang - 2009 [modifers] { get { } } Read-only property 24 Property class Employee { // fields private string name; private float salary; // properties public string NAME { get { return name; } set { name = value; } } public float SALARY { get { return salary; } } } Nguyen Ha Giang - 2009 Read... Ha Giang - 2009 22 Property • Provide a flexible mechanism to read, write or compute the values of private fields Getter/Setter method Property Private member Private member private string name private string name - GetName() - SetName(…) Nguyen Ha Giang - 2009 Property: NAME 23 Property • Properties as “smart” fields [modifers] { [ set { } ] [ get { . CLASS, STRUCT, INTERFACE ThS. Nguyễn Hà Giang 1Nguyen Ha Giang -2 009 Content 1. Class definition 2. Constructor & destructor 3 class –Sealed class 8. Struct 9. Interface 2Nguyen Ha Giang -2 009 Class declaration Nguyen Ha Giang -2 009 3 [access modifier] class <class_name> [:base class, interfaces…] { // class body } Access. information Nguyen Ha Giang -2 009 7 Constructor • Ex Nguyen Ha Giang -2 009 8 public class Student { // data member string name; int year; float mark; // function member … } public Student() { name = “Ha Giang& quot;; year

Ngày đăng: 28/03/2014, 16:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan