JAVA | 12 - Object 类

package com.company;
class Book {
    private String name;
    private int price;

    public Book(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String toString() {
        return this.name + " " + this.price;
    }

    public boolean equals(Object object) {
        if (object instanceof Book) {
            if (this == object) {
                return true;
            } else if (object == null) {
                return false;
            } else if (this.name.equals(((Book) object).name) && this.price == ((Book) object).price) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Book bookA = new Book("JAVA",100);
        Book bookB = new Book("Python",100);
        System.out.println(bookA);
        System.out.println(bookB);
        System.out.println(bookA.equals(bookB));
    }
}

package com.company;
class Link {
    //创建内部节点类
    private class Node {
        private Object data;
        private Node next;
        //节点类构造
        public Node(Object data) {
            this.data = data;
        }
        //节点类关系传递
        public void relation(Node newNode) {
            if (this.next == null) {
                this.next = newNode;
            } else {
                this.next.relation(newNode);
            }
        }
        //节点查询
        public boolean contains(Object data) {
            if (data.equals(this.data)) {
                return true;
            } else {
                if (this.next == null) {
                    return false;
                } else {
                    return this.next.contains(data);
                }
            }
        }
        //索引查询节点数据
        public Object getNode(int index){
            if(Link.this.foot == index){
                return this.data;
            }
            else{
                foot ++;
                return this.next.getNode(index);
            }
        }
        //修改索引上的数据
        public void setNode(int index, Object data){
            if(Link.this.foot == index){
                this.data = data;
            }
            else{
                foot ++;
                this.next.setNode(index,data);
            }
        }
        //删除数据
        public void removeNode(Node previous, Object data){
            if(data.equals(this.data)){
                previous.next = this.next;
            }
            else {
                this.next.removeNode(this,data);
            }
        }
        //插入数组数据
        public void toArrayNode(){
            Link.this.retArray[Link.this.foot ++] = this.data;
            if(this.next != null){
                this.next.toArrayNode();
            }
        }
    }
    //链表类属性
    private Node root;
    private int count = 0;
    private int foot;
    private Object [] retArray;
    //获取链表长度
    public int getCount() {
        return this.count;
    }
    //判断链表是否为空
    public boolean isEmpty() {
        if (this.count == 0) {
            return true;
        } else {
            return false;
        }
    }
    //链表类添加节点
    public void add(Object data) {
        if (data == null) {
            return;
        }
        //实例化节点类
        Node newNode = new Node(data);
        //设置头结点
        if (this.root == null) {
            this.root = newNode;
        } else {
            this.root.relation(newNode); //交给Node类解决引用关系
        }
        //节点数目累加
        this.count++;
    }
    //节点查询
    public boolean contains(Object data) {
        if (this.root == null || data == null) {
            return false;
        }
        return this.root.contains(data); //交给Node类数据查询
    }
    //索引取得数据
    public Object get(int index){
        if(index > this.count){
            return null;
        }
        this.foot = 0;
        return this.root.getNode(index); //交给Node类取得数据
    }
    //修改索引上的数据
    public void set(int index, Object data){
        if(index > this.count){
            return;
        }
        this.foot = 0;
        this.root.setNode(index, data); //交给Node类修改数据
    }
    //删除数据
    public void remove(Object data){
        if(Link.this.contains(data)){
            if(data.equals(this.root.data)){ //待删除数据为头结点时
                this.root = this.root.next; //删除头结点数据
            }
            else {
                this.root.next.removeNode(this.root,data); //待删除数据不为头结点时,交给Node类删除数据
            }
            this.count --;
        }
    }
    //返回数组
    public Object[] getRetArray() {
        if(this.root == null){
            return null;
        }
        this.foot = 0; //数组索引控制
        this.retArray = new Object[this.count];
        this.root.toArrayNode(); //交给Node类插入数组数据
        return retArray;
    }
}
interface Animals{
    public String getName();
    public int getAge();
}
class Zoo{
    private Link animals = new Link();

    public Link getAnimals(){
        return this.animals;
    }
    public void add(Animals animals){
        this.animals.add(animals);
    }
    public void remove(Animals animals){
        this.animals.remove(animals);
    }
    public Link search(String keyWord){
        Link result = new Link();
        Object object [] = this.animals.getRetArray();
        for(int i = 0; i < object.length; i ++){
            Animals animals = (Animals)object[i];
            if(animals.getName().contains(keyWord)){
                result.add(animals);
            }
        }
        return result;
    }
}
class Tiger implements Animals{
    private String name;
    private int age;
    public Tiger(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 "name:" + " " + this.name + " " + "age:" + " " + this.age;
    }
    public boolean equals(Object object){
        Tiger tiger = (Tiger) object;
        if(this == object){
            return true;
        }
        else if(object == null){
            return false;
        }
        else if(!(object instanceof Tiger)){
            return false;
        }
        else if(this.name.equals(tiger.name) && this.age == tiger.age){
            return true;
        }
        return true;
    }
}
class Panda implements Animals {
    private String name;
    private int age;
    public Panda(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 "name:" + " " + this.name + " " + "age:" + " " + this.age;
    }
    public boolean equals(Object object) {
        Panda panda = (Panda) object;
        if (this == object) {
            return true;
        } else if (object == null) {
            return false;
        } else if (!(object instanceof Panda)) {
            return false;
        } else if (this.name.equals(panda.name) && this.age == panda.age) {
            return true;
        }
        return true;
    }
}
public class Main {
    public static void main(String[] args) {
        Zoo zoo = new Zoo();
        zoo.add(new Panda("Beibei",4));
        zoo.add(new Panda("JingJing",5));
        zoo.add(new Panda("HuanHuan",3));
        zoo.add(new Panda("YingYing",6));
        zoo.add(new Panda("NINi",3));
        zoo.add(new Tiger("DaDa",4));
        zoo.add(new Tiger("SISi",8));
        zoo.add(new Tiger("MaMa",2));
        zoo.add(new Tiger("PiPi",5));
        Link link = zoo.search("i");
        Object object [] = link.getRetArray();
        for(int i = 0; i < object.length; i ++){
            System.out.println(object[i]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值