jmu-Java-03面向对象基础-05-覆盖

jmu-Java-03面向对象基础-05-覆盖

Java每个对象都继承自Object,都有equals、toString等方法。
现在需要定义PersonOverride类并覆盖其toString与equals方法。

  1. 新建PersonOverride类

a. 属性:String name、int age、boolean gender,所有的变量必须为私有(private)。

b. 有参构造方法,参数为name, age, gender

c. 无参构造方法,使用this(name, age,gender)调用有参构造函数。参数值分别为"default",1,true

d.toString()方法返回格式为:name-age-gender

e. equals方法需比较name、age、gender,这三者内容都相同,才返回true.

  1. main方法
    2.1 输入n1,使用无参构造函数创建n1个对象,放入数组persons1。
    2.2 输入n2,然后指定name age gender。每创建一个对象都使用equals方法比较该对象是否已经在数组中存在,如果不存在,才将该对象放入数组persons2。
    2.3 输出persons1数组中的所有对象
    2.4 输出persons2数组中的所有对象
    2.5 输出persons2中实际包含的对象的数量
    2.5 使用System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));输出PersonOverride的所有构造函数。

提示:使用ArrayList代替数组大幅复简化代码,请尝试重构你的代码。

输入样例:
1
3
zhang 10 true
zhang 10 true
zhang 10 false

输出样例:
default-1-true
zhang-10-true
zhang-10-false
2
[public PersonOverride(), public PersonOverride(java.lang.String,int,boolean)]

代码实现1

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner se=new Scanner (System.in);
        int n1=se.nextInt();
        PersonOverride persons1[] =new PersonOverride[n1];
        for(int i=0;i<n1;i++) {
        	persons1[i]=new PersonOverride();
        	System.out.println(persons1[i].toString());
        }
        int n2=se.nextInt();
        ArrayList<PersonOverride> persons2=new ArrayList<PersonOverride>();
        for(int i=0;i<n2;i++) {
        	PersonOverride a=new PersonOverride(se.next(),se.nextInt(),se.nextBoolean());
        	if(!persons2.contains(a)) {
        		persons2.add(a);
        		System.out.println(a.toString());
        	}
        }
        System.out.println(persons2.size());
        System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
    }
}

class PersonOverride {
	private String name;
	private int age;
	private boolean gender;
  public PersonOverride() {
		this.name = "default";
		this.age = 1;
		this.gender = true;
	}
	public PersonOverride(String name, int age, boolean gender) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
	}
	
	@Override
	public String toString() {
		return  name + "-" + age + "-" + gender;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + (gender ? 1231 : 1237);
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PersonOverride other = (PersonOverride) obj;
		if (age != other.age)
			return false;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	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;
	}
}


代码实现2

import java.util.*;
 
 
class PersonOverride{
	private String name;
	private int age;
	private boolean gender;
    public PersonOverride() {
		this("default", 1, true);
	}
	public PersonOverride(String name,int age,boolean gender) {
		this.name = name;
		this.age = age;
		this.gender = gender;
    }
	@Override
	public String toString() {
		return this.name+"-"+this.age+"-"+this.gender;
	}
	@Override
	public boolean equals(Object o) {
		if (this == o){
            return true;
        }
        if(o == null)
        {
            return false;
        }
        if (this.getClass() != o.getClass()){
            return false;
        }
		PersonOverride p = (PersonOverride) o;
		return Objects.equals((this.name), p.name) && this.gender == p.gender && this.age==p.age;
	}
}
public class Main{
    public static void main(String [] args){
   	Scanner in=new Scanner(System.in);
    	int n1 = in.nextInt();
    	in.nextLine();
    	PersonOverride[] persons1 = new PersonOverride[n1];
    	for(int i=0;i<n1;i++) {
    		persons1[i] = new PersonOverride();
    	}
    	int n2 = in.nextInt();
    	in.nextLine();
    	PersonOverride[] persons2 = new PersonOverride[n2];
    	int len2=0;
    	for(int i=0;i<n2;i++) {
    		String name = in.next();
    		int age = in.nextInt();
    		boolean gender = in.nextBoolean();
    		in.nextLine();
    		PersonOverride p = new PersonOverride(name,age,gender);
    		boolean flag = true;
    		for(int j=0;j<len2;j++) {
    			if(persons2[j].equals(p)) {
    				flag = false;
    				break;
    			}
    		}
    		if(flag) {
    			persons2[len2] = p;
    			len2++;
    		}
    	}
    	for(PersonOverride p:persons1) {
    		System.out.println(p);
    	}
    	for(int i=0;i<len2;i++) {
    		System.out.println(persons2[i]);
    	}
    	System.out.println(len2);
    	System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
    }
}

你可能感兴趣的:(JAVA学习,java学习过程,java,开发语言,后端,蓝桥杯,算法)