Java-thinking in Java Chapter 5th exercises

Source: Internet
Author: User

1
//: Main.java/** * String初始化 */class Test {    String s;}class Main {    publicstaticvoidmain(String[] args) {        new Test();        System.out.println("s = " + t.s);    }}/** * Output: s = null *///:~
2

The initialization effect is the same, the member variable definition is determined when the definition is initialized, and is determined when the constructor is constructed.

//: Main.java/** * 初始化 */class Test {    String s1;    "haha";    String s3;    Test () {        "hehe";    }}class Main {    publicstaticvoidmain(String[] args) {        new Test();        System.out.println("s1 = " + t.s1);        System.out.println("s2 = " + t.s2);        System.out.println("s3 = " + t.s3);    }}/** * Output: s1 = null s2 = haha s3 = hehe *///:~
3
//: Main.java/** * 默认构造器 */class Test {    Test () {        System.out.println("This is Test");    }}class Main {    publicstaticvoidmain(String[] args) {        new Test();    }}/** * Output: This is Test *///:~
4
//: Main.java/** * 默认构造器 */class Test {    Test () {        System.out.println("This is Test");    }    Test (String s) {        System.out.println("Test : " + s);    }}class Main {    publicstaticvoidmain(String[] args) {        new Test("Hello");    }}/** * Output: Test : Hello *///:~
5
//: Main.java/** * Constructor Reload * /Class Dog {dog () {System.out.println ("wang! wang! wang! "); } Dog (String s) {System.out.println ("barking!"); } Dog (inti) {System.out.println ("howling!"); }}class Main { Public Static void Main(string[] args) {Dog D1 =NewDog (); Dog D2 =NewDog ("Hello"); Dog D3 =NewDog (1); }}/** * output:wang! wang! wang! barking! howling! *///:~
6
//: Main.java/** * Constructor Overloading-parameter order is different */Class Dog {dog () {System.out.println ("wang! wang! wang! "); } Dog (String s,inti) {System.out.println ("barking!"); } Dog (intI, String s) {System.out.println ("howling!"); }}class Main { Public Static void Main(string[] args) {Dog D1 =NewDog (); Dog D2 =NewDog ("Hello",1); Dog D3 =NewDog (1,"Hello"); }}/** * output:wang! wang! wang! barking! howling! *///:~
7
//: Main.java/** * 默认构造器 */class Test {    "Hello World";}class Main {    publicstaticvoidmain(String[] args) {        new Test();        System.out.println(t.s);    }}/** * Output: Hello World *///:~

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

Java-thinking in Java Chapter 5th 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.