Basic Java Issues (2)

Source: Internet
Author: User

1. Constructors

/** * Test--Constructor */public class Test {public static void main (string[] args) {//member variable initialization order                A. The default constructor initializes the person P1 = new person ();                System.out.println (P1.getname () + ":" + p1.getage ());                B. Member variables display initialize person P2 = new Person ("CC");                System.out.println (P2.getname () + ":" + p2.getage ());                Person P3 = new person (10);                System.out.println (P3.getname () + ":" + p3.getage ());                C. Initialize the person P4 = new Person ("CC", 10) through the constructor to the member variable;                System.out.println (P4.getname () + ":" + p4.getage ());                D. Initializing member variables by the Set method person P5 = new person ();                P5.setname ("DD");                P5.setage (22);        System.out.println (P5.getname () + ":" + p5.getage ());        }};class person {private String name = "AA";        private int age = 1; constructor person (variable) action//1, compositionUsed to create the object person () {System.out.println ("null parameter constructor");                }//2, constructor for member variable initialization//3, the constructor can overload person (String N, int a) {this.name = n;                This.age = A;        System.out.println ("with parametric constructor");                } person (String N) {this.name = n;        System.out.println ("name constructor");                } person (int a) {this.age = A;        System.out.println ("Age constructor");        }/** * @return the name */public String GetName () {return name;        }; /** * @param name the name to set */public void SetName (String name) {this.name =        Name        };        /** * @return The Age */public int getage () {return age;        };        /** * @param age-The age-to set */public void Setage (int.) {this.age = age; };}; 

Basic Java issues (2)

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.