Dotnet course
מתוך OdedWiki
קורס .net דוט נט dot net
[עריכה] שיעור I
10.3.2008
[עריכה] הגדרה
- API: אוסף פונקציות, לדוגמה: call api.function
- ב-DOTNET ה-API עונה לשם NET Framework.
- פלטפורמה:
- ב-WIN95: ה-API ישב בתוך מערכת ההפעלה.
- ב-WIN98: ישנם שני API, לדוגמה: API95 ו-API98
- יש תמיכה לאחור אך אפליקציות חדשות לא רצות על WIN95
- הופיעה שפת JAVA שיכולה לרוץ על JVM - Java Virtual Machine שיכולה לשבת על כל מערכת הפעלה ומכילה API משלה. כך בעצם הוציאו לראשונה את ה-API ממערכת ההפעלה.
- מיקרוסופט הגיבה לSUN ב:
- C#: שפת פיתוח שמקבילה ל-JAVA
- MSIL שמקביל ל-CLASS
- CLR שמקביל ל-JVM
- השוני מ-SUN הוא שמיקרוסופט פיתחה שפות המקבילות לרוב השפות הקיימות לדוגמה J# כך שכולם יכלו לכתוב ל-DOTNET.
- בנוסף, האפליקציות לא חייבות את ה-CLR דווקא על ה-CLIENT אלא מספיק שהוא יהיה על ה-SERVER.
[עריכה] dotnet Platform
כל מה שמתממשק ל-DOTNET
- framework : ה-API
- my services: שירותי רשת
- visual studio: סביבת פיתוח
- קומפיילרים של כל השפות
- המבנה הוא:
- שפות פיתוח
- XML WEBSERVICES / USER INTERFACE
- ADO.NET DATA AND XML
- FRAMEWORK CLASS LIBRARY
- COMMON LANGUAGE RUNTIME
- FRAMEWORK CLASS LIBRARY
- ADO.NET DATA AND XML
- XML WEBSERVICES / USER INTERFACE
- שפות פיתוח
- יתרונות: פשטות, מחלקות שניתנות להרחבה.
[עריכה] ש.ב.
קריאת קובץ ה-PDF שב- scratch->ehelpdesk->doronamir לסכם עד עמוד 8
[עריכה] תוכנות נדרשות
- VISUAL STUDIO DOT NET 2005 PROFFESIONAL EDITION OR DEVELOPER EDITION
- SQL SERVER 2005 - SQL SERVER MANAGEMENT STUDIO
- מתקינים קודם את STUDIO ואח"כ את ה- SQL SERVER
[עריכה] בוצע בשיעור הזה
- INTRODUCTION
[עריכה] שיעור II
17.3.2008
[עריכה] המשך תיאוריה
- ב-C# אין פונקציות, יש רק מת'ודות: מת'ודה = פונקציה שיש לה מחלקה
- השפה c# => קומפיילר שמתרגם מ-C# לשפת => IL - INTERMEDIATE LANGUAGE => ה-IL רץ על CLR
- מיקום: c:winnt/microsoft.net/framework
- סוגי הקבצים העיקריים הם : exe ו- dll
- exe מבקש PROCESS ממערכת ההפעלה שנותן בידוד וזיכרון
- יכולה להיות פונקציונאליות ב-exe אך נהוג לחלק את הפונקציונאליות לקבצים של dll
- dll הוא מחסן/אוסף של מחלקות / פונקציונאליות
- בין dllים ו-exe צריך להיות קשר והוא נקרא reference
- שניהם כתובים בשפת IL
- exe מבקש PROCESS ממערכת ההפעלה שנותן בידוד וזיכרון
- קבצי המקור הם בפורמט file.cs לפני שהם עוברים קומפילציה
[עריכה] קוד
namespace=>class=>method טורקיז = צבע למחלקות כחול = מילים שמורות ב-C# השפה casesensitive
- פותחים את ההשלמה האוטומטית ע"י ctrl+space
- מתחילים לכתוב את ה-CLASS, בוחרים מהרשימה את ה-CLASS הרלוונטי ע"י TAB
- כשמוסיפים נקודה נפתחות המת'ודות
- מריצים עם ctrl+f5
- שלום עולם:
console.writeline("whatup world?")
- ניתן לבצע "פירוק" של הקובץ ע"י dassembler.
- כל ה-"using" בתחילת הקוד הם reference ל-namespaces, הם מציינים אלו קבוצות של מחלקות אפשר לעבוד איתן.
- namespaces הם אוסף של classים, היכולת לחלק בצורה לוגית מחלקות בתוך קבצי dll.
- ב-helloworld פנינו ל-
- dll בשם system.dll
- namespace בשם system (לא חייב להיות כמו שם ה-DLL)
- ניתן לצפות בכל ה-namespace בתוך ה-OBJECT BROWSER, הענפים הראשיים הם קבצי dll ותחתם קיימים ה-namespace ותחתם CLASSים.
[עריכה] DEBUGGING
- ניתן להריץ DEBUGGING עם F11 ועם לחיצה על השטח בצד שמאל לסימון BREAKPOINT.
- אפשר לפתוח קובץ class חדש בסיומת cs עם קליק ימני על שטח לבן ב-solution explorer
- ניתן לעשות EXCLUDE לקבצים ע"י קליק ימני ב-solution explorer משם add משם class.
- לאחר שעושים exclude ניתן לראות את הקבצים שנעלמו ע"י "show all files" בחלק העליון של ה-solution explorer
[עריכה] ש"ב
ליצור אפליקציה שתקלוט את כל הנתונים של משתמש, גם בSTRING וגם בINT.
[עריכה] פתרון
using System;
namespace myNamespace
{
class myClass
{
public static void Main()
{
Console.WriteLine("enter your name:");
string name = Console.ReadLine();
Console.WriteLine("Hi " + name);
Console.WriteLine("enter your age:");
//int.parse transforms the value entered into "age" variable into a number
int age =int.Parse (Console.ReadLine());
Console.WriteLine("enter your city of residence:");
string city = Console.ReadLine();
Console.WriteLine("enter your birth year:");
int year = int.Parse(Console.ReadLine());
int calcage = DateTime.Today.Year-year;
DayOfWeek weekday = DateTime.Now.DayOfWeek;
Console.WriteLine("hello " + name);
Console.WriteLine("you are " + calcage + " years old and you live at "
+ city + ". btw, you said that you are " + age
+ " and what the !@#$% are U doing writing C# on a " + weekday+ "?");
Console.ReadLine();
}
}
}
[עריכה] שיעור III
[עריכה] חזרה
מסך solution explorer:
- references: קבצי ה-dll שאנו משתמשים בהם.
- קבצים עם סיומת cs: קבצי ה-source.
[עריכה] תיאוריה
- הייתה בעיה של המרת INT בגודל 32BIT ל-16BIT
- CTS: מערכת טיפוסים, common type system
- value
- system types
- user defined types
- reference
- system types
- user defined types
- value
- class הטיפוס הגמיש ביותר
- יצירת טיפוסים
- כדי לייצר טיפוס משלנו יוצרים טיפוס enum, enumration: רשימה.
- לדוגמה:
- כדי לייצר טיפוס משלנו יוצרים טיפוס enum, enumration: רשימה.
using System;
namespace myNamespace
{
enum myColors { red=234, blue=543, green=20 }
class myClass
{
public static void Main()
{
Console.WriteLine("hello world");
Console.WriteLine(myColors.red);
Console.WriteLine(myColors.green);
Console.WriteLine(myColors.blue);
// to see the int values we "cast"
Console.WriteLine((int)myColors.red);
Console.WriteLine((int)myColors.green);
Console.WriteLine((int)myColors.blue);
Console.ReadLine();
}
}
}
- דוגמה נוספת של ENUM מורכבת יותר:
using System;
namespace myNamespace
{
enum myDays { sun=1, mon=2, tue=3, wed=4, thu=5, fri=6, sat=7 }
class myClass
{
public static void Main()
{
Console.WriteLine("hello");
Console.WriteLine("tyoe {0} for {1}",(int)myDays.sun,myDays.sun);
Console.WriteLine("tyoe {0} for {1}", (int)myDays.mon, myDays.mon);
Console.WriteLine("tyoe {0} for {1}", (int)myDays.tue, myDays.tue);
Console.WriteLine("tyoe {0} for {1}", (int)myDays.wed, myDays.wed);
Console.WriteLine("tyoe {0} for {1}", (int)myDays.thu, myDays.thu);
Console.WriteLine("tyoe {0} for {1}", (int)myDays.fri, myDays.fri);
Console.WriteLine("tyoe {0} for {1}", (int)myDays.sat, myDays.sat);
Console.WriteLine("please select the day of the week numerically");
int day = int.Parse(Console.ReadLine());
Console.WriteLine("you selected {0}", (myDays)day);
if (day == (int)myDays.sun)
{
Console.WriteLine("Have a great {0}", (myDays)day);
}
if (day == (int)myDays.mon)
{
Console.WriteLine("Have a great {0}", (myDays)day);
}
if (day == (int)myDays.tue)
{
Console.WriteLine("Have a great {0}", (myDays)day);
}
if (day == (int)myDays.wed)
{
Console.WriteLine("Have a great {0}", (myDays)day);
}
if (day == (int)myDays.thu)
{
Console.WriteLine("Have a great {0}", (myDays)day);
}
if (day == (int)myDays.fri)
{
Console.WriteLine("Have a great {0}", (myDays)day);
}
if (day == (int)myDays.sat)
{
Console.WriteLine("Have a great {0}", (myDays)day);
}
Console.ReadLine();
}
}
}
[עריכה] syntax
- if
- else
- else if (מאפשר ליצור if מקונן)
- דוגמה:
using System;
namespace myNamespace
{
class myClass
{
public static void Main()
{
Console.WriteLine("hi");
Console.WriteLine("num?");
int x = int.Parse(Console.ReadLine());
if (x < 10)
{
Console.WriteLine("smaller then 10");
}
else
{
Console.WriteLine("bigger or equal to 10");
}
}
}
}
- switch: במקום IF מקונן אפשר להשתמש ב- switch
using System;
namespace myNamespace
{
class myClass
{
public static void Main()
{
Console.WriteLine("select a number");
int x = int.Parse(Console.ReadLine());
switch (x)
{
case 1:
Console.WriteLine("1 was selected");
break;
case 2:
Console.WriteLine("2 was selected");
break;
default:
Console.WriteLine("something else was selected");
break;
}
}
}
}
[עריכה] ש.ב.
- בכונן +
- לסכם כללי - 2 שורות לדף בערכה
- לעשות את התרגילים שעשינו היום
- לבנות תוכנית המבקשת להקליד ערך מספרי ומדפיסה תאריך מלא.
- לדוגמה:
- נקליד 21, 12, 1975
- נקבל: יום שני דצמבר 1975 שאינה שנה מעוברת
- לדוגמה:
- שאלות לדורון: doron@doronamir@.com
[עריכה] פתרון
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ShowDates
{
public enum MyDays
{
Sunday = 1,
Monday,
Tuesday,
Wednday,
Thursday,
Friday,
Saturday
}
public enum MyMonths
{
JAN = 1,
FEB,
MAR,
APR,
MAY,
JUN,
JUL,
AUG,
SEP,
OCT,
MOV,
DEC
}
public enum MyYears
{
y1970,
y1971,
y1972,
y1973,
y1974,
y1975,
y1976,
y1977,
y1978,
y1979,
y1980
}
class Program
{
static void Main(string[] args)
{
int Day, Month, Year;
Console.WriteLine("Please insert the following values:Day Month Year: ");
Day =
int.Parse(Console.ReadLine());
Month =
int.Parse(Console.ReadLine());
Year =
int.Parse(Console.ReadLine());
if ((Day < (int)MyDays.Sunday) && (Day > 30))
{
Console.WriteLine("Error in Day!");
}
if (!(Month >= (int)MyMonths.JAN) || !(Month <= (int)MyMonths.DEC))
{
Console.WriteLine("Error in Month!");
}
if ((Year < (int)MyYears.y1970) && !(Year > (int)MyYears.y1980))
{
Console.WriteLine("Error in Year!");
}
int temp = 0;
temp = Day % 7;
Console.WriteLine("The date you entered is {0}-{1}-{2}",
(
MyDays)temp,
(
MyMonths)Month,
Year);
}
}
}
[עריכה] שיעור IV
- הזנה ופרסום נתונים מ-STRUCT:
- ניתן להגדיר אובייקטים עם struct שייקבלו בהמשך התוכנית נתונים.
- לדוגמה, אובייקט סטודנט שיכול להכיל שם, משפחה וגיל. ניתן להצביע על מידע בין structים ע"י "=".
דוגמה:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
struct student
{
public string firstname;
public string lastname;
public int age;
class Program
{
static void Main(string[] args)
{
student st1, st2, st3;
st1.firstname = "oded";
st1.lastname = "breiner";
st1.age = 25;
st2.firstname = "michael";
st2.lastname = "jordan";
st2.age = 80;
Console.WriteLine("first={0},last={1},age={2}", st1.firstname,st1.lastname,st1.age);
st3 = st2;
Console.WriteLine("first={0},last={1},age={2}", st3.firstname, st3.lastname, st3.age);
}
}
}
}
- עבודה עם פונקציות, את המידע למעלה ניתן להציג באמצעות פונקציה (מתודה) שאנחנו מגדירים בשם PRINTSTUDENT:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
struct student
{
public string firstname;
public string lastname;
public int age;
class Program
{
static void Main(string[] args)
{
student st1, st2, st3;
st1.firstname = "oded";
st1.lastname = "breiner";
st1.age = 25;
st2.firstname = "michael";
st2.lastname = "jordan";
st2.age = 80;
st3 = st2;
printStudent(st1);
printStudent(st3);
}
static void printStudent(student st)
{
Console.WriteLine("first={0},last={1},age={2}", st.firstname, st.lastname, st.age);
}
}
}
}
- פונקציה שתחזיר סכום של גילאים:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
struct student
{
public string firstname;
public string lastname;
public int age;
class Program
{
static void Main(string[] args)
{
student st1, st2, st3;
st1.firstname = "oded";
st1.lastname = "breiner";
st1.age = 25;
st2.firstname = "michael";
st2.lastname = "jordan";
st2.age = 80;
st3 = st2;
printStudent(st1);
printStudent(st3);
int sum = GetAgeSum(st1, st3);
Console.WriteLine(sum);
}
static void printStudent(student st)
{
Console.WriteLine("first={0},last={1},age={2}", st.firstname, st.lastname, st.age);
}
static int GetAgeSum(student sta, student stb)
{
int sum = (int)sta.age + (int)stb.age ;
return sum ;
}
}
}
}
- ספירה, העברת ערך באמצעות ref:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int x;
x = 100;
AddOne(ref x);
Console.WriteLine(x);
}
static void AddOne(ref int x)
{
x++;
}
}
}
- ל-ref יש חסרון מכיוון שהוא דורש איתחול, לכן יש את out שלא דורש איתחול:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int x;
x = 100;
AddOne(out x);
Console.WriteLine(x);
}
static void AddOne(out int x)
{
x=100;
}
}
}
- לולאות:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
}
}
}
- הגדרת מערך:
int[] arr = new int[5];
arr[0] = 45;
arr[1] = 34;
arr[2] = 87;
arr[3] = 11;
arr[4] = 23;
[עריכה] ש.ב.
קריאת פרק 4, יצירת תוכנית שמחשבת את הימים כפי שהראו בשיעור. להגיש דוגמאות לכל סוג של קוד בפרק. (ב-ARRAYLIST צריך להוסיף USING SYSTEM.COLLECTIONS)
[עריכה] שיעור V
- דוגמה ל-REFERENCE TYPE:
using System;
namespace nsDoron
{
class Person
{
public string firstname;
public string lastname;
public int age;
}
class myClass
{
static void Main()
{
Person p1 = new Person ();
Person p2;
p1.firstname="oded";
p1.lastname = "breiner";
p1.age = 25;
p2 = p1;
Console.WriteLine(p2.age);
Console.WriteLine(p2.firstname);
Console.WriteLine(p2.lastname);
}
}
}
- encapsulation: הגנה על נתונים ופונקציות בתוך אובייקטים
using System;
namespace nsDoron
{
class Person
{
private string firstName;
private string lastName;
private int age;
//method to change private members
public void setFirstName(string fn)
{
if (fn == "")
{
Console.WriteLine("NO NAME");
}
else
{
firstName = fn;
}
}
public string getFirstName()
{
return firstName;
}
}
class myClass
{
static void Main()
{
Person p1 = new Person();
p1.setFirstName("");
Console.WriteLine(p1.getFirstName());
}
}
}
- ההבדל בין PROPERTIES ל-METHODS = מאפיינים או מצבים מול פעולות.
using System;
using System.Collections.Generic;
using System.Text;
namespace Idantr
{
//referance type now there is a change and we will see using private members..
class Person
{
private string firstName;
private string lastName;
private int age;
// this method is not connected to the behaviour of the class
public void SayHello()
{
Console.WriteLine("Hello {0}", this.firstName);
}
// property
//no () here in a property starts with capital letter
public string FirstName
{
set { this.firstName = value; }
get { return this.firstName; }
}
}
class myClass
{
static void Main(string[] args)
{
Person p1 = new Person();
p1.FirstName = "Idan";
Console.WriteLine(p1.FirstName);
}
}
}
- PROPERY של גיל:
using System;
using System.Collections.Generic;
using System.Text;
namespace Idantr
{
//referance type now there is a change and we will see using private members..
class Person
{
private string firstName;
private string lastName;
private int age;
// this method is not connected to the behaviour of the class
public void SayHello()
{
Console.WriteLine("Hello {0}", this.firstName);
}
// property
//no () here in a property starts with capital letter
public string FirstName
{
set { this.firstName = value; }
get { return this.firstName; }
}
public int Age
{
set
{
if (value != 0)
{
this.age = value;
}
else
{
this.age = 1;
}
}
get { return this.age; }
}
}
class myClass
{
static void Main(string[] args)
{
Person p1 = new Person();
p1.FirstName = "Idan";
Console.WriteLine(p1.FirstName);
p1.Age = 32;
Console.WriteLine(p1.Age);
}
}
}
- שימוש ב-constructor לאיתחול הנתונים:
using System;
using System.Collections.Generic;
using System.Text;
namespace Idantr
{
//referance type now there is a change and we will see using private members..
class Person
{
public Person(string firstName, string lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
private string firstName;
private string lastName;
private int age;
// this method is not connected to the behaviour of the class
public void SayHello()
{
Console.WriteLine("Hello {0}", this.firstName);
}
// property
//no () here in a property starts with capital letter
public string FirstName
{
set { this.firstName = value; }
get { return this.firstName; }
}
public int Age
{
set
{
if (value != 0)
{
this.age = value;
}
else
{
this.age = 1;
}
}
get { return this.age; }
}
public void PrintData()
{
Console.WriteLine("FN={0},LN={1}, AGE={2}", this.firstName, this.lastName, this.age);
}
}
class myClass
{
static void Main(string[] args)
{
Person p1 = new Person("IDANTR1","TREIVISH",32);
p1.PrintData();
}
}
}
[עריכה] ירושה
- ירושה: inheritance + הוספת תכונות:
class Worker : Person
{
private string id;
public string ID
{
set { this.id=value;}
get { return this.id;}
}
}
- ע"מ ליצור פונקציה שניתנת לדריסה ע"י אובייקט חדש לאחר ירושה יש להגדיר אותה כ"virtual".
- ואז לדרוס אותה ע"י override
דוגמה:
using System;
using System.Collections.Generic;
using System.Text;
namespace Idantr
{
//referance type now there is a change and we will see using private members..
class Person
{
//default constructor built on my own
public Person()
{
this.firstName = "No Name";
this.lastName = "No Last Name";
this.age = 0;
}
public Person(string firstName)
{
this.firstName = firstName;
this.lastName = "No Last Name";
this.age = 0;
}
public Person(string firstName, string lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
private string firstName;
private string lastName;
private int age;
// this method is not connected to the behaviour of the class
//I added virtual so i can inherit the method and override it
public virtual void SayHello()
{
Console.WriteLine("Hello {0}", this.firstName);
}
// property
//no () here in a property starts with capital letter
public string FirstName
{
set { this.firstName = value; }
get { return this.firstName; }
}
public int Age
{
set
{
if (value != 0)
{
this.age = value;
}
else
{
this.age = 1;
}
}
get { return this.age; }
}
public void PrintData()
{
Console.WriteLine("FN={0},LN={1}, AGE={2}", this.firstName, this.lastName, this.age);
}
}
//horasha : inheritance + add data to base class and overide virtual method
class Worker : Person
{
private string id;
public string ID
{
set { this.id = value; }
get { return this.id; }
}
public override void SayHello()
{
//base.SayHello();
// if there was no property to the father i could not do the following (FirstName)
Console.WriteLine("Hello {0} ID {1}", this.FirstName, this.id);
}
}
class myClass
{
static void Main(string[] args)
{
Person p1 = new Person("IDANTR1","TREIVISH",32);
p1.SayHello();
Worker w1 = new Worker();
w1.FirstName = "Idan";
w1.ID = "000000018";
w1.SayHello();
}
}
}
- ניתן לבצע שימוש ב-CONSTRUCTOR של מחלקת אב ע"י פקודת base.
- CASTING בין מחלקות בנים ואבות
using System;
namespace nsDoron
{
class Person
{
public virtual void Hello()
{
Console.WriteLine("Hello Person... ");
}
}
class Worker : Person
{
public Worker(string firstName, string id)
{
this.firstName = firstName;
this.id = id;
}
public string firstName;
public string id;
public override void Hello()
{
Console.WriteLine("I worker {0}{1}",this.firstName,this.id);
}
}
class employee : Worker
{
public employee(string empID, string firstName, string id):base(firstName,id)
{
this.empID = empID;
}
public string empID;
public override void Hello()
{
Console.WriteLine("I employee {0}", this.empID);
}
}
class myClass
{
static void Main()
{
Person p1 = new Person();
//p1.Hello();
Worker w1 = new Worker("Doron", "00101");
//w1.Hello();
employee e1 = new employee("4444", "Amir","0010101");
//e1.Hello();
p1 = w1;
p1.Hello();
w1 =(Worker)p1;
}
}
}
- הסבר נוסף על פולימורפיזם ב-doronamir.com
[עריכה] ש.ב.
לקרוא שיעורים 1-8 ב->דורון אמיר -> 005 +שיעורים ב-STARTER
[עריכה] שיעור VI
[עריכה] STRUCT פשוט, עם מערך והדפסה:
using System; namespace nsDoron {
public struct Student
{
public string firstName;
public string lastName;
public int age;
}
class myClass
{
public static void Main()
{
Student s1, s2, s3;
s1.age = 10;
s1.firstName = "aFname";
s1.lastName = "aLname";
s2.age = 30;
s2.firstName = "bFname";
s2.lastName = "bLname";
s3.age = 40;
s3.firstName = "cFname";
s3.lastName = "cLname";
Student[] arr = new Student[3];
arr[0] = s1;
arr[1] = s2;
arr[2] = s3;
foreach (Student var in arr)
{
Console.WriteLine("fn={0} ln={1} age={2}", var.firstName, var.lastName, var.age);
}
}
}
}
[עריכה] בשילוב עם PROPERTIES ומת'ודה HELLO שהיא PUBLIC:
using System; namespace nsDoron {
class Person
{
private string firstName;
private string lastName;
private int age;
public string FirstName
{
set { this.firstName = value; }
get { return this.firstName; }
}
public string LastName
{
set { this.lastName = value; }
get { return this.lastName; }
}
public int Age
{
set {
if (value < 1)
{
this.age = 0;
}
else
{
this.age = value;
}
}
get { return this.age; }
}
public string Hello()
{
return string.Format("hello from {0}{1}", this.FirstName,this.LastName);
}
}
class myClass
{
public static void Main()
{
Person p1 = new Person();
p1.FirstName = "aaaa";
p1.LastName = "bbbbb";
p1.Age = -10;
Console.WriteLine(p1.FirstName);
Console.WriteLine(p1.LastName);
Console.WriteLine(p1.Age);
Console.WriteLine(p1.Hello());
}
}
}
[עריכה] עם CONSTRUCTOR:
using System; namespace nsDoron {
class Person
{
public Person()
{
this.firstName = "NONAME";
this.lastName = "NONAME";
this.age = 0;
}
public Person(string firstName,string lastName,int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
private string firstName;
private string lastName;
private int age;
public string FirstName
{
set { this.firstName = value; }
get { return this.firstName; }
}
public string LastName
{
set { this.lastName = value; }
get { return this.lastName; }
}
public int Age
{
set {
if (value < 1)
{
this.age = 0;
}
else
{
this.age = value;
}
}
get { return this.age; }
}
public string getData()
{
return string.Format("hello from {0} {1} {2}", this.FirstName, this.LastName, this.Age);
}
}
class myClass
{
public static void Main()
{
Person p1 = new Person("aFname", "aLastname", 40);
Person p2 = new Person();
Person p3 = new Person("ODED", "BREINER", 25);
Console.WriteLine(p1.getData());
Console.WriteLine(p2.getData());
Console.WriteLine(p3.getData());
}
}
}
[עריכה] עם ירושה ושימוש BASE
using System; namespace nsDoron {
class Person
{
public Person()
{
this.firstName = "NONAME";
this.lastName = "NONAME";
this.age = 0;
}
public Person(string firstName,string lastName,int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
private string firstName;
private string lastName;
private int age;
public string FirstName
{
set { this.firstName = value; }
get { return this.firstName; }
}
public string LastName
{
set { this.lastName = value; }
get { return this.lastName; }
}
public int Age
{
set {
if (value < 1)
{
this.age = 0;
}
else
{
this.age = value;
}
}
get { return this.age; }
}
public string getData()
{
return string.Format("hello from {0} {1} {2}", this.FirstName, this.LastName, this.Age);
}
}
class Student : Person
{
public Student(string firstName, string lastName,int age, string stID):base(firstName,lastName,age)
{
this.stID = stID;
}
private string stID;
public string Hello()
{
return String.Format("f={0},l={1},a={2},id={3}",this.FirstName,this.LastName,this.Age,this.stID);
}
}
class myClass
{
public static void Main()
{
Student s1 = new Student("oded", "breiner", 25, "6541657");
Console.WriteLine(s1.Hello());
Console.WriteLine(s1.getData());
}
}
}
[עריכה] עם דריסה OVERRIDE
using System;
namespace nsDoron {
class Person
{
public Person()
{
this.firstName = "NO NAME";
this.lastName = "NO NAME";
this.age = 0;
}
public Person(string firstName)
{
this.firstName = firstName;
this.lastName = "No Last NAME";
this.age = 0;
}
public Person(string firstName,string lastName,int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
private string firstName;
private string lastName;
private int age;
public string FirstName
{
set { this.firstName = value; }
get { return this.firstName; }
}
public string LasttName
{
set { this.lastName = value; }
get { return this.lastName; }
}
public int Age
{
set {
if (value < 1)
{
this.age = 0;
}
else
{
this.age = value;
}
}
get { return this.age; }
}
public string getData()
{
return String.Format("Hello from {0} {1} {2}",this.FirstName,this.LasttName,this.age);
}
public virtual string Hello()
{
return String.Format("Hello from Person");
}
}
class Student : Person
{
public Student(string firstName,string lastName,int age,string stID):base(firstName,lastName,age)
{
this.stID = stID;
}
private string stID;
new public string getData()
{
return String.Format("F={0} L={1} A={2} ID={3}",this.FirstName,this.LasttName,this.Age,this.stID);
}
public override string Hello()
{
return String.Format("Hello from student");
}
}
class myClass
{
public static void Main()
{
Student s1 = new Student("Doron", "Amir", 32, "01010");
//Console.WriteLine(s1.Hello());
// Console.WriteLine(s1.getData());
// Console.WriteLine( ((Person)s1).getData() );
Console.WriteLine(((Person)s1).Hello());
} }
}
[עריכה] CLASS אבסטרטקטי
ניתן ליצור CLASS מסוג ABSTRACT שלא יכול לייצר INSTANCES, לדוגמה, CLASS של בעלי חיים.
[עריכה] מערך שמחזיק כמה סוגים של אובייקטי ילדים שמריצים מת'ודת HELLO בצורות שונות - פולימורפיזם
using System;
namespace nsDoron {
abstract class Person
{
public string name;
public virtual void Hello()
{
Console.WriteLine("Hello from the Base [person]");
}
}
class student : Person
{
public student(string name)
{
this.name = name;
}
public override void Hello()
{
Console.WriteLine("Hello from Student {0}",this.name);
}
public void SayStudent()
{
Console.WriteLine("I'm Student !!! {0}",this.name);
}
}
class worker : Person
{
public worker(string name)
{
this.name = name;
}
public override void Hello()
{
Console.WriteLine("Hello from worker {0}", this.name);
}
public void SayWorker()
{
Console.WriteLine("I'm Worker !!! {0}", this.name);
}
}
class employee : Person
{
public employee(string name)
{
this.name = name;
}
public void SayEmployee()
{
Console.WriteLine("I'm employee !!! {0}", this.name);
}
}
class myClass
{
static void Main()
{
// Person x = new Person();
Person[] arr = new Person[5];
worker w1 = new worker("w1");
worker w2 = new worker("w2");
student s1 = new student("s1");
student s2 = new student("s2");
employee e1 = new employee("e1");
arr[0] = w1;
arr[1] = w2;
arr[2] = s1;
arr[3] = s2;
arr[4] = e1;
foreach (Person var in arr)
{
var.Hello();
}
} }
}
[עריכה] INTERFACE = סט של תכונות משותפות
using System;
namespace nsDoron {
interface Ihello
{
void Hello();
}
class Dog:Ihello
{
private string name;
public Dog(string name)
{
this.name = name;
}
public void Hello()
{
Console.WriteLine("Hello from Dog {0}", this.name);
}
}
abstract class Person:Ihello
{
public string name;
public abstract void Hello();
}
class student : Person,Ihello
{
public student(string name)
{
this.name = name;
}
public override void Hello()
{
Console.WriteLine("Hello from Student {0}",this.name);
}
public void SayStudent()
{
Console.WriteLine("I'm Student !!! {0}",this.name);
}
}
class worker : Person,Ihello
{
public worker(string name)
{
this.name = name;
}
public override void Hello()
{
Console.WriteLine("Hello from worker {0}", this.name);
}
public void SayWorker()
{
Console.WriteLine("I'm Worker !!! {0}", this.name);
}
}
class employee : Person
{
public employee(string name)
{
this.name = name;
}
public override void Hello()
{
Console.WriteLine("I'm employee !!! {0}", this.name);
}
public void SayEmployee()
{
Console.WriteLine("I'm employee !!! {0}", this.name);
}
}
class myClass
{
static void Main()
{
// Person x = new Person();
Dog d1 = new Dog("pluto");
Ihello[] arr = new Ihello[5];
worker w1 = new worker("w1");
worker w2 = new worker("w2");
student s1 = new student("s1");
student s2 = new student("s2");
employee e1 = new employee("e1");
arr[0] = w1;
arr[1] = w2;
arr[2] = s1;
arr[3] = d1;
arr[4] = e1;
foreach (Ihello var in arr)
{
var.Hello();
}
} }
}
[עריכה] ש.ב.
לבנות מערך עם אובייקטים עפ"י פרק 10 ולהפעיל את המת'ודות הייחודיות של האובייקטים עם IS ו-AS
[עריכה] שיעור VII
- abstract method: מת'ודה שחייבים לדרוס. מהווה בקרה על הקוד, בכך שלא מאפשרת התנהגות defaultיבית לסוג. חייבת להיות בתוך abstract class.
- abstract class: לא נועדה לייצר אובייקטים אלא רק להוריש פונקציונאליות. מחלקה כללית.
- sealed class: מחלקה סופית שכבר לא ניתן להוריש ממנה, כלומר סוג ספציפי של אובייקט.
[עריכה] תרגיל
WORKER (2) STUDENT (2) משותף: ID NAME
[עריכה] פתרון
using System;
namespace oded
{
class Student
{
public Student()
{
this.id = "0000";
}
public Student(string id)
{
this.id=id;
}
private string id;
public string ID
{
set {this.id=value;}
get {return this.id;}
}
public string ShowMyID()
{
return string.Format("my id is {0}",this.id);
}
}
class Worker : Student
{
public Worker()
{}
public Worker(string id):base(id)
{ }
}
class mine
{
static void Main()
{
Student s1 = new Student("123");
Student s2 = new Student("456");
Worker w1 = new Worker("555");
Worker w2 = new Worker("888");
Student [] arr = new Student[4];
arr[0] = s1;
arr[1] = s2;
arr[2] = w1;
arr[3] = w2;
foreach (Student i in arr)
{
Console.WriteLine(i.ShowMyID());
}
}
}
}
[עריכה] ש.ב.
קובץ PAINT mod10_exc+ הבונוס בסוף מבוצע באמצעות "is" ו-"as" להגשה.
