Object类及应用概述

本文介绍了Java中的Object类,包括如何创建Object类型的实例以及其toString方法的作用。讲解了如何在实践中使用Object类,并强调了toString方法在自定义类中的重写,以提供更具体的字符串表示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Object类创建

一:创建object类型实例
public Object(){

}

1.接口

package com.ll;

public interface BaseInterface {
    Object superMethod(Object obj);
}

2.实践类1

package com.ll;

public class BaseInterfaceImp implements BaseInterface {

    @Override
    public Object superMethod(Object obj) {
        int []ints = (int[])obj;
        return ints.length>1?ints[0]+ints[1]:0;
    }
}

3.实践类2

package com.ll;

public class BaseInterfaceImpA implements BaseInterface{
    @Override
    public Object superMethod(Object obj) {
        String str = (String) obj;
        return str.length()>=6 && str.length()<=16?true:false;
    }
}

4.测试类

package com.test;

import com.ll.BaseInterface;
import com.ll.BaseInterfaceImp;

public class Test {
    public static void main(String[] args) {
        BaseInterface baseInterface1 = new BaseInterfaceImp();
        BaseInterface baseInterface2 = new BaseInterfaceImp();
        int []ints = {34,25};
        int sum = (Integer)(baseInterface1.superMethod(ints));
        System.out.println(sum);
    }
}
二:创建object类型实例

Java任何引用类型对象都可以赋值给object类型变量

toString方法返回Object对象的字符串表示形式(此方法可以在任何类中重写,重写后将得到一个新的字符串返回形式)

手写toString方法

package com.entity;

public class Users {
    private String id;
    private String name;
    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public Users(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Users() {

    }

    @Override
    public String toString() {
        String begin = "{";
        String end = "}";
        begin+="\"";
        begin+="id";
        begin+="\"";
        begin+=":";
        begin+=this.getId();
        begin+=",";


        begin+="\"";
        begin+="name";
        begin+="\"";
        begin+=":";
        begin+=this.getName();
        begin+=",";


        begin+="\"";
        begin+="age";
        begin+="\"";
        begin+=":";
        begin+=this.getAge();
        begin+=end;


        return begin;
    }
}
package com.test;

import com.entity.Users;

public class Test2 {
    public static void main(String[] args) {
        Users users = new Users("No12345","喜羊羊",6);
        System.out.println(users.toString());
    }

}

也可以直接调用:

package com.entity;

public class Users {
    private String id;
    private String name;
    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public Users(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Users() {

    }

    @Override
    public String toString() {
        return "Users{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值