Verification of member method and member variable access rights

Source: Internet
Author: User

Package Com.jaovo;/** *_1_ member Variable access rights verification * Public Private protected default (permissions) * Self-package own class accessible Access to accessible * own packages other classes can access non-accessible access to accessible * Other classes can access non-accessible inaccessible Ask inaccessible * Own package other classes have an inheritance relationship accessible inaccessible accessible * Other classes have an inheritance relationship accessible inaccessible accessible *-------- -------------------------------------------------------------------*_2_ member Method access permission * Public Priva       TE protected defaults (default permissions) * Self-package class can access accessible access accessible * Own package other classes can access inaccessible          Accessible * Other classes can access the other class can access non-accessible inaccessible * own package other classes have an inheritance relationship can access inaccessible accessible Classes that can access * Other packages have an inheritance relationship that can be accessed not accessible accessible*/import Cn.jaovo.d;import cn.jaovo.e;//Public class chengyuan{//Public class Chengyuan extends c{//Public class Chengyuan extends d{ Public classChengyuan extends e{ Public Static voidMain (string[] args) {//permission access for a member variable    /*1 b b = new B ();//own Bag other class System.out.println (B.I1);        System.out.println (B.I2);        System.out.println (B.I3);//i3 can access the private System.out.println (B.I4) in B; */    /*2 Chengyuan ch = new Chengyuan ();//self-pack own class System.out.println (CH.I1);        System.out.println (CH.I2);        System.out.println (CH.I3);        System.out.println (CH.I4); */    /*3 d = new D ();//Other Package class System.out.println (D.I1); System.out.println (D.I2);//i2 is not public in D; it cannot be accessed from an external package System.out.println (D.I3);//i3 can access private syste in D M.out.println (D.I4);//i4 can access the protected in D*/    /*4 Chengyuan ch = new Chengyuan ();//own package other classes have inheritance relationship Chengyuan extends C System.out.println (CH.I1);        System.out.println (CH.I2);        System.out.println (CH.I3);//i3 can access System.out.println (CH.I4) in B; */    /*5 Chengyuan ch = new Chengyuan ();//Other packages class has an inheritance relationship Chengyuan extends D System.out.println (CH.I1); System.out.println (CH.I2);//i2 is not public in D; it cannot be accessed from an external package System.out.println (CH.I3);//i3 can access private Syst in D        Em.out.println (CH.I4); *///======================================================    //two access rights for member methods        /*1 Chengyuan ch = new Chengyuan ();//self-pack own class System.out.println (CH.M1 ());        System.out.println (ch.m2 ());        System.out.println (CH.M3 ());           System.out.println (CH.M4 ()); */        /*2 b b = new B ();//Bag Other class System.out.println (B.M1 ());        System.out.println (b.m2 ());        System.out.println (B.M3 ());//m3 () can access the private System.out.println (B.M4 ()) in B; */        /*3 E e = new E ();//Other Package Class System.out.println (E.M1 ()); System.out.println (e.m2 ());//m2 is not public in E, it cannot be accessed from an external package System.out.println (E.M3 ());//m3 can access private Sy in E Stem.out.println (E.M4 ()); M4 () can access the protected in E*/        /*4 C = new C ();//own package other classes have inheritance relationship Chengyuan extends C System.out.println (C.M1 ());        System.out.println (c.m2 ());           System.out.println (C.M3 ());//m3 () can access System.out.println (C.M4 ()) in C; */        //5Chengyuan ch =NewChengyuan (); System. out. println (CH.M1 ()); System. out. println (Ch.m2 ());//symbol not foundSystem. out. println (Ch.m3 ());//symbol not foundSystem. out. println (CH.M4 ()); }}classb{//1 member variables     Public intI1 = -; intI2 = $; Private inti3 = -; protected intI4 = -; //2 Member Methods     Public intM1 () {return 1;} intM2 () {return 1;} Private intM3 () {return 1;} protected intM4 () {return 1;}}classc{//1 member variables     Public intI1 = -; intI2 = $; Private inti3 = -; protected intI4 = -; //2 Member Methods     Public intM1 () {return 1;} intM2 () {return 1;} Private intM3 () {return 1;} protected intM4 () {return 1;}}//========================================================//d.class files and e.class files in the CN package, in order to conveniently put them herePackage Cn.jaovo; Public classd{//1 member variables     Public intI1 = -; intI2 = $; Private inti3 = -; protected intI4 = -; //2 Member Methods     Public intM1 () {return 1;} intM2 () {return 1;} Private intM3 () {return 1;} protected intM4 () {return 1;}}//-------Package Cn.jaovo; Public classe{//1 member variables     Public intI1 = -; intI2 = $; Private inti3 = -; protected intI4 = -; //2 Member Methods     Public intM1 () {return 1;} intM2 () {return 1;} Private intM3 () {return 1;} protected intM4 () {return 1;}}

Verification of member method and member variable access rights

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.