[Java] Object-oriented

Reference "Introduction to Java that can be clearly understood p263-p366"

Chapter 7 Summary

First, as a reason to learn object orientation This is to prevent the source code from becoming too long and complicated for the developers themselves to understand.

In the first place, object-oriented programming and basic grammar are completely different in what they learn and how they learn. Therefore, many people use it without understanding the essence.

・ Object-oriented definition The concept of componentization used when developing software

・ The essence of object orientation Reproduce characters in the real world and their behavior in the virtual world inside your computer

In object-oriented programming, the programmer puts each part into Write "responsibility (role)" as a program. This responsibility is "information retention" and "operation" To fulfill these, objects have "attributes" and "operations".

"Attribute" A box to remember information about the character "Operation" The procedure of actions and actions performed by the character

Objects work together by reading and writing attributes to each other and calling operations. It works as one program as a whole.

・ Three major object-oriented functions Encapsulation Inheritance Polymorphism

Chapter 8 Summary

The role of programmers in object orientation (1) Consider the responsibilities that each object should bear, and define the types and contents of "attributes" and "operations". ② Create and move each object in the virtual world.

If you want to indicate an entity that is active in a virtual world, use the word instance. Also, the act of creating an instance from a class expresses instantiation.

If you create a class of characters, you need a class that tells you how it works. That is the main class.

public class main {
	public static void main(String[] args) {
	}
}

Below, in this book, the code is written using "Battle between Hero and Haunted Mushroom" as an example, so I will refer to it. (Use Hero class, Matango class, main class)

Hero class ・ How to declare a class

public class Hero {
}

・ How to declare attributes

[name] name(String type)
[HP]   hp(int type)

・ How to declare an operation Object-oriented uses methods to define operations

	void sleep() {
		this.hp = 100;
		System.out.println(this.name + "Sleeped and recovered!");
	}

In summary, it looks like this

public class Hero {
	String name;
	int hp;

	void sleep() {
		this.hp = 100;
		System.out.println(this.name + "Sleeped and recovered!");
	}

}

If you define a class, you can use it as a type. The reason we need a class variable is to identify a particular one of the instances with the same name.

main class -How to create an instance

Class name variable name=new class name();

-Assigning values to fields

Variable name.Field name=value;

-Finally, call the method and give "instruction".

I actually moved it

Hero class

Hero.png

main class

main.png

It is displayed properly in the execution result.

In summary, classes and methods are created based on the clear idea of object orientation. Think of it as replacing people, things, and events in the real world with classes.

Chapter 9 Summary

Here we will mainly learn about constructors.


public class Hero {
	String name;
	int hp;

	void sleep() {
		System.out.println(this.name + "Sleeped and recovered!");
	}
	Hero(){
		this.hp = 100;
		
	}
	
}

Add the Hero () method as above. This Hero () method "when new is done, the process defined inside the method is automatically performed at the same time as new" It has the property of. Such a method is called a constructor.

Therefore, it is not necessary to assign the initial value to HP on the main method side.

When you actually move it, you can see that the initial HP contains 100.

main1.png

-Conditions considered as a constructor ① The method name is exactly the same as the class name (2) The return value is not described in the method declaration

It is also possible to declare that the constructor can be received as an argument.

	Hero(String name){
		this.hp = 100;
		this.name = name;
	}

Specify in the main method when newing the argument that you want to pass when the constructor is executed.

main2.png

You can see that Minato is included in the name.

Impressions

I'm new to Java, so I've been doing object-orientation somehow. Is it really correct even if I actually write this article? There were some questions about what it meant. In particular, I felt that there were many parts where the constructor part was ambiguous, so It seems that there were few parts that could be put together. However, rather than understanding such an essential meaning, reading, writing, and executing a book I thought it would lead to understanding. By taking a closer look at this book, I think I could understand a little about what I had been doing. I thought that I could master it by learning more about the chapters that follow.

Recommended Posts

[Java] Object-oriented
Object-oriented FizzBuzz (Java)
[Java] Object-oriented summary_Part 1
[Java] Object-oriented syntax-Constructor
Object-oriented (Java) basics
[Java] Object-oriented summary_Part 2
[Java] Object-oriented syntax-Package
Java
Java
Object-oriented (java) with Strike Gundam
Object-oriented summary by beginners (Java)
Java 3 major elements (object-oriented) memorandum
Java learning (0)
Studying Java ―― 3
[Java] array
[Java] Annotation
[Java] Module
Java array
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Object-oriented summary
[Java] Object-oriented syntax --class method / argument
Java Day 2018
java (array)
Java static
Java serialization
JAVA paid
Java (set)
java shellsort
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)
[java] interface