I always read it when I explain it, so I summarize it
** Aggregate variables and methods in a class, hide them, and instantiate them for mass production and use **
――A mechanism to put together, hide, and make a lot ――What is a class? --Instance is an "entity" --What is defined in the class can be used by instantiating --So, it's better to think of classes and instances as a set. --Mechanism for making highly independent parts --One of the three major object-oriented elements --Types and entities correspond to sets and elements in set theory
** Can be classified **
--For example, "American Shorthair", "Japanese Cat", "Pug", and "Shiba Inu" ――The substance of "American Shorthair" and "Japanese cat" of the type "cat" ――The substance of "pug" and "shiba inu" of the type "dog"
** You can pass the necessary information to the entity when instantiating. The instantiated target continues to manage its state. Information to be published is restricted for instantiated targets **
--When instantiated, the constructor is executed --Fields are set when the constructor is executed --Constructor is "what to build" --A field is "attribute information" --For example, a class called "Cat" and an instance called "Cat" --The information required for the "Cat" class to instantiate is the "name" (only this time) --When "Cat" is instantiated, "Cat", which is the information required for the instance, is set in the constructor. --Birth of "Cat" ――Because you continue to be a "cat", you can play "cat" as many times as you like.
Cat.java
public class Cat {
String name;
Cat(String name) {
this.name = name;
}
public void cry() {
System.out.println("Nya ~");
}
}
Main.java
public class Main {
public static void main(String[] args) {
Cat nekosan = new Cat("Cat");
nekosan.cry();
}
}
--Unify the roles --Classes can be treated as types ――The terms used in the explanation differ depending on the viewpoint, context, and person. --Approximately the same --Instantiation --Instantiation --Generate --new
-> new part corresponds to the code
--Approximately the same
-> This time (Java), the "Dog {" part. For PHP, "__constract". "Initialize" for Ruby. For Python, "\ _ \ _ init \ _ \ _". "Init" for Swift
--Approximately the same --Field --Member variables --Instance variables
Summary of why we make it object-oriented
Akira Hirasawa (Author) Why make it object-oriented, 2nd edition https://amzn.to/2VSrzwe
Encapsulation has multiple elements and perspectives when trying to explain programming technology There are many words to explain, and even if the subject of explanation is the same, the nuances may differ depending on the viewpoint, so it is the most important and difficult feeling.
Recommended Posts