Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JAVA inheritance, constructor, override and overload methods

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "JAVA inheritance, construction methods, rewriting and overloading methods how to use", detailed content, clear steps, details handled properly, I hope that this "JAVA inheritance, construction methods, rewriting and overloading methods to use" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

Construction method

The constructor of the class is a special method, and the name of this method must be consistent with the name of the class. The constructor cannot have a return value without using void, cannot be called directly, and can be called automatically when the class object is instantiated, or when new is called. General constructors are used for initialization when class objects are instantiated. If a class does not write a constructor, the system automatically adds a no-parameter constructor to this class at compile time. If the declaration class writes a constructor, the system no longer adds a parameterless constructor. It is recommended that when you finish writing a constructor, it is best to write a nonparametric constructor.

Non-parametric structure

To put it bluntly, it is the construction method without parameters.

If you don't understand the concept, for example, it's clear.

Declare a class A

Public class A {}

Create a constructor A () in class An and print out a sentence

The constructor must be the same as the class name

Public class A {public A () {System.out.println ("Construction method A ()");}}

Create a new test class and nuw an object of class An in the class

Public class Test {public static void main (String [] args) {An a = new A ();}}

Try to execute the main method

A constructor is equivalent to a method that is used automatically in a new object

Parametric structure

Parametric construction is a method of construction with parameters.

Declare a constructor with parameters in class A, passing in two parameters of type String, an and b

Public class A {public A () {} public A (String a, String b) {System.out.println (a + b);}}

Pass parameters when new an object an in a test class

Public class Test {public static void main (String [] args) {An a = new A ("aaa", "bbb");}}

Execute the main method

Be careful

When there is no constructor, a no-parameter construct is hidden in the class. But create a parametric construct, and the hidden non-parametric construct will disappear. Then the new object can only take parameters in the future. So when you have a parametric construction, you must create a no-parameter construction method to put it there.

Inheritance of class

In the java language, the class class is single inheritance and multiple implementation interfaces. Interface interface is multi-inherited.

Why inherit it? Because the subclass wants to add new functionality to the parent class.

Subclasses inherit parent classes can inherit methods and properties in the parent class

The following examples are analyzed:

The parent class is the person, and the child class is the old driver.

Human characteristics are: two hands, two eyes, two feet, can eat and drink

The old driver inherited the human characteristics and added the function of being able to drive.

The code is as follows:

This is a human class with the following attributes and functions.

Public class Ren {public final String shou = "two hands"; public final String jiao = "two feet"; public final String yan = "two eyes"; public void chi () {System.out.println ("can eat");} public void he () {System.out.println ("can drink");}}

This is the category of old drivers, who inherit human beings. And add a function that can drive a car.

Public class Siji extends Ren {public void kai () {System.out.println ("can drive");}}

Test: create the output property of the veteran driver object and call the method.

Public class Test {public static void main (String [] args) {Siji b = new Siji (); System.out.println (b.jiao); System.out.println (b.shou); System.out.println (b.yan); b.chi (); b.he (); b.kai ();}}

Method rewriting, overloading

Override overrides: the method name, return type, and parameters are all the same. If there is such a situation, it must be an inheritance relationship.

Overloading: methods with the same method name, return type, number of formal parameters, and methods with different types do not have to be inheritance relations, but can be found in the same class, such as constructor overloading

Rewrite

Because the subclass is not satisfied with the method of the parent class, I want to change it. This is how the subclass overrides the parent class.

For example, people can eat and drink, but the old driver does not like it will eat this function, want to change it, change it to eat shit.

The code is as follows:

The veteran driver rewrites the human chi () method.

The method name must be the same.

Public class Siji extends Ren {public void kai () {System.out.println ("can drive");} public void chi () {System.out.println ("can eat shit");}}

Now override the run test class

Heavy load

Parametric construction and non-parametric construction are method overloading. There are two methods with the same name in a class, but their return value type and parameter type are different.

The return value type and parameter type of the two methods, and the number of parameters, as long as one is different. But they have the same method name, so both methods are overloaded.

Read this, the "JAVA inheritance, construction methods, rewriting and overloading methods how to use" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report