关于java的学习记录:
1.
/**
* Object是所有类的父类,利用Object类可以接收任意类型的对象
* 如果在不确定参数类型的时候,使用Object是最好的
* Object类可以接收一切引用数据类型
*/
public class Demo1 {
public static void main(String[] args) {
Object obj=new int[] {1,2,3};
System.out.println(obj);//[I@70dea4e表示数组
if(obj instanceof int[]) {
int data[]=(int[])obj;
for(int x=0;x<data.length;x++) {
System.out.println(data[x]);
}
}
}
}
class Book{
private String title;
private double price;
public Book(String title,double price) {
this.title=title;
this.price=price;
}
public void setTitle(String title) {
this.title=title;
}
public String getTitle() {
return this.title;
}
public void setPrice(double price) {
this.price=price;
}
public double getPrice() {
return this.price;
}
public String toString() {
return "书名:"+this.title+"价格:"+this.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==book.title && this.price==book.price) {
return true;
}
return false;
}
}
2.
/**
* 实现一个宠物商店的模型,一个宠物商店可以保存多个宠物的信息(名字、年龄)
* 可以实现宠物的上架、下架、模糊查询
* 1.定义出宠物的标准
* 宠物商店和具体的宠物没有任何的关系,只和接口有关
* 2.定义宠物商店
* 3.根据宠物标准定义各个子类
*/
interface Pet{//定义一个宠物的标准
public String getName();//得到宠物的名字
public int getAge();//得到宠物的名字
}
class PetShop{//一个宠物商店要保存有多个宠物信息
private List<Pet> pets=new ArrayList<>();
public void add(Pet pet) {//上架
this.pets.add(pet);//向集合里保存信息
}
public void delete(Pet pet) {//下架
this.pets.remove(pet);
}
//模糊查询一定是返回多个内容
public List<Pet> search(String keyWord) {
List<Pet> result=new ArrayList<>();//保存结果
Object[] obj=this.pets.toArray();
for(int i=0;i<obj.length;i++) {
Pet p=(Pet)obj[i];
if(p.getName().contains(keyWord)) {
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 String toString() {
return "猫的名字:"+this.name+",年龄:"+this.age;
}
public boolean equals(Object obj) {
if(this==obj) {
return true;
}
if(obj==null) {
return false;
}
if(!(obj instanceof Cat)) {
return false;
}
Cat cat=(Cat)obj;
if(this.name.equals(cat.name) && this.age==cat.age) {
return true;
}
return false;
}
}
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 String toString() {
return "狗的名字:"+this.name+",年龄:"+this.age;
}
public boolean equals(Object obj) {
if(this==obj) {
return true;
}
if(obj==null) {
return false;
}
if(!(obj instanceof Dog)) {
return false;
}
Dog dog=(Dog)obj;
if(this.name.equals(dog.name) && this.age==dog.age) {
return true;
}
return false;
}
}
public class Demo2 {
public static void main(String[] args) {
PetShop shop=new PetShop();
shop.add(new Cat("kaka",20));
shop.add(new Cat("fafa",10));
shop.add(new Cat("caca",9));
shop.add(new Dog("dada",9));
shop.add(new Dog("caxa",9));
shop.add(new Dog("daza",5));
shop.delete(new Dog("dada",9));
List<Pet> all=shop.search("da");
Object[] obj=all.toArray();
for(int i=0;i<obj.length;i++) {
System.out.println(obj[i]);
}
}
}