This call between constructor methods

Person class:

/** this can be called directly in the constructor * / public class person {private string name; private int age; public person() {/ / name = "Li Si"; / / age = 20; this ("Li Si", 20); / / this statement must be written in the first line of the constructor} public person (string name, int age) {this. Name = name; this. Age = age;} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }

}

Test class:

Public class test {public static void main (string [] args) {/ / create an object of person and call the constructor of empty parameter person P = new person(); system.out.println (p.getname()); system.out.println (p.getage());}}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>