Codewars 8 Kyu : Classy Extentions

题目

Classy Extensions, this kata is mainly aimed at the new JS ES6 Update introducing class extends You will be preloaded with the Animal class, so you should only edit the Cat class.

任务Task

Your task is to complete the Cat class which Extends Animal and replace the speak method to return the cats name + meows. e.g.
‘Mr Whiskers meows.’
The name attribute is passed with this.name (JS), @name (Ruby) or self.name (Python).

解题思路:

  1. 本题需要我们完善animal的子类cat类,需要给它写一个构造方法,同时override animal中的同名方法。
  2. animal中存在虚函数,同时是两个类中的同名方法,因此使用override,指重写,注意和重载区分,重载是指具有相同的方法名,通过改变参数的个数或者参数类型实现同名方法的不同实现。

代码

public class Cat : Animal
{

  public string name;
  public Cat(string name) : base(name)
  {
    this.name=name;
  }
  public override string Speak(){
      return $"{this.name} meows.";
    }
  // TODO: Override Animal's Speak method
}

参考资料

区分重写、重载、覆盖
https://blog.csdn.net/lichuanpeng/article/details/72793371
https://blog.csdn.net/u010926964/article/details/20719951

发表评论

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