[Java Issues] inherited method invocation issues

Source: Internet
Author: User

Because of the time in Java is not long, let me put a software from Matlab to Java, so there are a lot of problems, the quickest way to solve these problems is to write some small code debugging and Internet to ask Niang, another way is to ask seniors, But when they describe a problem more than when I search the Internet, and they may not know the answer, so the second method I seldom use, this is the reason I write this article, the problem is written down, slowly thinking, with the improvement of Java technology, may be resolved, even if not resolved, the next time programming, I also know that writing code is not up to my requirements, to change the method.

Having said so much, I have encountered the problem of inheriting method calls, let's look at an example:

Files 1 Name: First.java

public class First
{
Private double A = 10;
Private double b = 20;

Public First ()
{

}

Public first (double a,double b)
{
THIS.A =a;
this.b =b;
}

Public double Geta ()
{
return A;
}

Public double Getb ()
{
return b;
}

}

  

Files 2 Name: Second.java


public class Second extends first
{
Private double A;
private double B;
First first;

Public Second (first first)
{
This.first = First;
A = First.geta ();
b = First.getb ();
}

}

Files 3 Name: Test.java (the file where the main function resides)

public class Test
{
public static void Main (String [] args)
{
First firstmain = new first (30,40);
Second Secondmain = new Second (firstmain);
Double testa = Secondmain.geta ();
Double testb = Secondmain.getb ();
System.out.println (testa);
System.out.println (TESTB);
}

}

The main function is debugged with debug:

The output is: 10, 20. Obviously this is not the answer I want, the answer I want is 30, 40.

Rewrite the method in document 1 in document 2 to get the answer I want. as follows:

In document 2, add the Geta () and Getb () methods, which are overrides (@Override).

The results of debugging the main function are as follows:

  

For the above questions, consider a different idea, if not inherited, in document 2 in the Write Geta () and Getb () method, that is, document 2 of the complete Java program as follows:

public class Second//Here is not the same as in previous document 2, this is no longer a extends inheritance relationship
{
Private double A;
private double B;
First first;

Public Second (first first)
{
This.first = First;
A = First.geta ();
b = First.getb ();
}


Public double Geta ()
{
return A;
}

Public double Getb ()
{
return b;
}

}

Other document programs do not change, debugging results are as follows:

The answer is also what I want 30, 40.

  

Although both of these methods can achieve the results I want, but using extends can save duplicate input code, so, how to use the inheritance relationship to achieve the results I want, at present I have no way to solve.

P.S.

It is easy to understand the difference between inheritance and no inheritance when comparing secondmain with inherited and no inheritance variables:

(Attached again to attached: below)

Inheritance relationship secondmain variable secondmain variable with no inheritance relation

[Java Issues] inherited method invocation issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.