It's harder than you think to teach Java to anyone who is new to programming and has only experience coding in C. (Assuming an information university. It seems that you often do C in the first year and Java in the second year.) </ Sub> </ sub> When asked "What is a class?", I explain that it is "object-oriented." What if you were asked, "What is object-oriented?"
"There are various monogoto in the world, and monogoto has a state and behavior. Therefore, for various monogoto, we collect states and behaviors and code them in the class. This is object-oriented. " The other party may be convinced. However, the other party may be confused by the explanation of "encapsulation" and "inheritance" that follows. If you suddenly learn that you "collect states and behaviors to express monogoto", then you learn "to confine the state to behavior" and "concrete monogoto is an abstract monogoto". There are many. It would be "pain" for those who are "practice rather than theory". In this way, it seems that there is no end to the number of people who are not good at object-oriented coding and move away from the programming area.
The true nature of "pain" is that "what is object-oriented, that is, what" is not explained in one word.
It is speculation, but people think of new concepts by comparing them to existing ones, and learn the error part by practice. In Laputa, the castle in the sky, the main characters Pazu and Theta follow the government aircraft Goliath to discover the legendary island Laputa floating in the sky, and it seems that Goliath will be lost once. At that time, collaborator Dora attempts to continue the chase using a kite called a "watch stand". At this time, Pazu is instructed as follows.
You can learn how to fly (a kite) with your body!
Although it is a scene where the courage of Pazu who manages his life-threatening unreasonableness without difficulty stands out, Dora did not instruct Pazu without thinking. Before Goliath's tail, Dora set out a small boat to Theta's whereabouts with Pazu in order to rescue Theta, who was half-threatening and taken to the government. At this time, Dora fainted due to the impact of the accident, and the small ship was about to crash, but Pazu suddenly took over the maneuver, and he died in nine deaths. The instructions for kite maneuvering (new concept) may have been in anticipation of Pazu's track record of properly maneuvering small boats (existing concept) in extremely tense situations.
Isn't object-oriented the same thing? You can compare object-orientation with something that everyone knows, and the difference can be learned by each person based on practice. At least it's better than not being able to even move your hands without getting an image. In this article, I will explain Java programming and object-oriented programming with this approach.
Finally, if you go back to the definition of "what is a program" and "what is a computer (or a computer)", ** It is logically provable that the class is a "program that can configure a computer" and that the instance is a computer **.
** To explain a class in one word, it is a "program that can configure a computer". ** ** This can be proved logically.
Similarly, it can be proved that * an instance is a "virtual computer" *.
First, the three elements of the program in Böhm Jakopni's theorem [1] You can do "sequential", "iteration", and "branch" all with the methods in the class. Of course, ** methods can also be called programs **, Unfortunately the method has no "storage". (Jabashi: A "local variable" that is valid only within a method cannot be said to be "remembered" because it expires at the end of the method) </ sub> Therefore, it is not possible to create a "header" in the Turing machine [2]. ** Methods can be programs, but not computers **. In that respect, a class has a "field" (called a "property" in a language other than java), and this can be used as a storage area. ** Classes can build computers **. (End of proof) In other words, a class is a blueprint for a computer.
The class itself is a type for creating an "entity" called an instance. Because the instance is created based on the blueprint called class An instance is "something created based on a blueprint called a class." Applying what we proved in Section 1-1 here, An instance is "Something made based on a blueprint called'computer blueprint'" Is. It's a computer (if made according to the blueprint). (End of proof)
From Chapter 1, a class is a "program that can configure a computer". It turns out that an instance is a "computer".
The "program that can configure a computer" is exactly the OS.
It may be strange, but every time you program in java, the programmer creates an OS, and every time you new
, you create a virtual computer.
For example, consider the following code.
"Windows" OS and "computer" computer
public class Mindows
{
private byte[] ram;
public byte getRam(int address){return this.ram[address];}
public void setRam(int address, byte value){this.ram[address]=value;}
public Mindows(int ram_size){this.ram = new byte[ram_size];}
}
class Main{ public static void main(String...args)
{
Mindows computer = new Mindows(64);//Creating a computer with 64 bytes of RAM
int i=0;
for(char each_char : "hello world".toCharArray())
computer.setRam(i++, (byte)each_char);
//0~Write "hello world" at address 10
for(int j=0; j<11; j++)
System.out.print((char)computer.getRam(j));
//0~Address 10(As char)display
//→ "hello world" is displayed on the console.
}}
This code defines the Mindows OS, creates a virtual computer computer
containing this OS, and
You are manipulating RAM. It is the OS (class) that defines what can be done using computer
(instance), but the OS does not do anything directly, and the actual operating entity is computer
(instance). is there.
Besides this, Make a ROM with the final constant field, Create a BIOS with the final method It is conceivable to use the file input / output function to have storage.
A ʻArrayList type instance is a virtual computer that can store
T` type objects in order in the storage area.
The List <T>
interface is just a "declaration" of a program that stores T
type objects in storage. ** It can be said that it "roughly" determines how to use the storage area **.
ʻArrayList and
LinkedList "implement" this interface. This fact means that ʻArrayList <T>
and LinkedList <T>
declare that "the storage area will be used to store T
type objects ".
The word ... </ sub> </ sub> class that implements the interface must overload the empty methods defined in the interface and define them specifically. ... execute </ sub> </ sub>
The declaration by the interface is easy to understand if you imagine home appliances. Rice cookers and washing machines (and vacuum cleaners in our day) can all be called computers. The rice cooker calculates the hardness and remaining time of cooked rice from the weight of rice and water. There are also scenes where the washing machine makes an automatic decision. You can see the vacuum cleaner by looking at Roomba. It is important to clearly state whether the class (program) you are about to make is a rice cooker, a washing machine, or a vacuum cleaner. In some cases, it is possible to say, "Only the class that implements a specific interface is allowed to use it specially." (For example, an exception class.)
"I don't want to show the circuit inside the computer to the customer" -that's encapsulation.
Let's take a look at the code shown in Chapter 2 again.
When writing to the RAM of computer
, I purposely call the methodsetRam (address, value to be written)
.
Even if I don't do that, is ram [address] = value to be written
useless?
Of course, that's fine in principle, but it's actually harder to do.
The developers of "Mindows" have created setRam (address, value to write)
as a function to write to memory, but they have not exposed the memory itself. The actual computer puts the access modifier private
in the java code so that the memory is locked inside the iron.
By doing so, ** the storage area is not tampered with by the user **, and the possibility of malfunction is reduced.
Unfortunately, you can't always think of an OS as a class. This is because there are (or most) OSs that have concepts that classes do not have. (Example: multitasking OS, virtual memory, etc. Also, if you have to think about clocks and CPUs, it will be difficult to express this in classes (practically)) Therefore, let's call the OS that can represent a class "class OS" and the computer that can represent an instance "instance computer" or "computer of class OS".
On the contrary, there is no OS that does not have any of the concepts of the class (only fields and methods). (Clear from the proof that the class can be considered an OS)
** In an environment where you can freely design an OS and create and use a virtual computer that contains that OS, object-oriented development is possible at least partially **. (Let's call it the "semi-object-oriented theorem") This is not surprising as every OS has all the concepts of classes. When an error occurs, it is easy to identify the error because it is sufficient to confirm which computer (OS) has the problem, and it is possible to flexibly respond to specification changes. However, encapsulation cannot be used without an access restriction mechanism, and inheritance is impossible without a copy mechanism.
<!-Implementation of private fields using md5->
[1]http://s150001.skr.u-ryukyu.ac.jp/lectures/index.php?ProgrammingAbility [2]http://e-words.jp/w/%E3%83%81%E3%83%A5%E3%83%BC%E3%83%AA%E3%83%B3%E3%82%B0%E3%83%9E%E3%82%B7%E3%83%B3.html
Recommended Posts