class Book extends Object {}
public class MainClass {
public static void main(String[] args) {
Book b = new Book();
String s = "Hello";
System.out.println(b);
System.out.println(b.toString());
System.out.println(s);
}
}
class Book {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
public String toString() {
return "title=" + this.title + " price=" + this.price;
}
}
public class MainClass {
public static void main(String[] args) {
Book b = new Book("java", 11.1);
System.out.println(b);
}
}
class Book {
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Book))
return false;
Book book = (Book) obj;
if (!this.title.equals(book.title))
return false;
if (this.price != book.price)
return false;
return true;
}
public String toString() {
return "title=" + this.title + " price=" + this.price;
}
}
public class MainClass {
public static void main(String[] args) {
Book b1 = new Book("java", 11.1);
Book b2 = new Book("java", 11.1);
System.out.println(b1.equals(b2));
}
}
public class MainClass {
public static void main(String[] args) {
Object obj = new int[] {1, 2, 3};
System.out.println(obj); // [I@7852e922
if (obj instanceof int[]) {
int data[] = (int[]) obj;
for (int i = 0; i < data.length; i ++) {
System.out.println(data[i]);
}
}
}
}
interface A {
public void fun();
}
class B implements A {
public void fun() {
System.out.println("Hello World !");
}
}
public class MainClass {
public static void main(String[] args) {
Object obj = new B();
A a = (A) obj;
a.fun();
}
}
public class MainClass {
public static void main(String[] args) {
Link all = new Link();
all.add("A");
all.add("B");
all.add("C");
all.remove("A");
Object[] data = all.toArray();
for (int i = 0; i < data.length; i ++) {
System.out.println(data[i]);
}
}
}
interface Pet { // 定义一个宠物的标准
public String getName(); // 得到宠物的名字
public int getAge(); // 得到宠物的年龄
}
class PetShop { // 一个宠物商店要保存有多个宠物信息
private Link pets = new Link(); // 保存的宠物信息
public void add(Pet pet) { // 上架
this.pets.add(pet);
}
public void delete(Pet pet) { // 下架
this.pets.remove(pet);
}
public Link search(String keyWork) {
Link result = new Link();
Object[] obj = this.pets.toArray();
for (int i = 0; i < obj.length; i ++) {
Pet p = (Pet) obj[i];
if (p.getName().contains(keyWork)) {
result.add(p);
}
}
return result;
}
}
class Cat implements Pet { // 如果不实现接口无法保存宠物信息
private String name;
private int age;
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Cat))
return false;
Cat other = (Cat) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String toString() {
return "Cat [name=" + name + ", age=" + age + "]";
}
}
class Dog implements Pet { // 如果不实现接口无法保存宠物信息
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Cat))
return false;
Dog other = (Dog) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String toString() {
return "Dog [name=" + name + ", age=" + age + "]";
}
}
public class MainClass {
public static void main(String[] args) {
PetShop shop = new PetShop();
shop.add(new Cat("小黑", 20));
shop.add(new Cat("皇受", 18));
shop.add(new Cat("吾皇", 14));
shop.add(new Dog("二哈", 6));
shop.add(new Dog("泰迪", 16));
shop.add(new Dog("阿拉斯加", 18));
shop.delete(new Dog("泰迪", 16));
Link all = shop.search("皇");
Object[] objs = all.toArray();
for (int i = 0; i < objs.length; i ++) {
System.out.println(objs[i]);
}
}
}