There is a problem in writing code: The package defines an interface class, another package to implement it, here using the interface isolation and dependency inversion principle, the two packets of dependencies upside down. However, there is a problem where the implementation class is instantiated using the factory pattern, so the implementation class does not want to be exposed to the package, but the implementation class also implements the public interface. So here's the question, if the class is a default type, and the member function is public, what is the access limit?
Implementation validation
1. First define an interface class in a package:
Package mytest Public
interface classaccesstest
{
void GetData ();
Charseqence getString ();
}
2. Define implementation classes and factories in the implementation package
Package classaccesstest
Import mytest. Classaccesstest
class Classaccesstestimp implements Classaccesstest
{
int mA = 0;
Classaccesstestimp (int a)
{
MA = A;
}
public void GetData ()
{
System.out.printlin (' The data is ' + MA);
}
Public charsequence getString ()
{return
("The data is" + MA);
}
Package classaccesstest
Import mytest. Classaccesstest public
class Factory
{public
static clasaccesstest getaccesstest ()
{
return new Classaccesstestimp (ten);
}
}
3. Then use the interface in the original package:
Package mytest
Import classacesstest. Factory
//import classaccesstest. Classaccesstestimp public
class Testmain
{public
static void Main (String arg[])
{
Factory.getaccesstest (). getString ();
<span style= "White-space:pre" > </span>//<span style= "font-family:arial, Helvetica, Sans-serif;" >classaccesstest test = </span>new <span style= "font-family:arial, Helvetica, Sans-serif;" >classaccesstestimp (Ten);</span>
}
}
4. Results:
In the Testmain class, the annotated part works correctly, and the commented out section is used to make an error, classaccesstestimp undefined
Conclusion
It is inferred from the above that the access rights of classes in Java can be less than the access rights of members, depending on the inverted and package packaging requirements, can be achieved to meet the requirements.