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)