1.创建一个控制台应用程序Exp06_01,在该程序中定义一个School类,该类中有一个私有的整型数组grade,用来存储学生的成绩,还有一个公共的整型索引器,用来读写grade数组中的各元素,该类中还有一个公共的构造函数,给grade数组初始化为有10个元素。在Program类的Main()方法中声明一个School类实例,然后利用索引来对grade数组中的元素进行赋值和显示。
namespace Exp06_01
{
class Program
{
public static void Main(string[] args)
{
School school=new School();
for(int i=0;i<10;i++){
school[i]=Convert.ToInt32(Console.ReadLine());
}
for(int i=0;i<10;i++){
Console.Write($"{school[i]} ");
}
}
}
class School
{
private int[] grade;
public School()
{
grade = new int[10];
}
public int this[int index]
{
set
{
if (index >= 0 && index < 10)
grade[index] = value;
}
get
{
if (index >= 0 && index < 10)
return grade[index];
else
return -1;
}
}
}
}
C# 索引器
<访问修饰符><返回值类型> this[<索引值类型> index(索引名)]
{
get {
//get构造
}
set {
//set构造
}
}
2.创建一个控制台应用程序Exp06_02
a.在该应用程序中创建一个公共的类Shape,该类中包含一个受保护的字符串字段Color,两个公共的构造函数,一个无参数,无实现代码;另一个含一个字符串参数,在该构造函数中,把参数赋给字段Color。有一个公共的方法GetColor(),该方法用来返回Color字段的值。 还有一个公共的返回值为double型的虚方法GetArea(),该方法的返回值为0.0
b. 该应用程序中还包含一个公共的Circle类,该类从Shape类中派生,该类含一个私有的double型的字段Radius,一个公共的构造函数,该构造函数有两个参数,其中一个给Color赋值,另一个给Radius赋值。对基类中的虚方法GetArea()进行重写,用来返回圆的面积。
c. 该应用程序中还包含一个公共的Rectangular类,该类从Shape类中派生,该类含两个受保护的double型的字段Width和Length,两个公共的构造函数,一个无参数的,用来给Width和Length都赋0;另一个构造函数有三个参数,其中一个给Color赋值,一个给Width赋值。还有一个给Length赋值。另外,该类中也要对基类中的虚方法GetArea()进行重写,用来返回长方形的面积。还存在一个公共的方法GetPerimeter(),用来返回长方形的周长。
d.该应用程序中还包含一个公共的Square类,该类从Rectangular类中派生,该类含一个公共的构造函数,该构造函数有两个参数,其中一个给Color赋值,另一个给Width和Length赋相同的值。
e.在Program类的Main()方法中定义这几种形状的实例,然后输出它们的面积及周长(圆的周长不用求)。
using System;
namespace Exp06_02
{
class Program
{
public static void Main(string[] args)
{
Circle circle = new Circle("blue", 1);
Console.WriteLine(circle.GetArea());
Rectangular rectangular = new Rectangular("red", 1, 2);
Console.WriteLine(rectangular.GetArea());
Console.WriteLine(rectangular.GetPerimeter());
Square square = new Square("green", 1);
Console.WriteLine(square.GetArea());
Console.WriteLine(square.GetPerimeter());
}
}
class Shape
{
private string Color;
public Shape() { }
public Shape(string Color)
{
this.Color = Color;
}
public string GetColor()
{
return this.Color;
}
virtual public double GetArea()
{
return 0.0;
}
}
class Circle : Shape
{
private double Radius;
public Circle(string Color, double Radius) : base(Color)
{
this.Radius = Radius;
}
override public double GetArea()
{
return Radius * Radius * Math.PI;
}
}
class Rectangular : Shape
{
protected double Width;
protected double Length;
public Rectangular()
{
Width = 0;
Length = 0;
}
public Rectangular(String Color, double Width, double Length) : base(Color)
{
this.Width = Width;
this.Length = Length;
}
override public double GetArea()
{
return Width * Length;
}
public double GetPerimeter()
{
return (Width + Length) * 2;
}
}
class Square : Rectangular
{
public Square(String Color, double Length) : base(Color, Length, Length) { }
}
}
使用base调用的基类构造函数
public class BaseClass
{
public BaseClass(){}
public BaseClass(int i){}
}
public class DerivedClass : BaseClass
{
public DerivedClass() : base(){}
public DerivedClass(int i) : base(i){}
static void Main()
{
DerivedClass sample1 = new DerivedClass();//调用DerivedClass和BaseClass的无参构造
DerivedClass sample2 = new DerivedClass(1);//调用DerivedClass和BaseClass的有参构造
}
}
ps:在派生类中使用base调用的基类构造函数时,基类构造先于派生类构造执行
3.创建一个控制台应用程序Exp06_03
a. 创建一个控制台应用程序,在程序中定义一个基类MyClass,其中包含公共虚拟方法GetString().这个方法应返回存储在受保护字段myString中的字符串。该字段可以通过只写公共属性containedString来访问。该类还存在一个公共抽象方法PrintString()。
b. 创建一个控制台应用程序,在上一题的基础上,从类MyClass中派生一个类MyDerivedClass。重写GetString(),该方法的调用基类的GetString方法获得myString中的字符串,然后在字符串中后面添加文本”(out put from derived class)”后,把新字符串返回。重写基类中的抽象方法PrintString(),向控制台输出字符串“Hello World!”。
c. 在main方法中创建派生类MyDerivedClass的对象,然后给属性containedString赋值,再调用GetString()和PrintString()方法。
namespace Exp06_03
{
class Program
{
public static void Main(string[] args)
{
MyDerivedClass derivedClass = new MyDerivedClass();
derivedClass.containedString = "A String";
Console.WriteLine(derivedClass.GetString());
derivedClass.PrintString();
}
}
abstract class MyClass
{
protected string myString;
virtual public string GetString()
{
return myString;
}
public string containedString
{
set
{
myString = value;
}
}
public abstract void PrintString();
}
class MyDerivedClass : MyClass
{
override public string GetString()
{
return base.GetString() + @" out put from derived class";
}
override public void PrintString()
{
Console.WriteLine("Hello World!");
}
}
}
使用override重载virtual方法或实现abstract方法
抽象方法(abstract )与虚方法(virtual)的区别:
抽象方法:
1.没有方法体(没有具体实现)
2.抽象方法只能在抽象类中声明
3.派生类必须重写抽象类中的抽象方法
虚方法:
1.虚方法必须有实现部分
2..派生类可以重写虚方法,也可以不重写