Java learning process - Promotion

When I forget you, I forget myself—— Wuthering Heights

Normal distribution: also known as Gaussian distribution, it is a very important probability distribution in mathematics, physics, engineering and other fields, and has a significant influence in many aspects of statistics. The normal curve is bell shaped, with low ends, high middle and symmetrical left and right. Because its curve is bell shaped, it is often called bell shaped curve. If the random variable X obeys a mathematical expectation of μ, Variance is σ^ Normal distribution of 2, denoted as n( μ,σ^ 2). The probability density function is the expected value of normal distribution μ Determines its position and its standard deviation σ Determines the magnitude of the distribution. When μ = 0 σ = The normal distribution at 1 is the standard normal distribution. The probability density function and expected variance are shown in the following figure:


This week, we will continue to learn about the polymorphism of the three Java features. So far, we have learned about the three Java features. Later, we will improve our programming ability on the basis of these three features.

1, Polymorphism

Polymorphism means that after the attributes and methods defined in the parent class are inherited by the subclasses, they can have different data types or show different behaviors, which makes the same attribute or method have different meanings in the parent class and its subclasses. Is this concept similar to the method overloading we learned last week~

For object-oriented languages, polymorphism is divided into compile time polymorphism and runtime polymorphism. Compile time polymorphism is static, which mainly refers to method overloading. It distinguishes different methods according to different parameter lists. After compilation, it will become two different methods to achieve polymorphism. While runtime polymorphism is dynamic, it is realized through dynamic binding, which is commonly referred to as polymorphism.

2, Three necessary conditions for implementing polymorphism in Java

There are three necessary conditions for Java to implement polymorphism: inheritance, rewriting and upward transformation. Only when these three conditions are met, developers can use unified logic implementation code to handle different objects in the same inheritance structure, so as to perform different behaviors.

1. inheritance: there must be subclasses and parent classes with inheritance relationships in polymorphism.

2. override: subclasses redefine some methods in the parent class. When these methods are called, the subclass's methods will be called.

3. upward Transformation: in polymorphism, a subclass reference needs to be assigned to the parent object. Only in this way can the reference call both the parent method and the subclass method. Let's take a concrete example to see how to implement Java class polymorphism.

3, Actual combat X

3, Actual combat

1. first, create a Graph class, define parameters and methods, and define the area() method to calculate the area.

package Third.tisheng5;
public class Graph {
    double radius;
    double upper;
    double lower;
    double high;

    Graph(double r) { //Construction method with 1 parameter
        this.radius = r;
    Graph(double up,double low,double hig) { //Construction method with 3 parameters
        this.upper = up;
        this.lower = low;
        this.high = hig;
    double area() { // Used to calculate the area of objects
        System.out.println("The method for calculating the object area in the parent class has no practical significance and needs to be overridden in the child class.");
        return 0;

2. then define the Circle class to inherit the Graph class, which calls the constructor of the parent class and overrides the area() method in the parent class.

package Third.tisheng5;
public class Circle extends Graph {
    Circle(double r) {
    double area() {//Override the area method of the parent class and return the value
        System.out.println("The circle\'s area:");
        return Math.PI * super.radius;

3 create a tradezoid class that inherits the Graph class, which is similar to Circle.

package Third.tisheng5;
public class Trapezoid extends Graph {
    Trapezoid(double up, double low,double hig) {
        super(up, low,hig);
    double area() {//Override the method of the parent class and return the value
        System.out.println("The Trapezoid\'s area: ");
        return (super.upper *+super.lower)*super.high / 2;

4. create a Test class. In the main() method of the class, first declare the graph variable of the graph class, then specify different objects for the graph variable, and call the area() method of these objects. The codes are as follows:

package Third.tisheng5;
public class Test {
    public static void main(String[] args) {
        Graph graph; // Declare variables of the Graph class
        graph = new Circle(9);//Call the Circle method of the Circle class
        graph = new Trapezoid(6, 8,10);//Calling the tradezoid method of the tradezoid class
        graph = new Graph(10, 10,12);

Operation results and screenshots

It can be found from the above code that no matter whether the object of the Graph variable is circle or Trapezoid, they are all subclasses of the Graph class, so they can be transformed upward to this class to achieve polymorphism.

Xiaobian has something to say

This week, let's review the overloading and rewriting of methods. Polymorphism is also implemented to facilitate you to understand the differences between them. The following is the exercise time

  1. Method Overloading: create an Overloading class, define the sum() method and overload the method (that is, change from the number of parameter types).
  2. Method rewrite: create an animation parent class, define the behavior() method, and rewrite the behavior() method in the subclass pig. (that is, the overridden subclass method of the method has the same name as the parent class, and the return type and parameter list).
  3. Realize polymorphism: calculate the perimeter of the graph, define the parent class Diagram class, and use the polymorphism of Java to calculate the perimeter of the graph in the subclass Circle class and Rectangle class.

Editor: Yue yijushi reviewed by: Shiwai Jushi

Posted by pouncer on Thu, 02 Jun 2022 00:25:25 +0530