Class שיורש מ-StreamReader - עמוד 2 - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

Class שיורש מ-StreamReader


moskitos

Recommended Posts

אני לא מכיר את התחביר של #C אבל הבנאי של המחלקה היורשת תשתמש בבנאי של המחלקה ממנה אתה יורש.

המטרה היא לקבל אינסטנס של המחלקה ממנה אתה יורש ואז להוסיף לה מאפיינים.

לדעתי אתה כותב קוד לפני שלמדת כמה עקרונות של פולימורפיזם וירושה (או תכנות מונחה עצמים בכלל).

קישור לתוכן
שתף באתרים אחרים

בוא נחשוב שניה מה יקרה אם נשתמש במה שאתה עשית.

נגיד ואני יוצר שתי פעמים את המחלקה BabyStreamReader כל פעם עם path שונה:


BabyStreamReader Reader=new BabyStreamReader(@"C:\blabla\blalas.txt")
BabyStreamReader Reader=new BabyStreamReader(@"C:\blalasdfghgdjsf\ghdfjhgt".txt)

מה שבפועל יקרה, זה שבגלל ששמת בבנאי של מחלקת האב מחרוזת שהיא hardcoded לא יהיה שום הבדל בין שני האובייקטים שיצרתי.

ולכן הסינטקס הנכון הוא:


Public BabyStreamReader(string path):base(path)
{
}

בבקשה תחזור על החומר כי זה מאוד מאוד בסיסי!

קישור לתוכן
שתף באתרים אחרים

לאחר שסיימתי את התרגיל יש לי שאלה:

התרגיל המקורי הוא:

The .NET class "StreamReader" can only read a single character or a line. Extend the class to be able to read a single integer at a time.

Create a new class that inherits from StreamReader. Add a new method to it called:

public int ReadNextInt()

Each time this method is called it will look for the next integer in the input file. It will skip any non-digit character. An integer is composed only of digits – you do not have to deal with sign or decimal point.

If the method encounters end of stream – it will return the number minus 9999.

Note that the user of the new class will be able to continue using any public method that currently exists in StreamReader class.

השאלה שלי היא האם יש דרך טובה יותר או נכונה יותר לפתור את התרגיל ?

האם לא נהוג לרשום Console.WriteLine בתוך המתודה אלא רק ב-Main ?

הקוד שלי:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace T380
{
public class BabyStreamReader : StreamReader
{
public BabyStreamReader(string path)
: base(path)
{


}


public int ReadNextInt()
{
string num = "";
BabyStreamReader InputFile = new BabyStreamReader(@"D:/C#/ConsoleApplication1/Classes/T380/bin/Debug/File.txt");


while (!InputFile.EndOfStream)
{
char character = (char)InputFile.Read();


if (character >= '0' && character <= '9')
{
num += character;
}
else if (num != "")
{
Console.WriteLine(num);
num = "";
}
}


return -9999;
}
}


class Program
{
static void Main(string[] args)
{
BabyStreamReader InputFile = new BabyStreamReader(@"D:/C#/ConsoleApplication1/Classes/T380/bin/Debug/File.txt");
int result = InputFile.ReadNextInt();
Console.WriteLine(result);
Console.ReadLine();
}
}
}

קישור לתוכן
שתף באתרים אחרים

תמיד תזכור כשאתה כותב מחלקה (ולא קוד של ממשק) אתה אמור להיות כמה שיותר גנרי, הכוונה היא שלא משנה מי ישתמש בה ואיך יהיה לו פשוט ואינטואיטיבי להשתמש בזה.

כלומר אתה לא יכול להגביל את המחלקה שלך לConsole project כי אם מחר תרצה להשתמש בזה בדף אינטרנט או חלון רגיל לא תוכל ותצטרך לכתוב קוד מחדש.

שורה תחתונה- תמיד תחשוב שיהיה מישהו אחר שישתמש במחלקה הזאת, תזכור שמחלקות עזר לא אמורות להכיר את הממשק ותנסה כמה שפחות לחבר בין מחלקות- כלומר להמעיט במצבים בהם מחלקות תלויות אחת בשניה, ואפילו יותר מצב בו יש תלות מעגלית. (כלומר שתי מחלקות שמתמשות אחת בשניה)

קישור לתוכן
שתף באתרים אחרים

כרגע הבעיה שלי היא שבכל איטרציה של המתודה היא תמיד תגיע בסוף ותחזיר -9999 וככה היא תצא ובעצם לא תסיים לקרוא את כל הקובץ

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;


namespace T380
{
public class BabyStreamReader : StreamReader
{
public BabyStreamReader(string path)
: base(path)
{


}


public int ReadNextInt()
{
int INum = 0;
string StNum = "";
ArrayList Numbers = new ArrayList();
BabyStreamReader InputFile = new BabyStreamReader("TextFile.txt");


char character = (char)InputFile.Read();


if (character >= '0' && character <= '9')
{
StNum += character;
}
else
{
INum = int.Parse(StNum);
StNum = "";
Numbers.Add(INum);


foreach (int i in Numbers)
{
return i;
}
}


return -9999;
}
}


class Program
{
static void Main(string[] args)
{
BabyStreamReader InputFile = new BabyStreamReader("TextFile.txt");

while (!InputFile.EndOfStream)
{
int result = InputFile.ReadNextInt();
Console.WriteLine(result);


}

Console.ReadLine();
}
}
}

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...