Java 構文3

getter, setter

class User {
  private String name;
  private int score;

  public User(String name, int score){
    this.name = name;
    this.score = score;
  }

  public int getScore(){ //getter
    return this.score;
  }

  public void setScore(int score){
    if (score > 0) {
    this.score = score;
   }
  }

}

public class MyApp{

  public static void main(String[] args){
    User tom = new User("tom", 65); // instance
    tom.setScore(85);
    tom.setScore(-22);
    System.out.println(tom.getScore());
  }
}

static:インスタンス化しなくても、クラスから直接扱えるフィールドやメソッドを定義できます。

class User {
  private String name;
  private static int count = 0;

  public User(String name){
    this.name = name;
    User.count++;
  }

  public static void getInfo(){
    System.out.println("# of instances: " + User.count);
  }

}

public class MyApp{

  public static void main(String[] args){
    User.getInfo();
    User tom = new User("tom");
    User.getInfo();
  }
}

initializer

class User {
  private String name;
  private static int count; //クラス変数

  static {
    User.count = 0;
    System.out.println("static initializer");
  }

  {
    System.out.println("Instance initializer");
  }

  public User(String name){
    this.name = name;
    User.count++;
    System.out.println("constructor initializer");
  }

  public static void getInfo(){
    System.out.println("# of instances: " + User.count);
  }

}

変更できないfinal修飾子

// final 変更できない

final class User {
  protected String name;
  private static final double VERSION = 1.1;

  public User(String name){
    this.name = name;
    User.VERSION = 1.2;
  }

  public final void sayHi(){
    System.out.println("hi! " + this.name);
  }
}

class AdminUser extends User {
  public AdminUser(String name){
    super(name);
  }

  @override
  public void sayHi(){
    System.out.println("[Admin]hi!" + this.name);
  }
}

public class MyApp{

  public static void main(String[] args){
    User tom = new User("tom");
  }
}

抽象クラス

//抽象クラス -> 具象クラス

abstract class User {
  public abstract void sayHi();
}

class JapaneseUser extends User {
  @Override
  public void sayHi(){
    System.out.println("こんにちは!");
  }
}

class AmericanUser extends User {
  @Override
  public void sayHi(){
    System.out.println("hi!");
  }
}

public class MyApp{

  public static void main(String[] args){
    AmericanUser tom = new AmericanUser();
    JapaneseUser aki = new JapaneseUser();
    tom.sayHi();
    aki.sayHi();
  }
}

interface

//interface

interface Printable {
  //定数、
double VERSION = 1.2;
  //抽象
  void print();
 // default method
 public default void getInfo(){
   System.out.println("I/F ver. " + Printable.VERSION);
 }
 // static
}


class User implements Printable {
  @Override
  public void print(){
    System.out.println("Now printing user prifile...");
  }

}

public class MyApp{

  public static void main(String[] args){
    User tom = new User();
    tom.print();
    tom.getInfo();
  }
}

列挙型とordinal()

// 列挙型
enum Result {
  SUCCESS,
  ERROR,
}

public class MyApp{

  public static void main(String[] args){
    Result res;

    res = Result.ERROR;

    switch (res){
      case SUCCESS:
      System.out.println("OK!");
      System.out.println(res.ordinal());
      break;
      case ERROR:
      System.out.println("NG!");
      System.out.println(res.ordinal());
      break;
    }

  }
}