In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample analysis of method rewriting in Java", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of method rewriting in Java".
1. Java method rewriting
Method rewriting is that the subclass rewrites the methods inherited by the parent class according to the requirements. When rewriting, you can use the super method to retain the methods in the parent class. Note: constructors cannot be overridden.
Create a human with attributes including name, sex, age, behavior method is to output information
II. Super keyword
When rewriting a method, define a method with the same name as the parent class in the subclass, and use the super keyword super. Method name ();, which implements the rewriting of the method
Package cn.zhz.Kind.dh;public class Person1 {public String sex; public String name; public int age; public void showInfo () {System.out.println ("name:" + name + "age:" + age + "gender:" + sex);}}
Create a student class, the attributes include the student number discipline, the behavior method is to output information
Package cn.zhz.Kind.dh;public class Student1 extends Person1 {public int stuId; public String stuSubject; public void showInfo () {super.showInfo (); System.out.println ("student number:" + stuId+ "discipline:" + stuSubject);}}
Create an instance of the student class, assign it, and output it after assignment
Introduction of package cn.zhz.Case.dh;import cn.zhz.Kind.dh.Student1;public class Student1Test {public static void main (String [] args) {Student1 student1 = new Student1 (); student1.stuId = 1; student1.name = "Zhang San"; student1.age = 18; student1.sex = "male"; student1.stuSubject = "physical"; student1.showInfo ();}} 2.1 super keyword
Super can be applied to subclass access to parent class members, such as:
Access the property super.print () of the parent class; note the non-private method
Access the method super.name of the parent class
Access the constructor super () of the parent class
Package cn.zhz.Inherit.dh;public class Pet {private String name = "John Doe"; private int health = 100; private int love = 0; public int age = 1; public Pet () {System.out.println ("parent class no parameter construction method");} public Pet (String name) {this.name = name } public Pet (String name, int health, int love) {/ / this can call the constructor of this class on the first line / / this (name); this.name = name; this.health = health; this.love = love; System.out.println ("parent class constructor with parameters");} public void setAge (int age) {this.age = age } public int getAge () {return age;} public void setHealth (int health) {if (health)
< 0 || health >{System.out.println ("Pet's health value is between 0 and 100"); this.health = 60; return;} this.health = health;} public int getHealth () {return this.health;} public void setName (String name) {this.name = name } public String getName () {return this.name;} public void setLove (int love) {if (love)
< 0 || love >{System.out.println ("Pet intimacy can only be between 0-100"); this.love = 60; return;} this.love = love;} public int getLove () {return this.love } / / output pet information public void print () {System.out.println ("Pet confession: my name is" + this.name + "Health value is" + this.health + "and owner's intimacy is" + this.love ");}} package cn.zhz.Inherit.dh;public class Dog extends Pet {private String strain =" Smart Labrador " Public Dog () {System.out.println ("no-parameter construction method for subclass dogs");} public Dog (String name, int health, int love, String strain) {/ / call the constructor of the parent class through super, which must be the first sentence / / super (); super (name, health, love); this.strain = strain System.out.println ("dog's parameter construction method");} public void setStrain (String strain) {this.strain = strain;} public String getStrain () {return this.strain;} public void print () {/ / call the parent's non-private print () method super.print (); System.out.println ("I am a" + this.strain) } public void M1 () {/ / super cannot call the private attribute of the parent class / / System.out.println (super.name); System.out.println (super.age);}}
When using the super keyword to access the members of the parent class, use the super keyword, super represents the object of the parent class, super can only appear in the methods and constructors of the subclass, and the constructor can only be the first sentence when the constructor is called with super. Super cannot access the private members of the parent class.
2.2 use of the super keyword
When a method in a subclass overrides the method of the parent class, or defines a member variable with the same name as the parent class in the subclass, use the super keyword to make the masked member visible. .
Package cn.zhz.Inherit.dh;public class Pet {private String name = "John Doe"; private int health = 100; private int love = 0; public int age = 1; public Pet () {System.out.println ("parent class no parameter construction method");} public Pet (String name) {this.name = name } public Pet (String name, int health, int love) {/ / this can call the constructor of this class on the first line / / this (name); this.name = name; this.health = health; this.love = love; System.out.println ("parent class constructor with parameters");} public void setAge (int age) {this.age = age } public int getAge () {return age;} public void setHealth (int health) {if (health)
< 0 || health >{System.out.println ("Pet's health value is between 0 and 100"); this.health = 60; return;} this.health = health;} public int getHealth () {return this.health;} public void setName (String name) {this.name = name } public String getName () {return this.name;} public void setLove (int love) {if (love)
< 0 || love >{System.out.println ("Pet intimacy can only be between 0-100"); this.love = 60; return;} this.love = love;} public int getLove () {return this.love } / / output pet information public void print () {System.out.println ("Pet confession: my name is" + this.name + "Health value is" + this.health + "and owner's intimacy is" + this.love);}}
An age attribute is defined in both the subclass and the parent class. When the subclass calls this age attribute, it will be found in this class first. In this case, you can use the super keyword to represent the parent class, using super. The age property in the parent class is called in the way of the
Package cn.zhz.Inherit.dh;public class Dog extends Pet {private String strain = "Smart Labrador"; private int age = 10; public Dog () {System.out.println ("No parameter construction method for child dogs");} public Dog (String name, int health, int love, String strain) {/ / call the constructor of the parent class through super, which must be the first sentence / / super () Super (name, health, love); this.strain = strain; System.out.println ("dog's parameter construction method");} public void setStrain (String strain) {this.strain = strain;} public String getStrain () {return this.strain;} public void print () {/ / call the parent class's non-private print () method super.print () System.out.println ("I am a" + this.strain);} public void M1 () {/ / super cannot call the parent class's private attribute / / System.out.println (super.name); System.out.println (super.age);} public void m2 () {/ / subclass overrides the subclass System.out.println (this.age) of the same name / / you can use the super keyword to call System.out.println (super.age), a member whose parent class is overridden by a subclass;}}
The difference between super and this
Distinguish the thissuper access attribute to access the properties of this class. If not, find the property access method in the parent class to access the method of this class. If you do not find the method access constructor of the parent class from the parent class, call the constructor of this class, and the first line of the constructor of the method calls the constructor of the parent class, which is placed in the first line of the constructor of the subclass
To sum up, this represents this class, while super represents the parent class.
Because super represents a parent class, if you declare multiple classes, do you want to use super.super. If you want to access members of the parent class in a subclass? The way?
Package cn.zhz.Kind;// grandfather class public class Animal {private int age; private String sex; public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getSex () {return sex;} public void setSex (String sex) {this.sex = sex Public String getSid () {return sid;} public void setSid (String sid) {this.sid = sid;} @ Override public void print () {/ / visits members of the parent class of the parent class and cannot use super.super. You can directly use super.print (); to call super.print (); System.out.println ("this is a method under the Sun Tzu class");}} package cn.zhz.Instance;import cn.zhz.Kind.Student;public class StudentTest {public static void main (String [] args) {Student student = new Student (); student.print ();}}
When the Student grandchild class calls the Animal grandchild class, it is also through super. Member, this is because java only supports single root inheritance, and a class can only have one direct parent class, but a class can have multiple indirect parent classes
3. The calling rules of the inherited constructor package cn.zhz.Kind;public class Car {private int site = 4; Car () {System.out.println ("carrying capacity is" + site+ "person");} public void setSite (int site) {this.site = site;} public void print () {System.out.println ("carrying capacity is" + site+ "person");}} package cn.zhz.Kind Public class Bus extends Car {public Bus (int site) {setSite (site);}} package cn.zhz.Instance;import cn.zhz.Kind.Bus;public class BusTest {public static void main (String [] args) {Bus bus = new Bus (20); bus.print ();}}
When the subclass constructor invokes the parameterized constructor of the parent class through the super display, it executes the corresponding constructor of the parent class instead of the parameterless constructor of the parent class
The subclass constructor calls its own other constructors through the this display, and applies the above two rules to the corresponding constructors.
Fourth, in-depth understanding of method rewriting
Method rewriting rules
Method name is the same
The parameter list is the same
Return value of the same type or its subclass
Access permissions cannot be stricter than the parent class
The static method of the parent class cannot be overridden by the subclass as a non-static method, and the non-static method of the parent class cannot be overwritten as a static method by the subclass.
A subclass can be defined on a static method of the parent class with the same name to hide the static method of the parent class in the subclass (super cannot be used in static methods)
Private methods of a parent class cannot be overridden by a subclass
You cannot run out more exceptions than the parent method
Package cn.zhz.Kind;public class Father {public void M1 () {System.out.println ("method of M1 of parent class");} / / method overloading public String M1 (int num1) {return "test" in the same class with the same name and different parameters } / / method return value type can be custom data type public Father m2 () {System.out.println ("parent class m2 method"); return new Father ();} public static void m3 () {System.out.println ("parent class static method m3");} private void m4 () {System.out.println ("parent class private method M4") }} package cn.zhz.Kind;public class Son extends Father {/ / subclass override methods cannot have less access than parent methods, and can expand the access permissions of methods / / subclass methods constitute method rewriting public void M1 () {System.out.println ("M1 methods after subclass overrides") as long as the access permissions are not stricter than those of the parent class. } / / method rewriting is the parent class with the same name and parameter / / the return value type of the subclass method can be a subclass of the value type returned by the parent class method, and it is also the method rewriting public Son m2 () {System.out.println ("m2 method after subclass rewriting"); return new Son () } / * the static method of the parent class cannot be rewritten as a non-static method, and vice versa, the non-static method of the parent class cannot be rewritten as the static method public void m3 () {System.out.println ("non-static method m3 of the subclass") } * / / A static method public static void m3 () can be defined in a subclass exactly like the parent class {/ / cannot use super// super.m3 (); Father.m3 (); System.out.println ("non-static method m3 of a subclass") in static methods } public static void main (String [] args) {Son son = new Son (); son.m1 (); son.m2 (); Son.m3 () }} compare item location method name parameter list return value permission access modifier method rewrite subclass the same or its subclass cannot be more stringent than the parent class reload the same kind the same but different irrelevant above is all the content of the article "sample Analysis of method rewriting in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.