[jdk14.0.2] 基礎2

package hpscript.myapp;
import hpscript.myapp.model.User;
import hpscript.myapp.model.AdminUser;

public class MyApp {

	public static void main(String[] args){

		User tom;
		tom = new User("john");

		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHello();
	}
}

$ javac hpscript/myapp/MyApp.java
$ java hpscript.myapp.MyApp

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(){
		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);
		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();
		User bob = new User("bob");
		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");
	}

	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();
		User bob = new User("bob");
		User.getInfo();
	}
}

final

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

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

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

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

	void sayHi(){
		System.out.println("[admin] Hi!" + this.name);
	}

}

public class MyApp {
	public static void main(String[] args){
		User tom = new User("tom");
		User.sayHi();
		AdminUser bob = new AdminUser("bob");
		AdminUser.sayHi();
	}
}

abstract

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 Printable {
	// 定数
	// 抽象メソッド default, static
	double VERSION = 1.2;
	void print();
	public default void getInfo(){
		System.out.println("I/F ver. " + Printable.VERSION);
	}
} 

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


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

列挙型

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;
		}
	}
}