[Java common sense] 1.0 one of the three features of Java -- class inheritance (constructor)

Posted by mrneilrobinson on Thu, 24 Feb 2022 14:40:07 +0100

1.0 transfer to the next platform. For the things written before, they have recently moved them to the Jane book, and they will write on the Jane book in the future.
2.0 in Java, the format of creating objects is:
Class name object name = new class name ();
For example:

JFrame jf = new JFrame();
When an object is created, it often needs to do something first before the object can be used normally. It can also be said to be preparatory work, such as assigning some initial values to the object or running some other methods first. At this time, a "constructor" is needed to:

    Construction object
    Initialize properties
    In this way, the things to be done during creation are written in the construction method, and each creation will be executed again.

A common Java format:

    public Return type method name (parameter){ }

For example:

public void study(){}
This is called "method" in Java.
The so-called method is the orderly combination of codes used to solve a class of problems. It is a functional module.

3.0 "constructor" in Java is generally called "constructor / constructor" in C language. For novices, Xiaobai wants to learn java promotion, Java architecture, web development, big data, data analysis, artificial intelligence and other technologies more easily. Here we share the system teaching resources and expand my Wei (Tongying): CGMX9880 [tutorial / tools / methods / troubleshooting]
Its definition format:

    public Class name () { }

Example: File Name: teacher java

 1 public class Teacher {
 2     String name;
 3     
 4     //Construction method, which is executed when constructing objects
 5     public Teacher(){
 6         System.out.println("Implemented the construction method of the teacher class");
 7     }
 8     public Teacher(String n){
 9         name = n;
10     }
11     
12     //Method overloading
13     public void teach(int a){
14         System.out.println("Implement teaching method 1");
15     }
16     public void teach(){
17         System.out.println("Implement teaching methods 2");
18     }
19     public void teach(String n){
20         System.out.println("Implement teaching methods 3");
21     }
22     public void teach(String n,int a){
23         System.out.println("Implement teaching methods 4");
24     }
25     public void teach(int a,String n){
26         System.out.println("Implement teaching methods 2");
27     }
28 }

File name: test java

 1 import javax.swing.JFrame;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         //Create an object and call the construction method
 6         Teacher tea = new Teacher("Li Si");
 7         
 8         
 9         Teacher tea1 = new Teacher();
10         tea.name = "Zhang San";
11         tea1.name = "Li Si";
12         tea = tea1;
13         tea1.name = "Wang Wu";
14         
15         System.out.println(tea.name);
16         System.out.println(tea1.name);
17         
18     }
19 }

Although it exists in the constructor in the form of "public class name () {}", unlike the created object, which has a return value type (such as "void"), it will return a memory address.
In memory, the stored data is divided into stack and heap.
Stack storage variables and heap storage objects have corresponding memory addresses. When executing the constructor, initializing the data is equivalent to storing all the data (all) of the object into the memory unit, variables (such as "tea" and "Zhang San") into the stack, and objects (such as "name" and "Teach") into the heap.
The above "Test.java" tea is stored in the stack and the name "Zhang San" is stored in the heap. Each has its own memory address. The operation result is output:
Wang Wu
Wang Wu
When "tea = tea1" is executed, it is equivalent to copying the memory address of tea1 to tea, and all the contents pointed to by tea will become the contents pointed to by tea, so the operation result is two "king five".
In "Test.java", call the construction method "Tecaher" (file: Teacher.java) as the main function, and execute "new Teacher();" once The constructor is called once during the operation. For novices, Xiaobai wants to learn java promotion, Java architecture, web development, big data, data analysis, artificial intelligence and other technologies more easily. Here we share the system teaching resources and expand my Wei (Tongying): CGMX9880 [tutorial / tools / methods / troubleshooting]
Teacher tea = new Teacher("Li Si");
The above can be rewritten as:

Teacher tea;
tea = new Teacher("Li Si");
Here, the constructor "Teacher" is called only once, and "the constructor of the Teacher class is executed" is printed only once.
File name: student java
1 public class Student {
2 public String name;
3 public int age;
4
5 public void study(){
6 System.out.println("student learning");
7 }
8
9 }
File name: unstudent java

1 package com.huaxin.lesson0304;
2 
3 public class UnStudent extends Student{
4     
5 }

As above, it is called class inheritance.
Definition: the subclass inherits all non private properties and methods of the parent class
Format: public class sub class name extends parent class name {}
"Student.java" college students inherit the non private attributes of "Student.java" students.
That is, all students have all the public, college students have.

Topics: Java Javascript Back-end Programmer Data Mining