In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the concept of Java inheritance". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Basic concepts of inheritance and synthesis
Inheritance: you can construct a new class based on an existing class. Inheriting existing classes can reuse the methods and fields of those classes. On this basis, new methods and fields can be added to expand the function of the class.
Compositing: creating existing objects in a new class is called compositing. In this way, you can reuse existing code without changing its form.
1. Inherited syntax
The keyword extends indicates that the new class derives from an existing class. An existing class is called a parent class or base class, and a new class is called a subclass or derived class. For example:
Class Student extends Person {}
The class Student inherits the Person,Person class as a parent or base class, and the Student class as a subclass or derived class.
two。 The grammar of synthesis
Composition is relatively simple, which is to create an existing class in a class.
Class Student {Dog dog;}
Uptracking modeling
1. Basic concept
The role of inheritance lies in the reuse of code. Because inheritance means that all methods of the parent class can also be used in the subclass, messages sent to the parent class can also be sent to the derived class. If there is an eat method in the Person class, there will be this method in the Student class, which means that the Student object is also a type of Person.
Class Person {public void eat () {System.out.println ("eat");} static void show (Person p) {p.eat ();}} public class Student extends Person {public static void main (String [] args) {Student s = new Student (); Person.show (s) / / ①}}
[running result]:
Eat
The show method defined in Person is used to receive Person handles, but what is received at ① is a reference to the Student object. This is because the Student object is also a Person object. In the show method, the handle passed in (a reference to the object) can be a Person object and a derived class object of Person. This behavior of converting a Student handle to a Person handle is called uptracking modeling.
two。 Why do you want to go back to styling?
Why do you deliberately ignore the type of object that calls eat when you call it? It would seem more intuitive to let the show method simply get the Student handle, but that would make every new class derived from the Person class implement its own show method:
Class Value {private int count = 1; private Value (int count) {this.count = count;} public static final Value v1 = new Value (1), v2 = new Value (2), v3 = new Value (3) } class Person {public void eat (Value v) {System.out.println ("Person.eat ()");}} class Teacher extends Person {public void eat (Value v) {System.out.println ("Teacher.eat ()") }} class Student extends Person {public void eat (Value v) {System.out.println ("Student.eat ()");}} public class UpcastingDemo {public static void show (Student s) {s.eat (Value.v1);} public static void show (Teacher t) {t.eat (Value.v1) } public static void show (Person p) {p.eat (Value.v1);} public static void main (String [] args) {Student s = new Student (); Teacher t = new Teacher (); Person p = new Person (); show (s); show (t) Show (p);}}
An obvious drawback of this approach is that it is necessary to define closely related methods for each derived class of the Person class, resulting in a lot of repetitive code. On the other hand, there is no error if you forget the overloading of the method. The three show methods in the above example can be merged into one:
Public static void show (Person p) {p.eat (Value.v1);}
Dynamic binding
When show (s) is executed, the output is Student.eat (), which is really the desired result, but it doesn't seem to be executed in the way we want it to be. Let's take a look at the show method:
Public static void show (Person p) {p.eat (Value.v1);}
It receives a Person handle, and when show (s) is executed, how does it know that the Person handle points to a Student object instead of a Teacher object? There is no way for the compiler to know, which involves the binding problem that will be explained next.
1. Binding of method calls
Connecting a method to a method body is called a Binding. If binding is performed before running, it is called "early binding". In the above example, when there is only one Person handle, the compiler does not know which method to call. Java implements a method invocation mechanism that determines the type of the object at run time, and then calls the corresponding method. This kind of binding at run time based on the type of the object is called dynamic binding. Unless a method is declared that all methods in final,Java are dynamically bound.
Use a graph to represent the inheritance relationship of uptracking modeling:
It can be summarized in code as follows:
Shapes=newShape ()
According to the inheritance relationship, it is legal to assign the created Circle object handle to a Shape, because Circle is a kind of Shape.
When one of the underlying class methods is called:
Shapes=newShape ()
At this point, Circle.draw () is called because of dynamic binding.
Class Person {void eat () {} void speak () {}} class Boy extends Person {void eat () {System.out.println ("Boy.eat ()");} void speak () {System.out.println ("Boy.speak ()") }} class Girl extends Person {void eat () {System.out.println ("Girl.eat ()");} void speak () {System.out.println ("Girl.speak ()") }} public class Persons {public static Person randPerson () {switch ((int) (Math.random () * 2)) {default: case 0: return new Boy () Case 1: return new Girl ();}} public static void main (String [] args) {Person [] p = new Person [4]; for (int I = 0; I < p.resume +) {p [I] = randPerson () / / randomly generate Boy or Girl} for (int I = 0; I < p. Eat; iTunes +) {p [I] .eat ();}
Person establishes a general interface for all classes derived from Person, and all derived classes have both eat and speak behaviors. Derived classes override these definitions and redefine both behaviors. In the main class, randPerson randomly selects the handle to the Person object. * * the appeal modeling occurs in the return statement. * * the return statement takes a handle to Boy or Girl and returns it as a Person type. At this time, you don't know what type it is, only that it is a Person object handle. Call the randPerson method in the main method to populate the array with Person objects, but you don't know the details. When the eat method of each element of the array is called, the role of dynamic binding is to execute the redefined method of the object.
However, there is a premise for dynamic binding, and the bound method must exist in the base class, otherwise it cannot be compiled.
Class Person {void eat () {System.out.println ("Person.eat ()");}} class Boy extends Person {void eat () {System.out.println ("Boy.eat ()");} void speak () {System.out.println ("Boy.speak ()") }} public class Persons {public static void main (String [] args) {Person p = new Boy (); p.eat (); p.speak (); / / The method speak () is undefined for the type Person}}
If no override method is defined in the subclass, the method in the parent class is called:
Class Person {void eat () {System.out.println ("Person.eat ()");} class Boy extends Person {} public class Persons {public static void main (String [] args) {Person p = new Boy (); p.eat ();}}
[running result]:
Person.eat ()
two。 Binding of static methods
Add the static keyword to all the above methods to make them static:
Class Person {static void eat () {System.out.println ("Person.eat ()");} static void speak () {System.out.println ("Person.speak ()");}} class Boy extends Person {static void eat () {System.out.println ("Boy.eat ()") } static void speak () {System.out.println ("Boy.speak ()");}} class Girl extends Person {static void eat () {System.out.println ("Girl.eat ()");} static void speak () {System.out.println ("Girl.speak ()") }} public class Persons {public static Person randPerson () {switch ((int) (Math.random () * 2)) {default: case 0: return new Boy () Case 1: return new Girl ();}} public static void main (String [] args) {Person [] p = new Person [4]; for (int I = 0; I < p.resume +) {p [I] = randPerson () / / randomly generate Boy or Girl} for (int I = 0; I < p. Eat; iTunes +) {p [I] .eat ();}
[running result]:
Person.eat ()
Person.eat ()
Person.eat ()
Person.eat ()
As a result, for static methods, no matter what subclass object the parent class reference points to, the method of the parent class is called.
This is the end of the introduction to the concept of Java inheritance. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.