JavaのSystem.out.println()が長いからラップしたいと思っていた。
このUtilクラスを継承すればラッパー関数p()でSystem.out.println()を呼び出せるようになった。
ただし無引数では呼び出せない。オーバーロードすれば無引数でも呼び出せるだろうけれど。
JavaのSystem.out.println()が長いからラップしたいと思っていた。
1 2 3 4 5 |
class Util { public static <T> void p(T arg) { System.out.println(arg); } } |
このUtilクラスを継承すればラッパー関数p()でSystem.out.println()を呼び出せるようになった。
ただし無引数では呼び出せない。オーバーロードすれば無引数でも呼び出せるだろうけれど。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import java.util.*; public class Main { public static void main(String[] args) { Zoo.main(); } } class Util { public static <T> void p(T arg) { System.out.println(arg); } } class Zoo extends Util { public static void main() { Animal a; a = new Cat(3); p(a.makeSound()); a = new Mike(7); p(a.makeSound()); } } abstract class Animal { protected int age; public Animal(int age) { this.age = age; } public int getAge() { return this.age; } abstract public String makeSound(); } class Cat extends Animal { public Cat(int age) { super(age); } public String makeSound() { String sound = "ニャー(" + this.getAge() + ")"; return sound; } } class Mike extends Cat { public Mike(int age) { super(age); } public String makeSound() { String sound = "ミケニャー(" + this.getAge() + ")"; return sound; } } |
1 2 |
ニャー(3) ミケニャー(7) |
new演算子とメンバ参照をワンライナーで書いたとき、
Java は new演算子である
PHP は メンバ参照である
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.*; public class Main { public static void main(String[] args) throws Exception { // new演算子が優先 System.out.println(new MyNum(8492).getNum()); } } class MyNum { protected int num; public MyNum(int num) { this.num = num; } public int getNum() { return this.num; } } |
1 |
8492 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php // メンバ参照が優先のためエラーになる // print_r(new MyNum(123)->getNum()); // new演算子の結合を優先してあげると正常終了する print_r((new MyNum(8492))->getNum()); class MyNum { protected $num; public function __construct(int $num) { $this->num = $num; } public function getNum(): int { return $this->num; } } |
1 |
8492 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.*; /* 60%を3回試行と80%を2回試行ではどちらの期待値が高いか */ public class Main { public static void main(String[] args) throws Exception { int n = 2000000; double result60 = Util.pパーセントの確率にt回挑戦したときに1回以上成功した割合_試行回数n(60, 3, n); double result80 = Util.pパーセントの確率にt回挑戦したときに1回以上成功した割合_試行回数n(80, 2, n); double result60Analitical = Util.pパーセントの確率にt回挑戦したときに1回以上成功した割合_解析解(60, 3); double result80Analitical = Util.pパーセントの確率にt回挑戦したときに1回以上成功した割合_解析解(80, 2); System.out.println("計算解60 : " + result60 + ", 解析解 : " + result60Analitical); System.out.println("計算解80 : " + result80 + ", 解析解 : " + result80Analitical); } } class Util { public static Random random = new Random(); public static double pパーセントの確率にt回挑戦したときに1回以上成功した割合_解析解(double percent, int times){ double analyticalSolution = 100.0 - Math.pow(1 - percent / 100.0, times) * 100; return analyticalSolution; } public static double pパーセントの確率にt回挑戦したときに1回以上成功した割合_試行回数n(double percent, int times, int n) { int cnt = 0; for(int i=0; i<n; i++){ if(pパーセントの確率にt回挑戦して1回でも成功したらtrueを返す(percent, times)){ cnt++; } } double proportion = cnt * 100.0 / n; return proportion; } public static double _0より大かつ100より小の少数を返す() { return random.nextDouble() * 100; } public static boolean pパーセントの確率に1回挑戦した結果を返す(double percent) { return _0より大かつ100より小の少数を返す() < percent; } public static boolean pパーセントの確率にt回挑戦して1回でも成功したらtrueを返す(double percent, int times) { for(int i=0; i<times; i++){ if(pパーセントの確率に1回挑戦した結果を返す(percent)){ return true; } } return false; } } |
1 2 |
計算解60 : 93.58305, 解析解 : 93.6 計算解80 : 96.00985, 解析解 : 96.0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.*; public class Main { public static void main(String[] args) throws Exception { List<オリンピック> ships = new ArrayList<>(); ships.add(new オリンピック("オリンピック")); ships.add(new オリンピック("タイタニック")); ships.add(new オリンピック("ブリタニック")); ships.forEach(ship -> System.out.println(ship)); } } class オリンピック { public String name; public オリンピック (String name) { this.name = name; } @Override public String toString() { return "この艦はオリンピック級" + this.name + "号です"; } } |
1 2 3 |
この艦はオリンピック級オリンピック号です この艦はオリンピック級タイタニック号です この艦はオリンピック級ブリタニック号です |
・抽象クラスもコンストラクタを持てる
・親クラスのコンストラクタから順に起動する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.*; public class Main { public static void main(String[] args) throws Exception { var a = new 三毛猫(12); } } abstract class Animal { public int age; abstract public void 鳴く(); public Animal(int age) { this.age = age; System.out.println("Animal コンストラクタ終了"); } } class Cat extends Animal { public Cat(int age) { super(age); System.out.println("Cat コンストラクタ終了"); } public void 鳴く() { System.out.println("ニャー(" + this.age + ")"); } } class 三毛猫 extends Cat { public 三毛猫(int age) { super(age); System.out.println("三毛猫 コンストラクタ終了"); } public void 鳴く() { System.out.println("ミケニャー(" + this.age + ")"); } } |
1 2 3 |
Animal コンストラクタ終了 Cat コンストラクタ終了 三毛猫 コンストラクタ終了 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.*; public class Main { public static void main(String[] args) throws Exception { オリンピック級 オリンピック号 = new オリンピック級("オリンピック号"); オリンピック級 タイタニック号 = new オリンピック級("タイタニック号"); オリンピック級 ブリタニック号 = new オリンピック級("ブリタニック号"); List<IShip> ships = new ArrayList<>(); ships.add(オリンピック号); ships.add(タイタニック号); ships.add(ブリタニック号); for(IShip ship : ships) { ship.ヨーソロー(); } } } interface IShip { public abstract void ヨーソロー(); } class オリンピック級 implements IShip { public String name; public オリンピック級(String name) { this.name = name; } @Override public void ヨーソロー() { System.out.println(name + "、ヨーソロー!"); } } |
1 2 3 |
オリンピック号、ヨーソロー! タイタニック号、ヨーソロー! ブリタニック号、ヨーソロー! |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Member taro = new Member("太郎", 31); Member hanako = new Member("花子"); System.out.println("taro : " + taro); System.out.println("hanako : " + hanako); } } class Member { private String name; private Optional<Integer> age; public Member(String name) { this.name = name; this.age = Optional.ofNullable(null); } public Member(String name, int age) { this.name = name; this.age = Optional.ofNullable(age); } public String getProfile() { return this.age .map(a -> String.format("%s(%d)", this.name, a)) .orElse(this.name); } @Override public String toString() { return this.getProfile(); } } |
1 2 |
taro : 太郎(31) hanako : 花子 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Animal a; a = new Cat(3); a.say(); a = new Mike(5); a.say(); } } interface Sayable { void say(); } abstract class Animal implements Sayable { protected int age; public Animal(int age) { this.age = age; } public int getAge() { return this.age; } } class Cat extends Animal { public Cat(int age) { super(age); } public void say() { System.out.println("ニャー(" + this.age + ")"); } } class Mike extends Cat { public Mike(int age) { super(age); } public void say() { System.out.println("ミケニャー(" + this.age + ")"); } } |
1 2 |
ニャー(3) ミケニャー(5) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) throws Exception { List<Fruit> fruitsBucket = new ArrayList<>(); fruitsBucket.add(new Fruit("りんご")); fruitsBucket.add(new Fruit("みかん")); fruitsBucket.add(new Fruit("オレンジ")); List<Juice> refrigerator = fruitsBucket.stream() .map(f -> new Juice(f.type)) .collect(Collectors.toList()); System.out.println("fruitsBucket : " + fruitsBucket); System.out.println("refrigerator : " + refrigerator); } } class Fruit { public String type; public Fruit(String type) { this.type = type; } public String toString() { return type; } } class Juice { public String type; public Juice(String type) { this.type = type; } public String toString() { return type + "ジュース"; } } |
1 2 |
fruitsBucket : [りんご, みかん, オレンジ] refrigerator : [りんごジュース, みかんジュース, オレンジジュース] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.*; import java.util.concurrent.Callable; public class Main { public static void main(String[] args) { Callable callable = () -> "ラムダ式が返した文字列"; try{ System.out.println("これは " + callable.call() + " です"); }catch(Exception e){ e.printStackTrace(); } } } |
1 |
これは ラムダ式が返した文字列 です |