JAVA learning lesson 11th (inheritance), java learning inheritance

Source: Internet
Author: User

JAVA learning lesson 11th (inheritance), java learning inheritance
Inheritance: (extends)

Many classes, but some members are repeated. To provide reusability, duplicate code is extracted, and inheritance is applied.

1. Improved code reusability

2. Relationship between classes,Inheritance provides the premise for polymorphism, and there is no polymorphism without inheritance.


Class MAN {String name; int age;} class student extends MAN // MAN is the base class of student, and student is the subclass of MAN {void study () {System. out. println (name + "Learning" + age);} class teacher extends MAN {void work () {System. out. println (name + "working" + age) ;}} public class Main {public static void main (String [] args) {student BLF = new student (); BLF. name = "BLF2"; BLF. age = 20; BLF. study ();}}


Concept diagram:


Single inheritance and multi-Inheritance

JAVA supports single inheritance and does not directly support multi-inheritance. However, it is improved on the Multi-Inheritance Mechanism of C ++.

Definition:


1. Single inheritance: a subclass can only have one direct parent class.

Class A extends B, B extends C;

2. Multi-inheritance: a subclass can have multiple direct parent classes (not allowed in java.

Class A extends B; A extend C;

Advantages of multiple inheritance:

Class A has some variables. Class B has some things. If C inherits A and B, C has both A and B.

Disadvantages of C ++ multi-inheritance:

See the Code:

# Include <iostream> using namespace std; class A {public: void show () {cout <"sd" <endl ;}} class B {public: void show () {cout <"sssd" <endl ;}} class C: public A, public B // A, B show are all the same function name {} C BLF; int main () {BLF. show (); // here, the multi-inheritance of c ++ code will lead to confusion. You must make show, show1, and show2 different in names before you can return 0 ;}


Multi-inheritance in java: Multi-inheritance is not directly supported, because once the same members exist in the parent class, uncertainty will occur. java improves the C ++ defect.


Java multi-inheritance is implemented through "Multi-Implementation ".

Java supports multi-layer (Multi-Level) Inheritance: D inherits C, C inherits B, and B inherits A, so the inheritance system appears,

Note:: To use an inheritance system:

1. view the top level of the system and understand the basic functions of the system.


2. Create the most subclass object of the system and use the function

Inheritance System diagram:





When to define inheritance?

Use inheritance when there is a relationship between classes. For example, if xxx is a type of yyy, xxx inherits from yyy and who is one of them.

Affiliation: A is a B; A inherits from B

Features of the member variables in the Child parent class:

1. member variables
2. member functions
3. Constructor

I. member variables

class father{int num = 5;}class son extends father{int num = 6;void show(){System.out.print(num+" : "+num);//6 : 6}}public class Main{public static void main(String[] args){son zSon = new son();zSon.show();}}



Sub-classes are included, and you are never looking for the parent class

When the name of a Member in this class is the same as that of a local variable, use this to distinguish it. When the name of a member in the Child parent class is the same as that of a local variable, use super to distinguish it.

The usage of super is similar to that of this.

class father{int num = 5;}class son extends father{int num= 6;void show(){System.out.print(this.num+" : "+super.num);// 6 :5}}public class Main{public static void main(String[] args){son zSon = new son();zSon.show();}}



The above code, as an understanding, the parent class has num, and the Child class inherits from the parent class naturally also has num

Differences:

This: indicates the reference of a class.
Super: represents the space of a parent class.

Sub-parent memory diagram:



The subclass cannot be accessed directly. The private content in the parent class can be accessed indirectly. This is not common during development and is often seen during interviews.

class father{private int num = 5;public int getnum(){return num;}}class son extends father{int num= 6;void show(){System.out.print(this.num+" : "+super.getnum());}}public class Main{public static void main(String[] args){son zSon = new son();zSon.show();}}


Ii. member functions


class father{void show1(){System.out.println("father");}}class son extends father{void show2(){System.out.println("son");}}public class Main{public static void main(String[] args){son zSon = new son();zSon.show1();zSon.show2();}}

When the sub-parent class contains the same member functions (same function names, consistent return types, and the same list of function parameters), the sub-class functions will be run to override the operation, the operation of the parent class is overwritten, which is a feature of the function in the Child parent class.

class father{void show(){System.out.println("father");}}class son extends father{void show(){System.out.println("son");}}public class Main{public static void main(String[] args){son zSon = new son();zSon.show();zSon.show();}}



Two features of a function: 1. Reload 2. Overwrite (rewrite/Rewrite)

The overload is in the same class, And the overwrite is in the subclass.

Overwrite note:

1. when the subclass method overwrites the parent class method, the subclass permission must be greater than or equal to the permission of the parent class to overwrite it (the above Code cannot be overwritten by adding public before the parent class show, son's show can also be added to public. One of them cannot be modified using static. Both must be static)

2. Static coverage can only be static or


When to use overwriting?

When extending a subclass for a class, the subclass needs to retain the function declaration of the parent class. However, when defining the special functions of the subclass, the override operation is completed.


For example, call: Call display function. you want to add a number from a region later.

Class phone {void show () {System. out. println ("number");} void call () {} void memssage (){}//....} class phone2 extends phone // some functions in the phone do not need to be modified, so use the inheritance {void show () // you only need to modify the show function {System. out. println ("from"); super. show (); // retain the function of the parent class} public class Main {public static void main (String [] args) {phone2 zSon = new phone2 (); zSon. show ();}}

Constructor features in the sub-parent class:

class father{father() {System.out.println("fu");}}class son extends father{son() {System.out.println("so");}}public class Main{public static void main(String[] args){//super();new son();}}



Print fu
So


When a subclass constructs an object, it is found that when accessing the subclass constructor, the parent constructor also runs?

In the first line of the subclass constructor, there is a default implicit statement super (); There is super (), indicating that the constructor is calling the null parameter in the parent class.
(If the parent class constructor has a parameter, super will write it on its own)

class father{father() {System.out.println("A");}father(int x) {System.out.println("B");}}class son extends father{son() {//super();System.out.println("C");}son(int x) {super(x);System.out.println("D"+x);}}public class Main{public static void main(String[] args){new son(6);}}



During subclass instantiation, all constructors in the subclass access the constructor of the null parameter of the parent class.

Why access the constructor of the parent class during instantiation?

Subclass inherits the parent class and has the content (attributes and actions) of the parent class. Therefore, before using the content of the parent class, you must know how the parent class is initialized, therefore, when constructing an object, the subclass must access the constructor in the parent class. To complete this required action, add the default super () to the constructor of the subclass ().
If no empty parameter constructor is defined in the parent class, the constructor of the subclass must use super to explicitly call the corresponding constructor in the parent class.

class father{int num;father() {num = 8;System.out.println("A");}}class son extends father{son() {System.out.println("C"+num);}}


If the subclass does not access the constructor of the parent class, print C 0. Therefore, it is necessary to access the constructor of the parent class.
When the memory space is set up, the num in the parent class will be displayed in the memory of the Child class. Therefore, the Child class must know how to initialize the parent class.

Note: The super statement must be in the first line of the subclass constructor. Because the initialization action of the parent class must be completed before the subclass.

This and super:


This and super must be in one row in the constructor. Does that cause a conflict.
In a subclass function, if this is used to call the original constructor, but both this and super must house the first line of the constructor of this class, so only one
However, other constructors in the subclass must access the constructor of the parent class.


The Code is as follows:

Class father {int num; father () {num = 8; System. out. println ("A") ;}} class son extends father {son () {}son (int x) {this (); // use this to save super. cause: use this to call son () of this class, while son () {} inherits from the default super () in father, son (); there must be at least one constructor accessing the parent class. out. println ("C" + num) ;}} public class Main {public static void main (String [] args) {new son (6 );}}


Subclass instantiation process:

class father{father() {show();}void show(){System.out.println("asdf");}}class son extends father{int num = 8;son(){super();//System.out.println("dfsdf");}void show(){System.out.println("num is "+num);}}public class Main{public static void main(String[] args){son s = new son();s.show();}}
Print:
Num is 0
Num is 8


Open the comments section of the above Code.
Print:
Num is 0
Dfsdf
Num is 8


Class father {father () {show ();} void show () {System. out. println ("asdf") ;}} class son extends father {int num = 8; son () {super (); // when the parent class content is initialized through super, the subclass is not initialized. After the super () parent class is initialized //, the member variables of the subclass are carried out. The initialization-> watershed} void show () {System. out. println ("num is" + num) ;}} public class Main {public static void main (String [] args) {son s = new son (); s. show ();}}

/** An Object Instantiation process * son s = new son (); * 1. JVM will read son in the specified path. class, and load it into the memory. If there is a direct parent class, first load the parent class * 2. open up space in heap memory and allocate the address * 3. and in the object space, initialize the properties of the object by default * 4. call the corresponding constructor for initialization * 5. in the first line of the constructor, the constructor of the parent class is called to initialize * 6. after the parent class is initialized, The subclass attributes are displayed and initialized. * 7. then perform specific initialization of the subclass constructor * 8. after the subclass Initialization is complete, assign the address value to the reference variable **/




What are the words used to calculate from now on in Lesson 11th of the seventh grade of the Chinese Jiangsu Education Edition,

The seventh-grade Chinese version of Jiangsu Education
Instructional Design

I. Word Teaching

Zhengyin

Zhou Ding's FF ng views zh won n Jin li ús su JIU bi o

Fifty-fifty

Explanatory words

① Stands tall and straight.

② Immortality: the glorious career and the Great Spirit will never be wiped out. Deleted: wiped out.

③ Passionate: filled with upright spirits, exciting and high spirits.

④ Great achievements: great achievements. Abundant, versatile, and prosperous.

⑤ Relatively far: far from each other. Distant: it refers to the distance.

6. surging: it is a metaphor for the constant emergence of new things, which is unstoppable. Surging, rising, and popping up.

Ii. Problem Solving

In April 22, 1958, the Monument to the People's Heroes was just built and has not yet been unveiled. the people throughout the country eagerly hope to learn about the monument in a timely manner. Xinhua News Agency reporter Zhou dingyi paid tribute to the monument, according to the introduction of Comrade Jia Guoqing, head of the construction site, I wrote this article. This article was first published in the People's Daily in April 23, 1958, followed by a close-up of post-income prose in Beijing Qianjin.

The title is "the eternal heroes of the People". These eight words are the inscription on the monument. This is the question, indicating the object to be praised by the author and revealing the central meaning of the full text.

The subtitle "viewing the Monument to the People's Heroes of the Capital" indicates the objects described in this article.

-- This is plain text.

Iii. Clarify writing ideas

Find out the words in the text indicating the "View" activity and divide the article into paragraphs.

The first part (§ 1) begins with a question.

The second part (§ 2-§ 10) describes the position, construction process, structure, inscription, inscription, and ten relives of the monument. This is the main part of the full text, which can be divided into three layers.

1 (§ 2 and 3) introduce the position, environment, construction process, scale and materials of the monument.

2 (§ 4 and 5) introduce the structure, inscription, decoration and shape of the monument.

3 (§ 6-§ 10) describes the ten relives around the big inscription blocks in sequence.

The third part (§ 11) expresses the deep feelings behind the view, echo the title and the beginning.

Iv. content understanding

In the first part, which words represent the characteristics of the monument?

Lofty, majestic, and solemn.

Part 2

1. Why is the construction process introduced in Section 3 of the text inserted?

The introduction to the construction process is both an integral part of the monument and a reflection of the victory of the party, the leaders of the nation, and the people of the whole country after the victory of the revolution, the high care and attention to the construction of the monument highlight the great significance of the monument.

2. What is the meaning of "One thousand eight hundred and forty years" in the inscription "over three years" and "over thirty years? What heroes do the Monument to the People's Heroes commemorate? (Exercise 1)

The "more than three years" refers to the Liberation War between the last three years from 1946 to 1949 in the new democratic revolution of China; "Thirty years ago" refers to the entire historical period from the 1919 "Wusi movement" to the victory of the Chinese new democratic revolution in 1949. "One thousand eight hundred and forty years" refers to the first Opium War outbreak in 1840, this marks the beginning of China's old democratic revolution.

It can be seen that the Monument to the People's Heroes honors the old democratic revolution, the new democratic revolution, and especially the three-year liberation war. In order to oppose internal and external enemies and strive for national independence and the freedom and happiness of the people, the people's heroes who have sacrificed in the previous struggles.

3. How can we understand the symbolic meaning of the "decorative pattern" and the "Big Flower circle" on the top of the east and west sides of the monument, composed of peony, Lotus, chrysanthemum, etc?

Red Stars and banners are important symbols of revolutionaries. Since ancient times, Song Bai has praised them for their unique and vigorous character, which is not afraid of the cold storms. Therefore, the decoration pattern composed of red stars, pine cypress and banners symbolizes the spirit of the revolutionary martyrs.

Lesson 11th bat and radar answers

Bats use ultrasonic waves to explore the path. When an ultrasonic wave encounters an obstacle, it is reflected back and transmitted to the bat's ears.
Do you want this answer?

Respondent: ye Xiangyang, a student from Class 5, Grade 4, Central Primary School, haiyou town

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.