C#實驗7 類和繼承
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();
}
}
}
相關文章
- C# OOP:繼承,介面和抽象類C#OOP繼承抽象
- C#中類的繼承C#繼承
- JS原型繼承和類式繼承JS原型繼承
- UML類圖(上):類、繼承和實現繼承
- C# 繼承 子類(派生類) 父類(基類)C#繼承
- 類的繼承_子類繼承父類繼承
- es5繼承和es6類和繼承繼承
- 類的繼承和派生繼承
- C#類繼承自泛型集合C#繼承泛型
- 實驗4 類的組合、繼承、模板類、標準庫繼承
- 【c#】繼承C#繼承
- C# 繼承C#繼承
- 征服 JavaScript 面試:類繼承和原型繼承的區別JavaScript面試繼承原型
- c# abstract抽象類及抽象方法_繼承C#抽象繼承
- Java之繼承和抽象類Java繼承抽象
- [C++]繼承和派生類C++繼承
- 繼承 重寫和抽象類繼承抽象
- C#如何實現多重繼承C#繼承
- 從本質認識JavaScript的原型繼承和類繼承JavaScript原型繼承
- c#繼承comC#繼承
- 類的繼承繼承
- javascript類繼承JavaScript繼承
- 子承父業-C#繼承C#繼承
- C#介面、抽象類、普通類和繼承(子類與父類)都有其特定的用途和場景C#抽象繼承
- Java 繼承與多型實驗Java繼承多型
- c#中判斷類是否繼承於泛型基類C#繼承泛型
- C#虛基類繼承與介面的區別C#繼承
- C#中繼承和多型的研究C#中繼繼承多型
- 瞭解下C# 繼承C#繼承
- TypeScript 介面繼承類TypeScript繼承
- 原型繼承:子類原型繼承
- C++ | 類繼承C++繼承
- iOS 繼承&類方法iOS繼承
- Java:類與繼承Java繼承
- js 原型鏈實現類的繼承JS原型繼承
- 繼承 基類與派生類繼承
- c# abstract抽象類與繼承類子類的建構函式_baseC#抽象繼承函式
- C#中的繼承(一)C#繼承