2012-03-29 32 views
0

基本的に、自分のフォームから値を取得する新しい単純なJavaクラスを作成する必要があります(プロセスとして設計し、 Javaクラスのメソッドが呼び出されると、Javaクラスはコンソールまたはテキストファイルのフォームから取得した値(例:system.println.out ...)を単に出力するだけです。インスタンスパラメータを使用してJavaクラスを作成し、パラメータを取得して結果を出力する

いくつかのインスタンスパラメータでクラスを作成します。これらのパラメータの初期値を示す行を出力します。

私はJavaを使い始めたばかりで、数日前には始まったばかりですが、プロジェクトの一環としてこの要件を満たしています。

このJavaクラスを書くのに助けてください。

+2

ここでコードスニペットは、これまでに何を試しましたか? –

+1

この宿題はありますか?もしそうなら、そのようにタグを付けます。 – mre

答えて

1

何か間違った記述をしようとする前に、Javaのクラスコンストラクタの概念を理解するために、いくつかのJava初心者の本(またはjavadoc)を読むことをお勧めします。

ラフクラスは、このようなことがあり

public class myClass{ 
    int param1; 
    int param2; 

    public myClass(int firstparam, int secondparam){ 
    this.param1 = firstparam; 
    this.param2 = secondparam; 
    } 
} 

public static void main(){ 
    myClass c = new myClass(1,2); 
    System.out.println(c.param1 + c.param2); 
} 

あなたがこれを理解していない場合は、Javaの基礎を学ぶしてください。..

1

あなたは、単にようなクラスとその建設業者作成することができます。

public class Test { 

    //a string representation that we will initialize soon 
    private String text; 

    //Firstly you have to instantiate your Test object and initialize your "text" 
    public Test(String text) { 
     this.text = text; 
     //System.out.println(text); 
     //You can print out this text directly using this constructor which also    
     //has System.out.println() 
    } 

    //You can just use this simple method to print out your text instead of using the 
    //constructor with "System.out.println" 
    public void printText() { 
     System.out.println(this.text);//"this" points what our Test class has 
    } 

} 

このクラスを使用するようですが:

public class TestApp { 
     public static void main(String[] args) { 

      Test testObject = new Test("My Text"); 

      /*if you used the constructor with System.out.println, it directly prints out 
      "My Text"*/ 

      /*if your constructor doesn't have System.out.println, you can use our  
      printText() method //like:*/ 
      testObject.printText(); 
     } 
    } 
関連する問題