私は階層person
,student
およびteacher
を持っています。学生と教師を比較するためにC#で一般的な比較方法を作成する方法
person
が基本クラスであり、student
およびteacher
が派生クラスです。
Teacher
が給与を有し、Student
がMarks
を有する。 Student
またはteacher
オブジェクトを送信するかどうかにかかわらず、一般的な比較メソッドを実装したいと思います。
比較方法は、それを比較し、t1.salaray
がt2.salary
よりも大きいので、true
を戻す場合、同じ方法でs1.marks > s2.marks
を比較する必要がある場合true
を返します。
私のコードは次のとおりです。
namespace ConsoleApplication5
{
public class person
{
String name;
int cnic;
public String Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Cnic
{
get
{
return cnic;
}
set
{
cnic = value;
}
}
public void Marks
{
}
}
}
class student : person
{
String rollno;
double marks;
public String Rollno
{
get
{
return rollno;
}
set
{
rollno = value;
}
}
public double Marks
{
get
{
return marks;
}
set
{
marks = value;
}
}
}
}
class Teacher : person
{
int salary;
public int Salary
{
get
{
return salary;
}
set
{
salary = value;
}
}
}
}
**Main class is:**
class Program
{
static void Main(string[] args)
{
student o1 = new student();
o1.Marks = 78.0;
o1.Name = "Abuzar";
Console.WriteLine();
Console.WriteLine("Student: 1");
Console.WriteLine("Name:" + o1.Name);
Console.WriteLine("Marks:" + o1.Marks);
Console.WriteLine("Roll No:" + o1.Rollno);
student o2 = new student();
o2.Marks = 78.0;
o2.Name = "Abuzar";
Console.WriteLine();
Console.WriteLine("Student: 2");
Console.WriteLine("Name:" + o2.Name);
Console.WriteLine("Marks:" + o2.Marks);
Console.WriteLine("Roll No:" + o2.Rollno);
student o3 = new student();
o3.Marks = 80.5;
o3.Name = "Liaqat";
o3.Rollno = "FA14-BSE-123";
Console.WriteLine();
Console.WriteLine("Student: 3");
Console.WriteLine("Name:" + o3.Name);
Console.WriteLine("Marks:" + o3.Marks);
Console.WriteLine("Roll No:" + o3.Rollno);
student o4 = new student();
o4.Marks = 74;
o4.Name = "Mohsin";
o4.Rollno = "FA14-BSE-123";
Console.WriteLine();
Console.WriteLine("Student: 4");
Console.WriteLine("Name:" + o4.Name);
Console.WriteLine("Marks:" + o4.Marks);
Console.WriteLine("Roll No:" + o4.Rollno);
student o5 = new student();
o5.Marks = 45.0;
o5.Name = "Asad";
o5.Rollno = "FA14-BSE-201";
Console.WriteLine();
Console.WriteLine("Student: 5");
Console.WriteLine("Name:" + o5.Name);
Console.WriteLine("Marks:" + o5.Marks);
Console.WriteLine("Roll No:" + o5.Rollno);
Teacher t1 = new Teacher();
t1.Name = "Asad Rafiq";
t1.Salary = 2500;
t1.Cnic = 17301 - 2568 - 89;
Console.WriteLine();
Console.WriteLine("Teacher: 1");
Console.WriteLine("Name:" + t1.Name);
Console.WriteLine("Cnic:" + t1.Cnic);
Console.WriteLine("Salary:" + t1.Salary);
Teacher t2 = new Teacher();
t2.Name = "Sara irum";
t2.Salary = 30000;
t2.Cnic = 17301 - 2512 - 80;
Console.WriteLine();
Console.WriteLine("Teacher: 2");
Console.WriteLine("Name:" + t2.Name);
Console.WriteLine("Cnic:" + t2.Cnic);
Console.WriteLine("Salary:" + t2.Salary);
Teacher t3 = new Teacher();
t3.Name = "Sanam Ali";
t3.Salary = 2500;
t3.Cnic = 17301 - 2568 - 89;
Console.WriteLine();
Console.WriteLine("Teacher: 3");
Console.WriteLine("Name:" + t3.Name);
Console.WriteLine("Cnic:" + t3.Cnic);
Console.WriteLine("Salary:" + t3.Salary);
Teacher t4 = new Teacher();
t4.Name = "Abbas khalid";
t4.Salary = 80000;
t4.Cnic = 17301 - 2568 - 89;
Console.WriteLine();
Console.WriteLine("Teacher: 4");
Console.WriteLine("Name:" + t4.Name);
Console.WriteLine("Cnic:" + t4.Cnic);
Console.WriteLine("Salary:" + t4.Salary);
Teacher t5 = new Teacher();
t5.Name = "Sana Malik";
t5.Salary = 90000;
t5.Cnic = 17301 - 2168 - 89;
Boolean b = o1.Compar(o2);
}
}
}
あなたの問題はどこですか?あなたのすべてのクラスで 'object.Equals'をオーバーライドし、あなたが行った比較ロジックでオーバーライドします。 Btw:自動プロパティを使用してコードを減らし、投稿内でスクロールしやすくするため、 'int MyProp {get myValue; }セット{myValue =値; }} '使用する' MyProp {get;セット; } '。 – HimBromBeere
GetTypeを使用してクラス名を取得します。学生学生=新しい学生(); タイプtype = student。GetType(); –
どこにあなたの情報を印刷するために、各オブジェクトのToString()をオーバーライドするかを、 – Ogbe