Help us understand the problem. What is going on with this article?

Java基礎メモ

More than 3 years have passed since last update.

クラス,フィールド,メソッド

クラス

データ(メンバ)と処理(メソッド)をまとめたもの

フィールド

  • クラスがもつデータ
  • クラスの中に定義されたデータ(変数)下のコードでは,int型変数のiとString型変数のstr

メソッド

  • 処理
  • SampleClass.javaの例では,multiIntprintStr
  • メソッドの型は返り値の型
  • printStrのように返り値を持たないメソッドもある
メソッドの型 メソッド名(引数のデータの型 引数名){
    処理;
    return 戻り値;
}

メンバ

  • メソッドとフィールドを合わせてクラスのメンバと呼ぶ

SampleClass.java
Class A{ //<== 'A':クラス名

    //フィールド
    int i;      
    String str;

    //メソッド
    int multiInt(int j){
        return i * j;
    }

    void printStr(){
        System.out.println(str);
    }

}

オーバーロード

  • 同じクラス内に同じ名前で引数や戻り値が異なるメソッドを持つことができる

オーバーロード例
class A{
    int i;
    String str;

    //メソッド
    int multiInt(int j){
        return i * j;
    }

    //メソッド
    int multiInt(float f){
        return (int)(i * f);
    }


}
メソッドの呼び出し
int i = ols.multiInt(5);
int f = ols.multiInt(5.5f);
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away