Java每个对象都继承自Object,都有equals、toString等方法。
现在需要定义PersonOverride类并覆盖其toString与equals方法。
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.
提示:使用ArrayList代替数组大幅复简化代码,请尝试重构你的代码。
输出样例:
default-1-true
zhang-10-true
zhang-10-false
2
[public PersonOverride(), public PersonOverride(java.lang.String,int,boolean)]
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;
}
}
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()));
}
}
个人技术集锦还为您提供以下相关内容希望对您有帮助:
7-1 jmu-Java-03面向对象基础-04-形状-继承(15 分)
自己写的,也才学不容易还望采纳 import java.util.*;abstract class Shape{ final static double PI=3.14;public abstract double getPerimeter();public abstract double getArea();public static double sumAllArea(Shape []shapes) { double x=0;for(Shape e:shapes) { x+=e.getArea();} re...
想学编程不知道从哪里开始??
链接: https://pan.baidu.com/s/1BarkCHynpQkGjmumTV69yw 提取码: up8s Rust是一门系统编程语言 [1] ,专注于安全 [2] ,尤其是并发安全,支持函数式和命令式以及泛型等编程范式的多范式语言。Rust在语法上和C++类似 [3] ,但是设计者想要在保证性能的同时提供更好的内存安全。 Rust最...