如何创建具有大小(长度和宽度),范围(1-13)内的数字和颜色的类。 [英] How do I create a class that has size(lenght and width) , a number within a range(1-13) and a color.

查看:87
本文介绍了如何创建具有大小(长度和宽度),范围(1-13)内的数字和颜色的类。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题需要一个单词说例如一个Tile类,它将保持大小,数字在一个范围内(1-13)并为其添加颜色。

该课程应该有一个或多个属性..



我尝试过:



The question require a single class say for example a Tile class that will hold the size ,number within a range(1-13) and add color to it.
The class should have one or more attributes ..

What I have tried:

public class Tile {
    
  public int _Start  ,_End, _Lenght,_width;
  public Range(int start, int end) {
    _Start = start;
    _End = end;

        public Color MyColor
    {
        get
        {
            return myColor;
        }
        set
        {
            myColor = value;
        }
    }

  }
Public int Start
{
Get
{
Return _Start;
}
Set
{
value = 1;
}
}
Public int End
{
Get
{
Return _End
}
Set 
{
Value = 13;
}
}

推荐答案

首先,请记住C#区分大小写:Public不是与公共相同,不会编译。这同样适用于获取和设置。你还需要声明变量,坚持一致的命名约定是一个非常好的主意,而不是将它们混合起来。



试试这个:

First off, remember that C# is case sensitive: "Public" is not the same as "public" and will not compile. The same applies to "Get" and "Set". You also need to declare variables, and it's a very good idea to stick to consistent naming conventions, rather then mixing them up as you are.

Try this:
public class Tile
    {
    private int _Number = 1;
    public int Height { get; set; }
    public int Width { get; set; }
    public Color Color { get; set; }
    public int Number
        {
        get { return _Number; }
        set
            {
            if (value < 1 || value > 13) throw new ArgumentException("Number out of range: " + value.ToString());
            _Number = value;
            }
        }
    }


这篇关于如何创建具有大小(长度和宽度),范围(1-13)内的数字和颜色的类。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆