This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ~~ ** The mistaken ** is the center. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. ~~ In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.
This time, in order to understand the basics, I will not describe any mistakes.
Language: Java11, JDK13.0.2 Operating environment: Windows 10
Let's start by declaring the class. This is a quote from Easy Java 7th Edition.
declaration.txt
class class name
{
Model name Field name;
...
Return type method name(Argument list)
{
Sentence;
...
return expression;
}
...
}
Let's compare it with the actual class based on this declaration.
Cat.java
class Cat
{
double weight;
String color;
//This is the type name and field declaration.
//You now have weight and color fields.
void setWeightColor(double w,String c)
{
weight = w;
color = c;
System.out.println("The weight of this cat" + weight + "I made it.");
System.out.println("The color of this cat" + color + "I made it.");
}
double getWeight()
{
System.out.println("Update the weight of the cat.");
return weight;
}
void introduce()
{
System.out.println("The weight of this cat" + weight + "It is kg.");
System.out.println("The color of this cat" + color + "The color.");
}
//This is the description of the method. Pay attention to the return type.
//Cat class is setWeightColor(double w,String c),getWeight()
//And introduce()Has a method of.
}
You can see which fields and methods are. These fields and methods are called ** members ** of the class.
As an aside, class names, field names, and method names are all ** identifiers **. Therefore, if you follow the rules of identifiers (think of them as rules of variables) that you have dealt with before, you can ** any name you like **. There is no problem in the program even if the identifier does not make sense, but if you write it in consideration of readability, it will be read back later. ・ It may be easy to understand when someone reads it.
The following is an example that is (for the time being) OK. No readability. Even in such a state, depending on the specifications of the IDE (Integrated Development Environment) and text editor, the source can be written without (relatively) obstacles, so it is really a useful tool of civilization.
longLongSoLongNames.java
class UnitedKingdomOfGreatBritainAndNorthernIreland
{
int pneumonoultramicroscopicsilicovolcanoconiosis;
String Tetaumatawhakatangihangakoauaotamateaurehaeaturipukapihimaungahoronukupokaiwhenuaakitanarahu;
void Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch()
{
System.out.println("The longest English word in the world is
In the name of the disease" + pneumonoultramicroscopicsilicovolcanoconiosis
+ "The longest place name in the world is"
+ Tetaumatawhakatangihangakoauaotamateaurehaeaturipukapihimaungahoronukupokaiwhenuaakitanarahu
+ "is.")
}
}
For weight
and color
declared in the Cat
class, you can use Cat
to create a new object or instance and then store the value in that object.
Example:
mainCatCreate.java
Cat firstCat = new Cat();
firstCat.weight = 2.3;
firstCat.color = "Tea tiger";
You can prepare as many objects as you like, as long as you create an object properly with new
.
Following the above example, it would take too much time to prepare values for all objects one by one, so use the method written in the Cat
class.
methodTry.java
Cat firstcat =new Cat();
firstCat.setWeightColor(2.3,"Tea tiger")
double myCatWeight = firstCat.getWeight();
System.out.println("The weight of the cat for the first time in my home" + myCatWeight + "was.");
It was a simple class, but it took about 20 minutes to write it, including checking it. If you don't write a lot, you won't be able to learn it, and I feel that mass-producing these classes is a real pleasure, so it may be a good idea to set up a number of times to create a class.
I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.
Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)
Recommended Posts