Java-thinking in Java chapter 6th exercises

Source: Internet
Author: User

1
package one;/** * 包内类 * <p/> * Created by C.L.Wang on 15/8/4. */publicclass One {    publicOne() {        System.out.println("This is one!");    }}/** * Output: This is one! */
import one.One;/** * 包外类 * <p/> * Created by wang on 15/8/4. */publicclass Two {    publicstaticvoidmain(String[] args) {        new One();    }}
2
package  one;/** * Name class * <p/> * Created by Wang on 15/8/4. */ public  class  vector  { public  vector  () {System.out.println (" Wang ' s Vector "); }}
import one.Vector;import// 未使用/** * 测试类名冲突 * <p/> * Created by wang on 15/8/4. */publicclass TestVector {    publicstaticvoidmain(String[] args) {        new Vector();        new java.util.Vector();        System.out.println(vector);        System.out.println(vector2);    }}/** * Output: wang‘s vector [email protected] [] */
3
package  debug;import  java.util.Arrays; /** * Output Information * Created by Wang on 15/8/4. */ public  class  log  { Span class= "Hljs-keyword" >public  static  void     v  (string[] strs) {System.out.println (arrays.tostring (STRs)); }}
package  Debugoff;/** * does not output * Created by Wang on 15/8/4. */ public  class  log  { public  static  void  v  (string[]    STRs) {System.out.println (); }}
importstatic debug.Log.*;//import static debugoff.Log.*; // 无输出/** * 测试输出 * Created by wang on 15/8/4. */publicclass TestLog {    publicstaticvoidmain(String[] args) {        v(args);    }}/** * Output: * (1) null * (2) [My, Name, is, Girl, !] */
4
package  one;/** * Protected access rights * Created by Wang on 15/8/4. */ public  class  packprotected  { protected  void  sayhelloprotected  () {SYSTEM.OUT.PRINTLN ( "Hello Protected" ); } public  void      Sayhellopublic  () {System.out.println ( "Hello public" ); //outside the package cannot access  void  sayhellopackage () {Syst    Em.out.println ( "Hello package" ); }}
import one.PackProtected;/** * protected跨越包继承 * * Created by wang on 15/8/4. */publicclass TestProtected extends PackProtected {    publicstaticvoidmain (String[] str) {        new TestProtected();        t.sayHelloProtected();        t.sayHelloPublic();    }}/** * Output: Hello Protected Hello Public */
5
 PackageOne/** * Test access rights * Created by Wang on 15/8/4. * * Public  class Access {     Public void Sayaccesspublic() {System.out.println ("Public"); }Private void sayaccessprivate() {System.out.println ("Private"); }protected void sayaccessprotected() {System.out.println ("Private"); }voidSayaccess () {System.out.println ("Package"); }}
ImportOne. Access;/** * Test Access Exception * Created by Wang on 15/8/4. * * Public  class callacess {     Public Static void Main(string[] args) {Access A =NewAccess (); A.sayaccesspublic ();//A.sayaccessprivate ();//a.sayaccessprotected ();//A.sayaccess ();}}/** * Output: * (1) public * (2) sayaccessprivate () can be in one. Access private * (3) sayaccessprotected () can be accessed in one. Access protected * (4) sayaccess () in one. Access is not public; it cannot be accessed from an external package * /
6
/** * In-Class processing protected data * <p/> * Created by Wang on 15/8/4. * *Class Data {protected intD = One;} Class ProcessData { Public void Changedata(Data data) {DATA.D = A;//In-Class processing of protected data}} Public  class proteceddata {     Public Static void Main(string[] STRs) {Data data =NewData (); System.out.println (DATA.D);NewProcessData (). Changedata (data);    System.out.println (DATA.D); }}/** * output:11 * *
7
package access;/** * 包类 * Created by wang on 15/8/4. */publicclass Widget {    publicvoidtalk() {        System.out.println("I am a girl!");    }}
import access.Widget;/** * 调用外部类 * Created by wang on 15/8/4. */publicclass TestWidget {    publicstaticvoidmain(String[] args) {        new Widget();        widget.talk();    }}/** * Output: * I am a girl! */
8
/** * Management class * Created by Wang on 15/8/4. * *Class Connection {Private Static intCount =0;//Statistics creation number    Private Connection() {count++; } Public StaticConnectionmakeconnection() {return NewConnection (); } Public int GetCount() {returncount--; }} Public  class connectionmanager {    intCountofconnection =4; connection[] CA =NewConnection[countofconnection];//non-static instance initialization{ for(inti =0; i < countofconnection;        ++i) {Ca[i] = connection.makeconnection (); }    } PublicConnectiongetconnection(inti) {if(I < countofconnection) {returnCa[i]; }Else{return NULL; }    } Public Static void Main(string[] args) {ConnectionManager c =NewConnectionManager (); for(inti =0; I < C.countofconnection +1; ++i) {Connection Connection = c.getconnection (i);if(Connection! =NULL) {System.out.println ("num ="+ Connection.getcount ()); }Else{System.out.println ("NULL"); }        }    }}/** * output:num = 4 num = 3 num = 2 num = 1 NULL * /
9

Because a class without a qualifier is a package access, a class outside the package cannot be called, resulting in a compilation error. You can access the class by placing it inside the class.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java-thinking in Java chapter 6th exercises

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.