(7-7)阅读程序,写出程序运行结果。
//写出程序运行结果
class Eye {// 猫的眼睛类
private String color;
public Eye(String color) {
this.color = color;
}
}
class Cat {// 猫类
private String name;
private Eye eye;
public Cat(String name, Eye eye) {
this.name = name;
this.eye = eye;
}
public boolean equals(Object obj) {
Cat cat = (Cat) obj;
if (this.name.equals(cat.name) && this.eye == cat.eye)
return true;
return false;
}
}
public class CatDemo {
public static void main(String[] args) {
Eye e1=new Eye("蓝色");
Cat tom1=new Cat("Tom",e1);
Cat tom2=new Cat("Tom",e1);
System.out.println(tom1==tom2);
System.out.println(tom1.equals(tom2));
}
}
//写出程序运行结果
class Eye {// 猫的眼睛类
private String color;
public Eye(String color) {
this.color = color;
}
}
class Cat {// 猫类
private String name;
private Eye eye;
public Cat(String name, Eye eye) {
this.name = name;
this.eye = eye;
}
public boolean equals(Object obj) {
Cat cat = (Cat) obj;
if (this.name.equals(cat.name) && this.eye == cat.eye)
return true;
return false;
}
}
public class CatDemo {
public static void main(String[] args) {
Eye e1=new Eye("蓝色");
Cat tom1=new Cat("Tom",e1);
Cat tom2=new Cat("Tom",e1);
System.out.println(tom1==tom2);
System.out.println(tom1.equals(tom2));
}
}
举一反三
- 给出下列【代码】注释标注的代码的输出结果。class Tom {int weight = 10;void Tom(){weight = 18;}}public class E {public static void main(String args[]) {Tom cat = new Tom();System.out.println(cat.weight); //【代码】}}
- 请阅读下面的程序,写出运行结果,如果编译失败,写明失败原因。 abstract class Animal{ public final abstract void eat(); } class Cat extends Animal{ public void eat(){ System.out.println("cat...fish"); } } class CatDemo{ public static void main(String[] args){ Animal a = new Cat(); a.eat(); } }
- 下列程序的运行结果是( )class Demo{private String name;Demo(String name){this.name = name;}private static void show(){System.out.println(name)}public static void main(String[] args){Demo d = new Demo(“lisa”);d.show();}}
- 有如下类的定义,创建Employee对象正确的是() public class Employee{ private int age; private String name; public void Employee(){ } public Employee(int age){ this.age = age; } public Employee(String name){ this.name = name; } } A: Employee e = new Employee( ) B: Employee e = new Employee(10); C: Employee e = new Employee(tom); D: Employee e = new Employee(10,"tom");
- (6-3)阅读程序,写出程序运行结果。 class Book{ private static int counter=0; private int id=1; private String name; public Book(String name) { this.name = name; counter++; this.id=this.id+8; } public static int getCounter() { return counter; } public int getID() { return this.id; } } public class BookDemo{ public static void main(String[] args) { Book b1=new Book("红楼梦"); Book b2=new Book("西游记"); Book b3=new Book("儒林外史"); System.out.println(b3.getCounter()*Book.getCounter()*b3.getID()); } }