PTA Java程序设计


7-1 jmu-Java-03面向对象基础-01-构造方法与toString


懒人一键copy

定义一个有关人的Person类,内含属性:

String name、int age、boolean gender、int id
所有的变量必须为私有(private)。
注意:属性顺序请严格按照上述顺序依次出现。

分析:

  • 定义一个Person的类,并在类中包含String nameint ageboolean genderint id,并且定义为私有属性(private
private String name;private int age;private boolean gender;private int id;

1.编写无参构造函数:

  打印”This is constructor”。
 将name,age,gender,id按照name,age,gender,id格式输出

2.编写有参构造函数

  依次对name,age,gender赋值。

分析:

  • 在IDEA中使用快捷键Alt+Insert快捷生成有参构造器,在Person类中创建无参构造器,并输出格式
public Person() {System.out.println("This is constructor");System.out.println(name+","+age+","+gender+","+id);} public Person(String name, int age, boolean gender) {this.name = name;this.age = age;this.gender = gender;}

3.覆盖toString函数:

按照格式:类名 [name=, age=, gender=, id=]输出。建议使用Eclipse自动生成.

分析:

  • 按照输出格式为Person [name=a, age=11, gender=false, id=0]
  • 在IDEA中使用快捷键Alt+Insert快捷生成toString方法,并编写输出格式
  • ps:快捷生成的话输出会name='a'多一对单引号,需要在toString方法中删除
 @Overridepublic String toString() {return "Person [" + "name=" + name +", age=" + age +", gender=" + gender +",token operator">+ id +']';}

4.对每个属性生成setter/getter方法

分析:

  • 这里就使用IDEA的快捷生成来生成set/get方法
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;}public boolean isGender() {return gender;}public void setGender(boolean gender) {this.gender = gender;}public int getId() {return id;}public void setId(int id) {this.id = id;}

5.main方法中首先从屏幕读取n,代表要创建的对象个数。

然后输入n行name age gender , 调用上面2编写的有参构造函数新建对象。
然后将刚才创建的所有对象逆序输出
接下来使用无参构造函数新建一个Person对象,并直接打印该对象。

分析:

  • 在main方法中使用Scanner输入n,并且数组的长度为n (代表有几个对象存入)
  • 遍历数组,在遍历的过程中创建对象,并将创建的对象逆序输出 (使用for循环倒序输出)
  • 创建一个Person对象,直接打印该对象的toString()方法
Scanner sc = new Scanner(System.in);int nextInt = sc.nextInt();Person[] s = new Person[nextInt];for (int i = 0; i < nextInt; i++) {String a = sc.next();int b = sc.nextInt();boolean c=sc.nextBoolean();s[i] = new Person(a,b,c);//将对象赋值给数组s的索引为0}//i=3//但是索引只有到2,故-1for (int i = nextInt-1; i >=0; i--) {System.out.println(s[i].toString());//打印出数组s的索引为0的toString方法}Person person = new Person();System.out.println(person.toString());

懒人一键copy

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int nextInt = sc.nextInt();Person[] s = new Person[nextInt];for (int i = 0; i < nextInt; i++) {String a = sc.next();int b = sc.nextInt();boolean c=sc.nextBoolean();s[i] = new Person(a,b,c);}for (int i = nextInt-1; i >=0; i--) {System.out.println(s[i].toString());}Person person = new Person();System.out.println(person.toString());}}class Person{private String name;private int age;private boolean gender;private int id;@Overridepublic String toString() {return "Person [" + "name=" + name +", age=" + age +", gender=" + gender +",token operator">+ id +']';}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;}public boolean isGender() {return gender;}public void setGender(boolean gender) {this.gender = gender;}public int getId() {return id;}public void setId(int id) {this.id = id;}public Person() {System.out.println("This is constructor");System.out.println(name+","+age+","+gender+","+id);}public Person(String name, int age, boolean gender) {this.name = name;this.age = age;this.gender = gender;}}