Getting Started with Java 1 Putting together similar things

As a premise, it is assumed that the environment can run java, eclipse (or).

System.out.println This time I thought about writing something for getting started with java for acquaintances, but I thought it would be fun to just copy the grammar, so I thought I would make programming that can fight RPG in several times. I will. This time, I will continue to explain the grammar, but I would be grateful if you could take a look. To get started, try the following:

Battle.java


public class Battle {
	public static void main(String[] args) {
		System.out.println("GameStart!");
	}
}

You should see the words Game Start! On the console. You don't have to worry about other grammars, but now you don't have to worry about it yet, but the Battle after the class and the file name Battle.java must match these two Battles.

variable

Next, let's increase it a little.

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		String name="taro";
		System.out.println(name);
		int hp =10;
		System.out.println(hp);
		System.out.println(name+"HP is"+hp+"is");
	}
}

There are some new ones. I think that the number of String name =" taro " and ʻint hp = 10has increased. Here, String represents a character string and int represents an integer. In other words, put "taro" in the box named name of the string, and put 10 in the box called hp of the integer. These can also be output to the console by putting them inSystem.out.println. Variables and strings are mixed in the last line, but you can also output to the console by setting variable +" string "`. Let's do a little more work

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		String playerName="taro";
		System.out.println(playerName);
		int playerHp =10;
		System.out.println(playerHp);
		System.out.println(playerName+"HP is"+playerHp+"is");
		String monsterName = "slime";
		System.out.println(monsterName);
		int monsterHp = 3;
		System.out.println(monsterHp);
		System.out.println(monsterName+"HP is"+monsterHp+"is");
	}
}

There are various changes, but first I changed name and hp to playerName and playerHp. In addition, monsterName and monsterHp have been added. It's a moment if you copy it, but don't you think it's troublesome if you write it by yourself? So the old smart people thought. Let's put together similar things together. Can you find something similar here from the code above? In fact, there are some similar ones.

First of all, probably the easiest to understand System.out.println (HP of playerName +" is "+ playerHp +" ") When There are two types of System.out.println (HP of monsterName +" is "+ monsterHp +") . Besides, it is similar that only the name and HP are displayed. These can be summarized as follows.

Method

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		String playerName="taro";
		int playerHp =10;
		display(playerName,playerHp);
		
		String monsterName = "slime";
		int monsterHp = 3;
		display(monsterName,monsterHp);
	}
	
	public static void display(String name,int hp) {
		System.out.println(name);
		System.out.println(hp);
		System.out.println(name+"HP is"+hp+"is");
	}
}

What's written on Battle.java is pretty neat, but there's a new display. This is called a method and is for summarizing the actions. This time, I expect that a box called name of String type and a box called hp of int type will be included, and if name and hp are entered, they will be displayed. This display does that. Let's dig a little deeper into the method. Try the following:

Clac.java


public class Calc {

	public static void main(String[] args) {
		int number1 = 3;
		int number2 = 6;
		int output=plus(number1,number2);
		System.out.println(output);
	}
	
	public static int plus(int plus1,int plus2) {
		int result = plus1+plus2;
		return result;
	}
}

There is also a plus method here. This is a method that adds number1 and number2 and returns the result. In order to return the result, you must first specify what the method returns. That is the int in front of plus. This specifies the type of result to be returned, that is, the type. And the part that returns the result is the part of return result. Although static was attached earlier, this is attached because static is attached to main. It's not very important, so leave it in the corner of your head for now. Let's get back to the main subject. Battle.java I mentioned earlier, but there are still more similar ones.

class

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		Player player=new Player();
		player.name="taro";
		player.hp=10;
		display(player.name,player.hp);
		
		String monsterName = "slime";
		int monsterHp = 3;
		display(monsterName,monsterHp);
	}
	
	public static void display(String name,int hp) {
		System.out.println(name);
		System.out.println(hp);
		System.out.println(name+"HP is"+hp+"is");
	}
	
	
}

class Player{
	String name;
	int hp;
}

I think that a new class called Player has been added above. class is a function to create a new type like String, int by yourself, Player player declares a new variable called player, andnew Player ()creates a new player type entity. If it is the official name, it is called an object. The number of lines of code itself hasn't decreased, but it's easier to see. Earlier, it was necessary to change the name of the variable in order to put the name and hp of the monster, but if you put it together in this way, you do not have to change the name of the variable. By the way, let's make a monster as well.

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		Player player=new Player();
		player.name="taro";
		player.hp=10;
		display(player.name,player.hp);
		
		Monster monster = new Monster();
		monster.name = "slime";
		monster.hp = 3;
		display(monster.name,monster.hp);
	}
	
	public static void display(String name,int hp) {
		System.out.println(name);
		System.out.println(hp);
		System.out.println(name+"HP is"+hp+"is");
	}
	
	
}

class Player{
	String name;
	int hp;
}

class Monster{
	String name;
	int hp;
}

Oita was refreshing. Actually, there is one problem here. People who work with Java get angry when they see the code player.name. As for the tatemae, if you make it possible to do it directly with player.name, you may mistakenly rewrite the value. Well, I don't want to get angry, so let's get the value and assign the value with the method.

Encapsulation

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		Player player=new Player();
		player.setName("taro");
		player.setHp(10);
		display(player.getName(),player.getHp());
		
		Monster monster = new Monster();
		monster.setName("slime");
		monster.setHp(3);
		display(monster.getName(),monster.getHp());
	}
	
	public static void display(String name,int hp) {
		System.out.println(name);
		System.out.println(hp);
		System.out.println(name+"HP is"+hp+"is");
	}
	
	
}

class Player{
	private String name;
	private int hp;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
}

class Monster{
	private String name;
	private int hp;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
}

Private is for restricting access and can only be accessed from the same class. Also, the keyword this used in setName, setHp, etc. means that of this class. This makes it easier to understand because the value received by the method can be the same name. There is something more similar here. That's Player and Monster. Let's put these together.

Inheritance

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		Player player=new Player();
		player.setName("taro");
		player.setHp(10);
		display(player.getName(),player.getHp());
		
		Monster monster = new Monster();
		monster.setName("slime");
		monster.setHp(3);
		display(monster.getName(),monster.getHp());
	}
	
	public static void display(String name,int hp) {
		System.out.println(name);
		System.out.println(hp);
		System.out.println(name+"HP is"+hp+"is");
	}
	
	
}

class Animal{
	private String name;
	private int hp;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
}

class Player extends Animal{
	
}

class Monster extends Animal{
	
}

I think there is a new Animal class, and Player and Monster have ʻextends Animal`. This is inheritance, and the values and methods are inherited as they are. Therefore, even if the value or method disappears from Player or Monster, no error will occur at the top. You can also do this by inheriting.

Polymorphism

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		Player player=new Player();
		player.setName("taro");
		player.setHp(10);
		display(player);
		
		Monster monster = new Monster();
		monster.setName("slime");
		monster.setHp(3);
		display(monster);
	}
	
	public static void display(Animal animal) {
		System.out.println(animal.getName());
		System.out.println(animal.getHp());
		System.out.println(animal.getName()+"HP is"+animal.getHp()+"is");
	}
	
	
}

class Animal{
	private String name;
	private int hp;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
}

class Player extends Animal{
	
}

class Monster extends Animal{
	
}

I rewrote the display method to make it display (Animal animal). I put it directly there like display (player) or display (monster). It seems that an error occurs because the type is different, but there is no problem because Player and Monster inherit the type called Animal by inheritance. You can make it even easier by using the following functions.

constructor

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		Player player=new Player("taro",10);
		display(player);
		
		Monster monster = new Monster("slime",3);
		display(monster);
	}
	
	public static void display(Animal animal) {
		System.out.println(animal.getName());
		System.out.println(animal.getHp());
		System.out.println(animal.getName()+"HP is"+animal.getHp()+"is");
	}
	
	
}

class Animal{
	private String name;
	private int hp;
	
	public Animal(String name,int hp){
		this.name=name;
		this.hp=hp;
	}
	
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
}

class Player extends Animal{
	public Player(String name, int hp) {
		super(name, hp);
	}
}

class Monster extends Animal{
	public Monster(String name, int hp) {
		super(name, hp);
	}
	
}

A new animal with the word Animal has been added. This is a constructor, not a method, and you can set the value when you create a new object with new. Since it is not a method, there is no type after public. Also, since the constructor cannot be inherited, the constructor is also created for Player and Monster. I can't set the value with this, so I use something called super () to put the value. This is the simplest thing to do, but inheritance itself is actually awkward and not recommended to use too much. The reason is that it is vulnerable to change. Java inheritance itself can only inherit one class. If you want to inherit more than one, it means B that inherits A, C that inherits B, and C that inherits C, but other classes that inherit B and B that inherit A and D and D that inherit B. If there is, then if A and B are matched to the implementation of one class, an error may occur in the other class. Even if you can fix the error, it's likely that your code will get dirty. So I don't recommend using inheritance. On the other hand, there is a desire to put Monster type or Player type in display (Animal animal) (so-called polymorphism), so let's use an interface instead.

Interface

Battle.java


public class Battle {
	public static void main(String[] args){
		System.out.println("GameStart!");
		Player player=new Player("taro",10);
		display(player);
		
		Monster monster = new Monster("slime",3);
		display(monster);
	}
	
	public static void display(Animal animal) {
		System.out.println(animal.getName());
		System.out.println(animal.getHp());
		System.out.println(animal.getName()+"HP is"+animal.getHp()+"is");
	}
	
	
}

interface Animal{
	String getName();
	int getHp();
}

class Player implements Animal{
	private String name;
	private int hp;
	
	public Player(String name,int hp) {
		this.name=name;
		this.hp=hp;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
}

class Monster implements Animal{
	private String name;
	private int hp;
	
	public Monster(String name,int hp) {
		this.name=name;
		this.hp=hp;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
}

You have deleted the inheritance relationship once, and have a new interface and implements for Player and Monster. However, the code in main remains the same. This is the same as extends and also has a class called Animal, so the above code is fine. There are String getName () and ʻint getHp () `in the interface, but thanks to doing this, you can also use getName (), getHp (), etc. in the main display. Also, if you implement as an interface feature, you must write what is written in the interface. This time, getName () and getHp () are written, so if you do not write them, a syntax error will occur. However, by causing a syntax error on the contrary, it guarantees that the implemented ones will always work getName () and getHp (). The difference between inheritance and interface is ambiguous for me. It's ambiguous, but to explain it, I think that inheritance is very tied, and interfaces are loosely tied.

This is the end of this time. I've explained this for a long time, but how was it? I hope you find it helpful.

Recommended Posts

Getting Started with Java 1 Putting together similar things
Getting Started with Java Basics
Getting started with Java lambda expressions
Getting Started with Ruby for Java Engineers
Getting Started with Java Starting from 0 Part 1
Links & memos for getting started with Java (for myself)
Getting Started with DBUnit
Getting started with Kotlin to send to Java developers
Getting Started with Swift
Getting Started with Docker
Getting Started with Doma-Transactions
Getting started with Java programs using Visual Studio Code
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Getting Started with Doma-Annotation Processing
Getting Started with JSP & Servlet
Getting Started with Spring Boot
Getting Started with Ruby Modules
Getting started with Java and creating an AsciiDoc editor with JavaFX
Getting Started with Java_Chapter 5_Practice Exercises 5_4
[Google Cloud] Getting Started with Docker
Getting Started with Docker with VS Code
Returning to the beginning, getting started with Java ② Control statements, loop statements
Getting Started with Doma-Criteria API Cheat Sheet
Getting Started with Docker for Mac (Installation)
Getting Started with Parameterization Testing in JUnit
Getting Started with Ratpack (4)-Routing & Static Content
Getting started with the JVM's GC mechanism
Getting Started with Language Server Protocol with LSP4J
Getting Started with Machine Learning with Spark "Price Estimate" # 1 Loading Datasets with Apache Spark (Java)
Getting Started with Creating Resource Bundles with ListResoueceBundle
Getting Started with Java_Chapter 8_About Instances and Classes
Getting Started with Doma-Using Subqueries with the Criteria API
Getting Started with Doma-Using Joins with the Criteira API
Getting Started with Doma-Introduction to the Criteria API
I tried Getting Started with Gradle on Heroku
Going back to the beginning and getting started with Java ① Data types and access modifiers
Experienced Java users get started with Android application development
Get started with serverless Java with the lightweight framework Micronaut!