Today, I visited the company as usual. Sit down at your workstation and turn on the computer, "It's another day of moving bricks". After thinking about it, I opened Idea skillfully, looked at today's requirements, and started typing the code. Hey, who wrote these codes, how come they appear in my code, and they are still waiting to be submitted. I remember I have never written them, so I looked at them with interest:
Isn’t this polymorphic? Whoever wrote the test on my computer couldn’t help but wonder. "Look at the output of this?"
A sound came from behind. Because I was thinking about the output, I didn't care about the source of the sound, so I continued. After looking at the code, I came to the conclusion:
polygon() before cal()
square.cal(), border = 2
polygon() after cal()
square.square(), border = 4复制代码
Copy after login
I thought: That’s it? At least you are a Java development engineer, okay? Although you usually do a lot of work, you still have some basic skills. I couldn't help but feel a little proud~
"Is this your answer? It seems that you are not doing well either"
The voice suddenly sounded again, this time I was not calm anymore , Nima! I also thought about this answer in my mind, okay? Who can see it? Moreover, it makes people want to perform a set of Awei's Eighteen Styles.
"Who are you?"
turned his head with a hint of doubt and anger. Why is there no one? I couldn't tolerate any doubts, "Xiaocai, wake up, why did you fall asleep during working hours?"Did you fall asleep during working hours? I opened my eyes and looked at my surroundings. It turned out to be a dream, and I breathed a sigh of relief. I looked around and saw the department manager standing in front of me. He was sleeping during working hours. Are you feeling unwell or something? I wrote a bunch of bugs yesterday but didn't correct them, and I submitted some messy stuff today. I think your performance this month is not what I want, and based on your performance, I have to start thinking about it for the department.
"I'm not, I didn't, I don't know why I fell asleep, please listen to my explanation!"
Before I could say this sentence, felt in my heart I want to take you home with the flower. I don’t care if it’s real or fake in the bar late at night. Please swing it to your heart’s content and forget about him. You are the most charming. Do you know? The alarm bell rang. I I stood up, my back was slightly wet, my forehead was slightly sweaty, I checked my phone, it was Saturday, 8:30, it turned out to be a dream! Strange, how could I have such a strange dream? It’s so scary. Then I thought of the part of the code in the dream. Are my results wrong? Based on memory, I typed it out again on the computer, and the results are as follows:
/*
polygon() before cal()
square.cal(), border = 0
polygon() after cal()
square.square(), border = 4
*/复制代码
Copy after login
Copy after login
Copy after login
square.cal(), the result of border
is actually 0, not 2. Am I not even good at polymorphism now? In front of your computer and mobile phone, you don’t know if you have come up with the correct answer! Regardless of whether you have it or not, let’s review polymorphism with Xiao Cai!
Some friends may be confused about not only the result of square.cal(), border
is 0, but also why it is not
square.square(), border = 4 Output the doubts first. Then let’s get started with doubts! Polymorphism
In object-oriented programming languages, polymorphism is the third basic feature after data abstraction and inheritance.
Polymorphism can not only improve the organization and readability of code, but also create scalable programs. The function of polymorphism is to eliminate the coupling relationship
between types.
1. Upward transformation
According to the
Richter substitution principle
: Wherever a base class can appear, a subclass can definitely appear.
The object can be used either as its own type or as its base type. This method of treating a reference to an object as a reference to its base type is called - Upcast
. Because the parent class is above the child class, the child class must reference the parent class, so it is called
Upward transformation.
public class Animal { void eat() {
System.out.println("Animal eat()");
}
}class Monkey extends Animal { void eat() {
System.out.println(" Monkey eat()");
}
}class test { public static void start(Animal animal) {
animal.eat();
} public static void main(String[] args) {
Monkey monkey = new Monkey();
start(monkey);
}
}/* OUTPUT:
Monkey eat()
*/复制代码
Copy after login
The start() method in the above
test class receives a reference to Animal, and naturally can also receive from Animal's exported class. When calling the eat() method, the eat() method defined in Monkey is naturally used without any type conversion. Because upgrading from Monkey to Animal can only reduce the number of interfaces, but not fewer interfaces than Animal. A not particularly appropriate analogy: Your father’s property will be inherited to you, and your property is still yours. In general, your property will not be less than your father’s.
class Super { public int field = 0; public int getField() { return field;
}
}class Son extends Super { public int field = 1; public int getField() { return field;
} public int getSuperField() { return super.field;
}
}class FieldTest { public static void main(String[] args) {
Super sup = new Son();
System.out.println("sup.field:" + sup.field + " sup.getField():" + sup.getField());
Son son = new Son();
System.out.println("son.field:" + son.field + " son.getField:" + son.getField() + " son.getSupField:" + son.getSuperField());
}
}/* OUTPUT
sup.field:0 sup.getField():1
son.field:1 son.getField:1 son.getSupField:0
*/复制代码
Copy after login
从上面代码中我们看到sup.field输出的值不是 Son 对象中所定义的,而是Super本身定义的。这与我们认识的多态有点冲突。
The above is the detailed content of These days, if you say you know Java, you have to know polymorphism.. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation?
Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems.
Further reading: Java Stream API improvements
Understand Stream forEach
The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods.
Capsule volume formula
The formula for capsule volume is as follows:
Capsule volume = Cylindrical volume Volume Two hemisphere volume
in,
r: The radius of the hemisphere.
h: The height of the cylinder (excluding the hemisphere).
Example 1
enter
Radius = 5 units
Height = 10 units
Output
Volume = 1570.8 cubic units
explain
Calculate volume using formula:
Volume = π × r2 × h (4
Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo