2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【pythonとJava比較】ループでメソッド呼び出し

Last updated at Posted at 2021-05-20

pythonをやり始めてrange()っていいなと思ったのでJavaと比較して書いてみた。
※対象の処理だけならpythonだと短く書けるがJavaと同じことができる状態にした。

range()でメソッド呼び出し

python

test.py
import sys

class Test:
    """
    テストクラス
    """

    def main(args: list) -> None:
        """
        メイン処理
        :param args: コマンドライン引数
        """
        [Test.test(i) for i in range(3)]
    def test(intVal: int) -> None:
        """
        呼び出すメソッド
        :param intVal: パラメータ
        """
        print(intVal)

if __name__ == "__main__":
    args = sys.argv
    Test.main(args)

java

Test.java
import java.util.stream.IntStream;

/**
 * テストクラス
 */
public class Test {

	/**
	 * メイン処理
	 * @param args コマンドライン引数
	 */
	public static void main(String[] args) {
		IntStream.range(0, 3).forEach(i -> Test.test(i));
	}

	/**
	 * 呼び出しメソッド
	 * @param intVal パラメータ
	 */
	public static void test(int intVal) {
		System.out.println(intVal);
	}

}

listのメソッド呼び出し

python

test.py
import sys

class Test:
    """
    テストクラス
    """

    def main(args: list) -> None:
        """
        メイン処理
        """
        list = []
        list.append(Item(100, 'バナナ'))
        list.append(Item(200, 'もも'))
        [print(item) for item in list]
    def test(intVal) -> None:
        """
        呼び出すメソッド
        :param intVal: パラメータ
        """
        print(intVal)

class Item:
    def __init__(self, item_no: int, item_name: str) -> None:
        self.item_no = item_no
        self.item_name = item_name
    def get_item_no(self) -> str:
        return self.item_no
    def get_item_name(self) -> str:
        return self.item_name
    def __str__(self) -> str:
        return "{} {}".format(self.get_item_no(), self.get_item_name())

if __name__ == "__main__":
    args = sys.argv
    Test.main(args)

java

Test.java
/**
 * テストクラス
 */
public class Test {

	/**
	 * メイン処理
	 * @param args コマンドライン引数
	 */
	public static void main(String[] args) {
		List<Item> list = new ArrayList<Item>();
		list.add(new Item(100, "バナナ"));
		list.add(new Item(200, "もも"));
		list.stream().forEach(item -> System.out.println(item));
	}

}
Item.java
public class Item {

	private int itemNo;
	private String itemName;

	public Item(int itemNo, String itemName) {
		this.itemNo = itemNo;
		this.itemName = itemName;
	}

	public int getItemNo() {
		return itemNo;
	}
	public String getItemName() {
		return itemName;
	}

	@Override
	public String toString() {
		return String.format("%s %s", getItemNo(), getItemName());
	}
}
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?