Codewars 8 Kyu :Basic subclasses – Adam and Eve

题目

According to the creation myths of the Abrahamic religions, Adam and Eve were the first Humans to wander the Earth.

You have to do God’s job. The creation method must return an array of length 2 containing objects (representing Adam and Eve). The first object in the array should be an instance of the class Man. The second should be an instance of the class Woman. Both objects have to be subclasses of Human. Your job is to implement the Human, Man and Woman classes.

代码

using System.Linq;
using System.Collections.Generic;
public class God
{ 
  public static Human[] Create()//该方法的作用是创建Human类型的数组(Human[])
  {
    //返回的是一个数组,因此声明一个类型为Human的list
    var list=new List<Human>();
    //实例Man类和Woman类
    Man adam=new Man();
    Woman eva=new Woman();
    //将两个实例添加进列表
    list.Add(adam);
    list.Add(eva);
    //调用ToArray,将列表转换为数组返回
    return list.ToArray();// TODO: Return an array containing a Man and Woman
  }
}

public class Human{//父类,Human类,list中存储的都是Human类型的数据
 public Human(){}//通过构造函数创造实例
}

public class Man:Human{//Man是Human的子类
  public Man(){}//通过构造函数创造实例
}

public class Woman:Human{//Woman是Human的子类
  public  Woman(){}//通过构造函数创造实例
}

参考资料

  1. 构造函数:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/constructors
  2. 类:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/classes
  3. 继承:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/inheritance

发表评论

您的电子邮箱地址不会被公开。