java练习题之常用类Object类,包装类

2024-01-03 15:34:57

常用类

应用知识点:

Object类

  • 包装类

习题:

1:(Object 类)仔细阅读以下代码,写出程序运行的结果;并简述 == 和 equals 的区别。

?

true

false

==是判断两个变量或实例是不是指向同一个内存空间。?比较两个引用类型的地址,基本类型比值

?"equals"是判断两个变量或实例所指向的内存空间的值是不是相同。

2:(包装类)以下包装类的类名正确的是(ABDEFH)

A. Byte

B. Short

C. Int

D. Long

E. Float

F. Double

G. Char

H. Boolean

3:(包装类)下面关于数据类型之间的转换正确的是()

Integer i = 5 ;

int a = new Integer(12);

Integer i2 = 20 + “”;

String s = true+””;

4:(Object 类)仔细阅读以下代码,写出程序运行的结果。

null??0

name Tom age 18

5: (包装类)仔细阅读以下代码,写出程序运行打印输出的结果;并解释每个结果的原因

true?????itn类型比较值,常量缓存区最大数为127 ?所以不相等

false ???new对象 创建新空间

false ???new对象 创建新空间 ??==比较的是引用

6: 为什么要重写toString()方法?

该方法不重写就默认返回对象的地址,实际中我们需要返回的是对象的内容

7: 为什么要重写equals()方法?

该方法默认比较对象地址,但是在开发过程中,某些情况下需要比较双方内容,所以需要提供重写

8: 定义平面内点的类Point,属性有横坐标和纵坐标。

① 每个属性都使用private修饰,为每个属性设置setter和getter方法。

② 设置无参的构造方法

③ 设置有参的构造方法Point(int x,int y)

④ 设置有参的构造方法Point(Point p)

⑤ 重写toString()方法,用于输出坐标点的信息

⑥ 重写euqals()方法,用于对两个坐标点进行比较

⑦ 创建两个对象,判断对象坐标是否相同。

package com.by.homework1;

public class Point {
????private String x;
????private String y;

????public Point() {
????}

????public Point(String x, String y) {
????????this.x = x;
????????this.y = y;
????}
????public Point (Point p){
????}

????public String getX() {
????????return x;
????}

????public void setX(String x) {
????????this.x = x;
????}

????public String getY() {
????????return y;
????}

????public void setY(String y) {
????????this.y = y;
????}
????public String toString(){
????????return "横坐标:"+x+",纵坐标"+y;
????}
????public boolean equals(Object o){
????????if(this==o){
????????????return true;
????????}
????????if(this.getClass()!=o.getClass()||o.getClass()==null){
????????????return false;
????????}
????????Point p=(Point) o;
????????return this.x.equals(p.x)&&this.y.equals(p.y);
????}
}





package com.by.homework1;

public class PointTest {
????public static void main(String[] args) {
????????Point p1=new Point("1","2");
????????Point p2=new Point("1","2");
????????if (p1.equals(p2)){
????????????System.out.println("坐标相等");
????????}else {
????????????System.out.println("坐标不等");
????????}
????}
}

9:? 定义一个person类,要求如下:

????① 属性:身份证号,姓名,性别,年龄,户籍,出生日期(String类型,"1998-09-09")

② 属性私有化,提供get/set,构造方法

????③ 介绍格式:(toString)

???姓名+户籍+年龄

????④ 提供对象比较equals方法,只要身份证号+姓名相同就认为对象相等

????⑤ 写一个测试类,定义一个Person数组,创建多个Person对象存储在数组中,

???i. ?打印输出所有Person对象的信息

???ii. 用控制台输入一个Person信息:

?例如:"370828199902145066"-"刘航"-'男'-58-北京-"1955/01/01"

???????判断数组中是否存在和此对象相等的对象(身份证号+姓名相同就认为对象相等)

package com.by.homework1;

public class Person {
????private String idCard;
????private String name;
????private String sex;
????private int age;
????private String location;
????private String birthday;

????public Person() {
????}

????public Person(String idCard, String name, String sex, int age, String location, String birthday) {
????????this.idCard = idCard;
????????this.name = name;
????????this.sex = sex;
????????this.age = age;
????????this.location = location;
????????this.birthday = birthday;
????}

????public String getIdCard() {
????????return idCard;
????}

????public void setIdCard(String idCard) {
????????this.idCard = idCard;
????}

????public String getName() {
????????return name;
????}

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

????public String getSex() {
????????return sex;
????}

????public void setSex(String sex) {
????????this.sex = sex;
????}

????public int getAge() {
????????return age;
????}

????public void setAge(int age) {
????????this.age = age;
????}

????public String getLocation() {
????????return location;
????}

????public void setLocation(String location) {
????????this.location = location;
????}

????public String getBirthday() {
????????return birthday;
????}

????public void setBirthday(String birthday) {
????????this.birthday = birthday;
????}
????public String toString(){
????????return "姓名:"+name+",户籍:"+location+",年龄:"+age;
????}
????public boolean equals(Object o){
????????if(this==o){
????????????return true;
????????}
????????if (this.getClass()!=o.getClass()||o.getClass()==null){
????????????return false;
????????}
????????Person person=(Person) o;
????????return this.idCard.equals(person.idCard)&&this.name.equals(person.name);
????}
}







package com.by.homework1;

import java.util.Scanner;

public class PersonTest {
????public static void main(String[] args) {
????????Person []people={new Person("1307262000","张三","男",18,"河北省","2000-01-01"),
????????????????new Person("1307262001","李四","女",19,"河南省","2000-02-01"),
????????????????new Person("1307262002","王五","男",20,"湖北省","2000-03-01")};
????????for (int i = 0; i < people.length; i++) {
????????????System.out.println(people[i]);
????????}
????????Scanner sc=new Scanner(System.in);
????????System.out.println("请输入身份证号");
????????String idCard=sc.next();
????????System.out.println("请输入姓名");
????????String name=sc.next();
????????System.out.println("请输入性别");
????????String sex=sc.next();
????????System.out.println("请输入年龄");
????????int age=sc.nextInt();
????????System.out.println("请输入籍贯");
????????String location=sc.next();
????????System.out.println("请输入出生日期");
????????String birthday=sc.next();
????????Person person=new Person(idCard,name,sex,age,location,birthday);
????????boolean boo=false;
????????for (int i = 0; i < people.length; i++) {
????????????if(person.equals(people[i])){
????????????????System.out.println("相等的为:"+people[i]);
????????????????boo=true;
????????????}
????????}
????????if(!boo){
????????????System.out.println("没有相等的");
????????}
????}
}

姓名:张三,户籍:河北省,年龄:18

姓名:李四,户籍:河南省,年龄:19

姓名:王五,户籍:湖北省,年龄:20

请输入身份证号

1307262000

请输入姓名

张三

请输入性别

asd

请输入年龄

12

请输入籍贯

123

请输入出生日期

213

相等的为:姓名:张三,户籍:河北省,年龄:18

Process finished with exit code 0

10: (Object 类)仔细阅读以下代码,写出程序运行的结果。

?????

?????

true

false

11: (Object 类)仔细阅读以下代码,//1 处填入哪些代码可以编译通过()

?????????

? ? ? A?stu1 + ?“ ?” + 100 B. 100 + “ ”?+ stu1

? ? ?C“ ” + 100 ?+ stu1 D. stu1 + 100 + “ ”这个不是String环境,加字符串才会认为是String 默认调用? ? ?toString方法 ???

12:(Object 类)在 Object 类中,定义的 finalize 方法在????当内存满到不足以创建对象?????????时调用;toString 方法返回值表示????对象的地址空间???????; equals 方法的作用为 ??比较两个对象的内容是否相同???????????; getClass 方法作用为 ??????获取类对象地址 ????????

13: (Object 类)编程:定义一个用户类(User 类),属性有用户名、用户密码 password、电话(tel),

要求如下:

对类进行封装,提供 get/set 方法;

同时提供无参数、有参数的构造方法

覆盖toString 方法,要求格式为:

username:一如既往,password:123456,tel:13051800688

覆盖 equals 方法,要求:只要用户名相同则为相同对象

写一个测试类,利用键盘分别输入两个用户信息并存储 Use 对象中,判断两个对象是否相同。

package com.by.homework1;

public class User {
????private String user;
????private String password;
????private String tel;

????public User() {
????}

????public User(String user, String password, String tel) {
????????this.user = user;
????????this.password = password;
????????this.tel = tel;
????}

????public String getUser() {
????????return user;
????}

????public void setUser(String user) {
????????this.user = user;
????}

????public String getPassword() {
????????return password;
????}

????public void setPassword(String password) {
????????this.password = password;
????}

????public String getTel() {
????????return tel;
????}

????public void setTel(String tel) {
????????this.tel = tel;
????}
????public String toString(){
????????return "username:"+user+",password:"+password+",tel:"+tel;
????}
????public boolean equals(Object o){
????????if(this==o){
????????????return true;
????????}
????????if(this.getClass()!=o.getClass()||o.getClass()==null){
????????????return false;
????????}
????????User user1=(User) o;
????????return this.user.equals(user1.user);
????}
}



package com.by.homework1;

public class UserTest {
????public static void main(String[] args) {
????????User []users={new User("haha","123","123"),
????????????????new User("haha","234","234")};
????????if (users[0].equals(users[1])){
????????????System.out.println("相等:"+users[0]+users[1]);
????????}
????}
}

14:(toString 方法,equals 方法)写出Object 类中toString 方法的签名, 以及equals 方法的签名

?????public String toString() {
????return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

这个方法返回对象的字符串表示。默认实现是返回对象的类名和哈希码的十六进制表示,通常这对于调试和日志记录是有用的。但是,通常我们会覆盖这个方法以提供更有意义的字符串表示。

public boolean equals(Object obj) {
????return (this == obj);
}

这个方法用于比较两个对象是否相等。默认实现是比较对象的内存地址,也就是说,只有当两个引用指向同一个对象时,才会返回true。但是,通常我们会覆盖这个方法以提供更符合我们需要的比较逻辑。

15:(toString 方法)写出下列代码的运行结果

null

name Tom age 18

???

文章来源:https://blog.csdn.net/zhzjn/article/details/135362254
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。