Postscript: 2020/3/13 Since the comment pointed out that the content of this time was "data hiding" rather than "encapsulation", it was corrected. The title has also changed from encapsulation to data hiding.
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.
Language: Java11, JDK13.0.2 Operating environment: Windows 10
I confirmed that the instance created by new
has its own members (fields and methods) inherited from the class. But in the last Cat
class
Cat myCat = new Cat;
mycat.weight = -20;
And, the value that was not expected when the Cat
class was created can be stored arbitrarily from within the main
method.
Allowing access to the ** field (this time double weight
) ** set in the Cat
class directly from within the main
method can cause problems.
private
Therefore, when "I want to have a member (field method) that cannot be handled from outside the class processing", the qualifier attached to the member (field method) is ** private
**.
This time I would like to handle the Cat class by changing from the String type color to the double type height.
classCat re-challenge
class Cat
{
private double weight;
private double height;
//class
public void setWeightHeight(double w,double h)
{
if(w > 0 && w <= 50){
weight = w;
System.out.println("The weight of this cat" + weight + "I made it kg.");
} else{
System.out.println("I don't think I have this weight ...");
}
if(h > 0 && h <= 200){
height = h;
System.out.println("The height of this cat" + height + "I made it cm.");
} else{
System.out.println("I've never heard of a cat of that height ...");
}
}
double getWeight()
{
System.out.println("Update the weight of the cat.");
return weight;
}
double getHeight()
{
System.out.println("Update the height of the cat.");
return height;
}
void introduce()
{
System.out.println("The weight of this cat" + weight + "It is kg.");
System.out.println("The height of this cat" + height + "cm.");
}
}
Since the weight height
of the Cat class is now private
, there is no need to worry about the value being tampered with.
public
But if you really can't change the value, you can't change the value of the new myCat
instance you want to new
.
But in the Cat
class
public void setWeightHeight(double w,double h)
I want you to pay attention to. This is a method defined in the Cat class, but its qualifier is set to ** public
**. This is a modifier that "allows access from outside the class" and is part of the process
weight = w;
height = h;
And ** accessing the field set to ** private
**.
Of the methods, using the fact that private
can be accessed from methods that are in the same class, ** Storage to the value set in private
via in-method processing is possible. Allowed **.
public void setWeightHeight(double w,double h)
Has an if statement inside it, which can prevent the assignment of values that do not meet the conditions. Now you don't have to worry about storing ridiculous values.
About the process I saw this time, "Use a method to store a value in the private
field. " Quoting from the reference book Easy Java 7th Edition,
A function that puts data (fields) and functions (methods) together in a class and attaches private to the members you want to protect so that they cannot be accessed without permission.
Is. This functional design ** ~~ encapsulation ~~ data hiding ** I call it. This is ** one of the basic principles of object-oriented design **, and classes are created for this purpose. I will deal with other principles again, but they are all important principles, so I want to grasp them firmly.
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