Half_Life
2016-04-08 09:49
package com.imooc;
public class Animal {
public String name;
public int age;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Animal other = (Animal) obj;
if (age != other.age)
return false;
return true;
}
}package com.imooc;
public class Dog extends Animal {
}package com.imooc;
public class Cat extends Animal {
}package com.imooc;
public class Initial {
public static void main(String[] args) {
Dog dog = new Dog();
Dog dog2=new Dog();
Cat cat = new Cat();
dog.age=1;
dog2.age=1;
cat.age=1;
if(dog.equals(dog2)){
System.out.println("dog和dog2的age相同");
}else{
System.out.println("dog和dog2的age不同");
}
if(dog.equals(cat)){
System.out.println("dog和cat的age相同");
}else{
System.out.println("dog和cat的age不同");
}
}
}结果为dog和dog2的age相同,dog和cat的age不同
为什么new同一个子类时,equals可以比较属性值,而new不同子类时,不能比较属性值,怎么修改才能比较不同子类的属性值
谢谢
把 这个代码换成下面那个
Cat cat = new Cat();
换成这个以后 就是相同的了
Dog cat = new Dog();
至于为啥比较出来不相同 是因为 它们的类型就是不一样的 所以还没有比到值 直接在类型这里就返回 flase了
所以不同,修改成相同的类型以后就能比较了
还有想比较两个类的数据是否相同 可以直接使用 == 来进行对比
if (dog.age == dog2.age){
System.out.println("dog和dog2的age相同");
}else{
System.out.println("dog和dog2的age不同");
}
if (dog.age == cat.age=1){
System.out.println("dog和cat的age相同");
}else{
System.out.println("dog和cat的age不同");
}
这样就可以对比了=-= 简单粗暴~望采纳
都是初学者,好吧,哭
天呐,能不能用简单一点的代码来写啊。这本身不是很复杂的问题,但是你的代码看得我眼花了。
是的 目前我们所学的知识就是后面可以这么去理解 等到后面慢慢累积了更多知识了 就会理解的更加透彻,现在先理解大概是个啥意思有一个认识 就好了 不能说完全懂了 也不能完全都不懂 ,我现在和你所说的这些也算是我个人学到现在的一些理解,有的地方也不是百分之百正确,基本上大概这么个意思 所以有的东西还是需要自己多多去琢磨 自己琢磨透彻了 在听别人稍微讲解一下 是很容易理解 和加深记忆的~
new 同一个类 用==比较的是 它们的类型 或者说是内存地址 这个东西它们是使用的一样的
不同类的时候 == 就无法比较类了,因为 类型不同 所在的 空间地址也不一样 内存地址也是不同的
但是 equals() 在String 字符串中 比较的时候 他比较的是两个 变量的 内容是不是一样的 并不是去比较类型
和内存地址 所以不同的类 就是 需要使用equals()来比较
equals() 重写 只是拿来比较对象的 来对比这个两个 对象中间的类型 是否一致 它的属性是否一致
name 测试过调试输出 name[i] 有数值吗? =-= 正常赋值了以后是可以判断和对比的
但是不要用等于号了 直接用 equals() 比较
猫.equals("猫") 这样就是判断 猫这个字符串是否和 猫这个字符串 相等
package com.imooc;
public class Animal{
String name;
int no;
}package com.imooc;
public class Dog extends Animal {
public Dog(){
this.name="狗";
this.no=1;
}
}package com.imooc;
public class Cat extends Animal {
public Cat(){
this.name="猫";
this.no=2;
}
}package com.imooc;
import java.util.Scanner;
public class Initial {
static Animal[] animal={new Dog(),new Cat()};
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请选择方式:\n1.按品种选择\n2.按序号选择");
int choose=input.nextInt();
if(choose==1){
searchByName(animal);
}else if(choose==2){
searchByAge(animal);
}
input.close();
}
static void searchByName(Animal[] animal){
Scanner input=new Scanner(System.in);
System.out.println("全部动物如下:\n序号\t品种");
for(int i=0;i<animal.length;i++){
System.out.println(animal[i].no+"\t"+animal[i].name);
}
System.out.print("请输入动物名:");
String choose1=input.next();
for(int i=0;i<animal.length;i++){
System.out.println(animal[i].name+choose1);
if(animal[i].name==choose1){
System.out.println("选择"+choose1+"成功");
exitOrContinue(1);
}else if(i==animal.length-1&&choose1!=animal[i].name){
System.out.println("选择"+choose1+"失败");
exitOrContinue(1);
}
}
input.close();
}
static void searchByAge(Animal[] animal){
Scanner input=new Scanner(System.in);
System.out.println("全部动物如下:\n序号\t品种");
for(int i=0;i<animal.length;i++){
System.out.println(animal[i].no+"\t"+animal[i].name);
}
System.out.println("\n请输入序号");
int choose2=input.nextInt();
if(choose2>0&&choose2<=animal.length){
if(choose2==animal[(choose2-1)].no){
System.out.println("选择"+choose2+"成功");
exitOrContinue(2);
}else{
System.out.println("选择"+choose2+"失败");
exitOrContinue(2);
}
}
input.close();
}
static void exitOrContinue(int eoc){
Scanner input=new Scanner(System.in);
System.out.println("是否继续:\n1.继续选择\n2.退出");
int choose3=input.nextInt();
if(choose3==1){
if(eoc==1){
searchByName(animal);
}else if(eoc==2){
searchByAge(animal);
}
}else if(choose3==2){
System.out.println("欢迎下次再来!");
}
input.close();
}
}no正常判断,name就无法正常判断了,即使是有值的
要想nuw出来的东西直接带有值
在子类里面 构造一个方法 在这个里面赋值
public class Cat extends Animal{
public Cat(){
name ="狗";
age = 2;
}
}
这样nuw出来的时候就直接有值了!
animal[0].name = "狗";
System.out.println(animal[0].name);
System.out.println(animal[1].name);
在nuw了以后 打印这句话 你会发现你对象里面的 name是空的 木有值 所以找不到
你给它赋予一个值以后 就可以去对比了 还有 字符串 是用 equals 去比较呢
基本数据类型 比如 整形 布尔 浮点型 就用 == 去比较
package com.imooc;
public class Animal {
public String name;
public int age;
}package com.imooc;
public class Dog extends Animal {
String name="狗";
int age=1;
}package com.imooc;
public class Cat extends Animal {
String name="狗";
int age=2;
}package com.imooc;
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入动物名");
String choose1=input.next();
Animal[] animal={new Dog(),new Cat()};
for(int i=0;i<animal.length;i++){
if(choose1==animal[i].name){
System.out.println("已找到"+choose1);
}else if(i==animal.length-1){
System.out.println("未找到"+choose1);
}
}
System.out.println("\n请输入1或2");
int choose2=input.nextInt();
for(int n=0;n<animal.length;n++){
if(choose2==animal[n].age){
System.out.println("已找到"+choose2);
}else if(n==animal.length-1){
System.out.println("未找到"+choose2);
}
}
input.close();
}
}Java入门第二季
531287 学习 · 6327 问题
相似问题