UT a private variable for learning

Source: Internet
Author: User

<body background= ". /picture/bg_body.gif "></body>
<pre>
public class Pmmmlexport {
Private DEBUGPRN logger = new Debugprn (PmMmlExport.class.getName ());
Private Boolean Mmlasyn;

Private Exportutil Exportutil = new Exportutil ();
Private csvprocess csvprocess = null;
Private string[] Counterids = null;

Public Pmmmlexport (Boolean Mmlasyn) {
This.mmlasyn = Mmlasyn;
}

Public csvprocess getcsvprocess () {
return this.csvprocess;
}

Public string[] Getcounterids () {
return counterids;
}

public void Setcounterids (string[] counterids) {
Counterids = Counterids;


----------------------------------------------------------------------------------


public class Exportutil {

private static DEBUGPRN logger = new Debugprn (ExportUtil.class.getName ());

Public Exportutil () {
Initdir ();
}

public void Initdir () {
String TmpDir = Pathutil.gettmpcmdir ();
if (new File (TmpDir). Exists ()) {
Fileutil.clear (TmpDir);
} else {
Fileutil.mkdirs (TmpDir);
}
}


</pre>

----------------------------------------------------------------------------------

<p> Scene:</p>

<p> need to "UT" a method in Pmmmlexport, so create objects of this class,
Member variables are defined in this class private exportutil Exportutil = new Exportutil () <br/>
The exportutil is initialized when the object is created, but the system-provided method is called in the Exportutil constructor method, which is "effective" when the system is running <br/>
Therefore, the object that created the Pmmmlexport will fail </p>

<br/>
<br/>
<br/>

<p> Solutions:</p>

<p> Scenario One: Because the member variable (exportutil) is private, by inheriting Pmmmlexport, and then creating the object "avoid" member variables (exportutil) initialization </p>
<p> Error: Private variables are "inherited" (not inherited??). "To subclasses, only subclasses cannot be accessed directly (through methods)
Regardless of the private, public,protected will inherit the quilt class </p>


<p> two: In subclasses, the re-declaration and initialization of member variables (exportutil) is private exportutil Exportutil = null, in this way overrides </P>
<p> Error: <br/>
1) A member variable inherited from a parent class whose access control remains the same. <br/>
2) A subclass defines a member variable with the same name as the parent class, and does not overwrite the member variable of the parent class, but two member variables coexist. </p>


<p> when the object is created, the member variable is "initialized" <br/>
The idea in scenario one is to "mask" the private member variable in the parent class by inheriting it (visible cannot be masked) <br/>
The idea in scenario two is "to override inherited from the parent class by declaring a member variable of the same name" (coexistence of both) </p>


<p><font color= "Red" > Thinking Adjustment:</font>
The above operation is not aware of the problem: When creating an object, the "initialization" of the member variable is in the process,<br/>
Init is unsuccessful, objects cannot be created successfully <br/>
So the idea of a mock is: before the new Class (), you should mock the private member variable </p>
<br/>
<br/>
<br/>

<p> follow this idea for other design:</p>

<p> three: Through the "anonymous class", overriding the construction method, in "Construction method", replace "member variable",
<br/> The key question is whether the construction method is before or after the member variable is initialized </p>

<p> Scenario Four: The process of getting a "member variable" through reflection does not require the creation of a specific object,<br/> but on the basis of the Replace member variable value or create object </p>



UT a private variable for learning

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.