C#實驗7 類和繼承

iamzxf發表於2015-05-05

1、運算子過載

    建立表示空間某點座標的類Space,並過載運算子“-”,得到某點關於原點的對稱座標。例如:某點座標為(x,y,z),那麼-(x,y,z)是其關於原點的對稱座標。

參考程式碼如下:

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

namespace ConsoleApplication1
{
    class Place
    {
        public double X { get; set; }
        public double Y { get; set; }
        public double Z { get; set; }

        public Place(double x, double y, double z)
        {
            this.X = x;
            this.Y = y;
            this.Z = z;
        }

        public static Place operator -(Place sp1)
        {
            return new Place(-sp1.X, -sp1.Y, -sp1.Z);
        }

    }
        
    class Program
    {
        static void Main(string[] args)
        {
            Place sp1 = new Place(1, 2, 3);
            Place sp2 = -sp1;

            Console.WriteLine("{0},{1},{2}",sp2.X,sp2.Y,sp2.Z);

            Console.ReadLine();

        }
    }
}


2、編寫控制檯應用程式,完成下列要求:

    (1)定義一個類Student,包含欄位及對應的屬性:ID(學號)、Name(姓名)、Department(院系)、Courses(選修課程,字串陣列);虛方法Introduce(顯示姓名及學號);

    (2)Student類定義索引器,根據下標索引選修課;過載索引器,根據選修課程名索引所在的下標;

    (3)類GgraduateStudent繼承自類Student,具有自己的欄位及對應的只讀屬性Research(研究方向);

    (4)類Student和類GraduateStudent均包含無參、有參的建構函式;

    (5)類GraduateStudent重寫Introduce方法。

參考程式碼如下:

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

namespace ConsoleApplication1
{
    class Student
    {
        //ID(學號)、Name(姓名)、Department(院系)、Courses(選修課程,字串陣列);虛方法Introduce(顯示姓名及學號);
        private int id;
        private string name;
        private string department;
        private string[] courses=new string[5];

        public int Id {
            get { return id; }
            set { this.id = value; }
        }

        public string Name
        {
            get { return name; }
            set { this.name = value; }
        }

        public string Department {
            get { return department; }
            set
            {
                this.department = value;
            }
        }

        public string this[int index]
        {
            get { return courses[index]; }
            set { courses[index] = value; }
        }

        public int this[string course]
        {
            get {
                for (int i = 0; i < courses.Length; i++)
                {
                    if (courses[i] == course)
                        return i;
                }
                return -1;
            }
        }

        public virtual void Introduce()
        {
            Console.WriteLine("Hi, I am a student, my id is {0}, and my name is {1}",this.id, this.name);
        }

        public Student()
        {
            Console.WriteLine("no parameter function of student");
        }

        public Student(int id, string name, string department)
        {
            this.id = id;
            this.name = name;
            this.department = department;
        }
    }

    class graduateStudent : Student
    {
        private string research;

        public String Research {
            get { return research; }
            set { this.research = value; }
        }

        public graduateStudent()
        {
            Console.WriteLine("no parameter function of graduate student");
        }

        public graduateStudent(int id, string name, string department, string research)
            : base(id, name, department)
        {
            this.research = research;
        }

        public override void Introduce()
        {
            Console.WriteLine("I am a graduate student, my id is {0}, and my name is {1}", this.Id, this.Name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Student stu1 = new Student();
            stu1.Introduce();

            Student stu2 = new Student(12, "zhang", "xindian");
            stu2[0]="computer science";
            stu2[1]="software";

            Console.WriteLine("the first course is {0}",stu2[0]);
            Console.WriteLine("the {0}-th course is software", stu2["software"]);


            graduateStudent grad1=new graduateStudent();
            grad1.Introduce();

            graduateStudent grad2=new graduateStudent(23,"lisi","software","image analysis");
            grad2.Introduce();


            Console.ReadLine();

        }
    }
}


考慮課程及只讀欄位時,參考程式碼如下:

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

namespace ConsoleApplication1
{

    class Student
    {
        private int id;
        private string name;
        private string department;
        private string[] courses;

        public int Id { get { return this.id; } set { this.id = value; } }
        public string Name { get { return this.name; } set { this.name = value; } }
        public string Department { get { return this.department; } set { this.department = value; } }

        public string this[int index]
        {
            get { return courses[index]; }
            set { courses[index] = value; }
        }

        public int this[string course]
        {
            get {
                for (int i = 0; i < courses.Length; i++)
                {
                    if (courses[i] == course)
                        return i;
                }

                return -1;
            }
        }

        public Student()
        { }

        public Student(int id, string name, string department, string[] courses)
        {
            this.id = id;
            this.name = name;
            this.department = department;
            this.courses = courses;
        }

        public virtual void Introduce()
        {
            Console.WriteLine("I am a student, my id is {0}, and my name is {1}",this.Id,this.name);
        }
    }

    class graduateStudent : Student
    {
        private readonly string research;

        public string Research {
            get { return this.research; }     
        }

        public graduateStudent()
        { }

        public graduateStudent(int id, string name, string department, string research, string[] courses)
            : base(id, name, department, courses)
        {
            this.research = research;
        }

        public override void Introduce()
        {
            Console.WriteLine("I am a graduate student, my id is {0}, and my name is {1}", this.Id, this.Name);
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Student stu1 = new Student();
            Student stu2 = new Student(12,"zhang","xindian", new string[]{"computer","software","information"});

            Console.WriteLine("the second course is {0}",stu2[1]);
            Console.WriteLine("the {0}-th course is information", stu2["information"]+1);
            stu2.Introduce();

            graduateStudent grad1 = new graduateStudent();
            graduateStudent grad2 = new graduateStudent(234, "lisi", "math", "image analysis", new string[] { "computer", "software", "image" });


            Console.WriteLine("the second course is {0}", grad2[1]);
            Console.WriteLine("the {0}-th course is image", grad2["image"]+1);
            grad2.Introduce();


            

            Console.ReadLine();

        }
    }
}



相關文章