Playing with cubes II
2015-09-27 21:15
176 查看
Description:
Hey Codewarrior!You already implemented a Cube class, but now we need your help again! I'm talking about constructors. We don't have one. Let's code two: One taking an integer and one handling no given arguments!
Also we got a problem with negative values. Correct the code so negative values will be switched to positive ones!
The constructor taking no arguments should assign 0 to Cube's Side property.
http://www.codewars.com/kata/playing-with-cubes-ii/csharp
using System; public class Cube { private int Side; //This cube needs your help. //Define a constructor which takes one integer and assignes its value to 'Side' public Cube() {} public Cube(int side) { this.Side = Math.Abs(side); } public int GetSide() { return this.Side; } public void SetSide(int s) { this.Side = Math.Abs(s); } }
可以使用this来处理构造函数,无参构造函数,字段的默认值,可以通过调用有参函数来处理
赋值的时候,可以调用类本身的函数
public class Cube { private int Side; //This cube needs your help. //Define a constructor which takes one integer and assignes its value to 'Side' public Cube(int s) { SetSide(s); } public Cube() : this(0) { } public int GetSide() { return Side; } public void SetSide(int s) { Side = System.Math.Abs(s); } }
可以使用函数的缺省参数
using System; public class Cube { private Int32 _side; public Cube(Int32 Side = 0){ SetSide(Side); } public Int32 GetSide() { return this._side; } public void SetSide(Int32 Side) { this._side = Math.Abs(Side); } }
相关文章推荐
- TSM-Install
- 服务器与客户端(2):多线程
- sql 2005 应用经验技巧与备忘
- java SE复习笔记47
- 黑马程序员----C 语言学习笔记之数组指针与字符串指针
- hibernate总结-缓存
- 存储器管理之页面置换算法
- 指向类成员函数的指针
- (MAC)Warning: mysql_connect(): No such file or directory
- 找出最长的胖子
- java SE复习笔记46
- js 变量作用域
- 信息安全系统设计基础 第二周学习笔记
- BufferedInputStream读取文本文件
- 服务器与客户端(1):单线程
- 百度2015校园招聘——前端笔试题
- uboot全局数据gd_t、bd_t和device_t
- windows上安装clang教程
- mac下卸载android studio
- 使用adb将tcpdump的流量导到wireshark