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 parse the method coverage and variable coverage of Java

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

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to analyze the method coverage and variable coverage of Java, the content is very detailed, interested friends can refer to, hope to be helpful to you.

First, let's take a look at the concise definitions of overloading, and overwriting (rewriting):

Method overloading: if two methods have the same method name but different parameters, it can be said that one method is an overload of the other method.

Method override: if a method is defined in a subclass and its name, return type, and parameter signature exactly match the name, return type, and parameter signature of a method in the parent class, then it can be said that the method of the subclass overrides the method of the parent class

Let's focus on the coverage problem, taking the following code as an example:

Public class People {public String getName () {return "people";}} public class Student extends People {public String getName () {return "student";}} public static void main (String [] args) {People p=new People (); System.out.println (p.getName ()); / / people Student s=new Student () System.out.println (s.getName ()); / / the result is student People pp=new Student (); System.out.println (pp.getName ()); / / the result is student}

The above results show that the getName method of the student class successfully overrides the method of the parent class.

Let's take a look at the coverage of variables:

Public class People {protected String name= "people";} public class Student extends People {protected String name= "student";} public static void main (String [] args) {People p=new People (); System.out.println (p.name); / / the result is people Student s=new Student () System.out.println (s.name); / / the result is student People pp=new Student (); System.out.println (pp.name); / / the result is people}

Through the running results, I found that the coverage of the variable is actually different from that of the method.

In my own words: variable coverage can only be regarded as half-baked coverage at best.

Otherwise, upward conversion and no data loss will occur.

People pp=new Student (); System.out.println (pp.name); / / the result is people

In my personal experience, the coverage of variables is easy to make mistakes. It makes people feel like going back to the inheritance of C++. [this does not mean the inheritance of C++ with virtual]

* Let's look at another piece of code:

Public class People {protected String name= "people"; public String getName () {return name;}} public class Student extends People {protected String name= "student"; public String getName () {return name;}} main (String [] args) {People p=new People (); System.out.println (p.getName ()) / / people Student s=new Student (); System.out.println (s.getName ()); / / student People pp=new Student (); System.out.println (pp.getName ()); / / student}

Obviously, such coverage is the more useful coverage for us, because only in this way can we achieve the goal of abstracting concrete objects into general objects, which is the same as polymorphism.

On how to parse Java method coverage and variable coverage is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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