In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use the Super keyword in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the Super keyword in Java.
Override the parent class method
1. What is overriding a parent method
In an inheritance relationship, the subclass inherits the method defined by the parent class, and when you make some changes to the inherited method in the subclass, this is overriding the parent method. That is, the subclass contains methods of the same name as the parent class.
two。 Rewriting the parent method follows the principle of "two identical, two small and one big".
The same: the method name and the parameter list are the same.
Two small: the return value type of the subclass method is less than or equal to the value type of the parent method; the subclass method declares that the exception class thrown is less than or equal to the exception class thrown by the parent method declaration.
Big one: the access permission of the subclass method is greater than or equal to that of the parent method.
3. Override the parent method case
For example:
/ / define Animal class class Animal {public String name;// name attribute public int age;// age attribute / / Animal call method void shout () {System.out.print ("animal call");}} / / define that Cat inherits Animal class class Cat extends Animal {void shout () {System.out.print ("Meow ~ ~") } / display name and age method public void show () {System.out.print ("I am" + name+ "," + "this year" age+ ");}} public class p20 {public static void main (String [] args) {/ / TODO Auto-generated method stub Cat c=new Cat (); / / create an instance object of the Cat class c.name =" British short-haired cat "; / / assign c.age=2 to the name attribute of the Cat class / / assign c.show () to the age attribute of the Cat class; / / call the show () method c.shout () of the Cat class; / / call the shout () method of the Cat class}}
The result of the output is:
I am an English short-haired cat. I am 2 years old Meow Meow.
From the above code, the Cat class inherits the shout () method of the Animal class. When the Cat class inherits the Animal class without the shout () method, the shout () method of Animal is called and prints "animal calls". At this time, the specific animal calls cannot be described. Cat objects represent cats, and the call is "Meow Meow ~". Method rewriting is to define a shout () method in the Catsubclass to override the method of the parent class. When the shout () method of the Cat class object is called, only the method overridden by the child class is called, not the shout () method of the parent class.
II. Super keyword
1. What is the super keyword
Super is the direct parent object of the current class and is a reference to the direct parent object of the current object.
two。 If the subclass object cannot access the method that the parent class overrides after the subclass overrides the method, you can use the super keyword to access the members of the parent class
The 3.super keyword calls the member variable and member method format of the parent class
Super. Member variable Super. Member method ([parameter 1, parameter 2, parameter 3.])
Examples of usage of the 4.super keyword
For example:
/ / define Animal class class Animal {String name= "animal"; / / name property int age=6;//age property void shout () {System.out.println ("barking from an animal");}} class Dog extends Animal {String name= "dog"; / / name attribute / / override the parent class's shout method void shout () {super.shout () } / / access the parent member variable void show () {System.out.println ("name:" + super.name+ "," + "Age:" + super.age+ "year!") ;} public class p22 {public static void main (String [] args) {/ / TODO Auto-generated method stub Dog d=new Dog (); / / create a dog object d.shout (); / / call the overridden shout method d.show () of the Dog object; / / call the show method of the Dog object}}
The result of the output is:
Animal call name: animal, age: 6 years old!
From the above code, first define an Animal class, inherit the Animal class in the Dog class, then override the parent class's shout () method, use super.shout () in the subclass to call the parent class overridden method, in the show () method to access the parent class properties, the super keyword is to access the properties and methods.
Third, the super keyword calls the construction method of the parent class
Constructor format of the parent class called by the 1.super keyword
Super ([Parameter1, Parameter2, Parameter3focus...])
A case study of the Construction method of calling parent Class with 2.super keyword
For example:
/ / define Fruit class class Fruit {/ / define Fruit class constructor public Fruit (String name,double weight) {System.out.print ("I am a" + name+ "," + "weight:" + weight+ "g!");}} / define Cherry inheriting Fruit class class Cherry extends Fruit {public Cherry () {super (Cherry, 66) / / call the constructor with parameters of the parent class}} public class p23 {public static void main (String [] args) {/ / TODO Auto-generated method stub Cherry c=new Cherry (); / / instantiate the subclass Cherry object}}
The result of the output is:
I am a cherry with a weight of 66.0g!
From the above code, a Fruit class is defined, and a constructor with parameters is defined in the Fruit class. Define that the Cherry inherits the Fruit class and calls the constructor with arguments to the parent class. The constructor of the Cherry class is called when the Cherry object is instantiated, and the constructor of the parent class is also called when the constructor of the Cherry class is called. The code that super calls the parent class constructor writes the first line of the subclass constructor, which can only occur once.
At this point, I believe you have a deeper understanding of "how to use the Super keyword in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.