2017-01-15 8 views
1

私は初心者です。色のグレースケール値を取得するために、さまざまなこと(赤、緑、青、アルファ値)を行うカラークラスを持つプログラムを作成しようとしています。しかし、私はどのようにBaseクラスのメンバ変数に値を代入するのか分かりません。
まず、私は、私はC#:BASEクラスのメンバー変数に値を代入して取得する方法

Color color = new Color(
    Convert.ToByte(
    Console.ReadLine() 
), Convert.ToByte(
    Console.ReadLine() 
), Convert.ToByte(
    Console.ReadLine() 
), 255 
); 
(I人々が値を入力することができるようにする)の色変数を宣言し、この

private byte red; 
private byte green; 
private byte blue; 
private byte alpha; 
public Color(byte red, byte green, byte blue, byte alpha) 
{ 
    this.red = red; 
    this.green = green; 
    this.blue = blue; 
    this.alpha = alpha; 
} 

ように、赤、青、緑、およびアルファ値をとるコンストラクタを作成します

それは正しいですか?赤い変数は、ユーザーが入力する値に割り当てられますか?

それが正しい場合、入力する前にユーザーにどのように質問できますか?例えば
は、彼らが入力赤の値を前に、私はそれらを聞いてきます:

入力あなたの赤の値

その後、私は彼らに

入力あなたの緑の値

を聞いてきます

これらの値を入力し続けるなど...

もう1つの問題:カラーオブジェクトから赤、緑、青の値を取得(取得)するために、カラークラスにメソッドを作成する必要があります。私はそれらを作りましたが、それが正しいかどうかわかりません。あなたは私のためにそれを確認できますか?

public byte Getred(byte red) 
{ 
    return red; 
} 

public byte Getgreen(byte green) 
{ 
    return green; 
} 

public byte Getblue (byte blue) 
{ 
    return blue; 
} 

public byte Getalpha(byte alpha) 
{ 
    alpha = 255; 
    return alpha; 
} 

答えて

2

のような性質の代わりgetmethodsを使用する場合があります。

Console.WriteLine("Please enter your red value: "); 
byte redValue = Convert.ToByte(Console.ReadLine()); 

Console.WriteLine("Please enter your green value: "); 
byte greenValue = Convert.ToByte(Console.ReadLine()); 

Console.WriteLine("Please enter your blue value: "); 
byte blueValue = Convert.ToByte(Console.ReadLine()); 

Color color = new Color(redValue, greenValue, blueValue, 255); 

値を取得する方法は、プライベートにしたい場合は正しいですし、特定の方法でしか公開しないでください。

編集:

あなたが唯一のクラスのフィールドはクラス内から変更することができますが、他の発信者が唯一の値を取得することができ、それを設定していない、あなたはプロパティを使用することができ、保存されますこれにしたい場合あなたはそれらのgetメソッドを書いています。

public class Color 
{ 
    public byte Red { get; private set; } 
    public byte Green { get; private set; } 
    public byte Blue { get; private set; } 
    public byte Alpha { get; private set; } 

    public Color(byte red, byte green, byte blue, byte alpha) 
    { 
     this.Red = red; 
     this.Green = green; 
     this.Blue = blue; 
     this.Alpha = alpha; 
    } 
} 

Color color = new Color(100, 100, 100, 255); 
byte redValue = color.Red; 
color.Red = 0; // Error, cannot set value outside the class. 
+0

ありがとう、私の方法については何ですか?彼らについてのsthを教えてください、pls? –

+0

@VũĐứcDũng私は自分の答えを更新しました。設定値をクラスに限定したいが、クラスの他のすべての呼び出し元を取得できるようにするには、フィールドの代わりにプロパティを使用します。 –

1

は、C#の場合、あなたは、ユーザーにプロンプ​​トメッセージを表示し、それらからの入力を受け取るためにConsole.WriteLineを使用することができます

class MyColor 
    { 
     private byte red; 
     private byte green; 
     private byte blue; 
     private byte alpha; 
     public MyColor(byte red, byte green, byte blue, byte alpha) 
     { 
      this.red = red; 
      this.green = green; 
      this.blue = blue; 
      this.alpha = alpha; 
     } 

     public byte Red 
     { 
      get 
      { 
       return this.red; 
      } 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      byte red; 

      while (true) 
      { 
       Console.WriteLine("Input a byte in the range of 0 to 255 - for Red:"); 
       string line = Console.ReadLine(); 
       if (line.Length > 3 || !byte.TryParse(line, out red)) 
       { 
        Console.WriteLine("Invalid Entry - try again"); 
        Console.WriteLine("Input a byte in the range of 0 to 255 - for Red:"); 
       } 
       else 
       { 
        break; 
       } 
      } 
     } 
    } 
+0

ありがとうございました。残念ながら、私はまだプロパティを学んでいない:(そう私の方法は右ですか? –

関連する問題