大数据预科班作业9

大数据预科班作业9

1. (Object 类)在Object 类中,定义的finalize 方法在______时调用,toString()方法返回值表示,equals 方法的作用为,getClass 方法作用为__________

1. 被垃圾回收时
2. 对象的字符串表现形式
3. 比较两个对象的值是否相等
4. 获得对象的实际类型

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

toString 方法: public String toString()
equals 方法: public boolean equals(Object obj) 注意:equals 方法参数为 Object 类型

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

class Student{
private int age;
private String name;
public Student(){}
public Student(String name, int age){
this.name = name;
this.age = age;
}
public String toString(){
return name + “ ” + age;
}
}
public class TestStudent{
public static void main(String args[]){
Student stu1 = new Student();
Student stu2 = new Student(“Tom”, 18);
System.out.println(stu1);
System.out.println(stu2);
}
}

null 0
Tom 18

4. (equals)写出下面程序运行的结果

public class TestEquals{
public static void main(String args[]){
String str1 = new String(“Hello”);
String str2 = new String(“Hello”);
System.out.println(str1.equals(str2));
System.out.println(str1 == str2);
}
}

true//比较的是值
false//比较的是地址

5. (getClass)写出下面程序运行的结果

class Animal{}
class Dog extends Animal{}
public class TestGetClass{
public static void main(String args[]){
Animal a1 = new Dog();
Animal a2 = new Animal();
System.out.println(a1 instanceof Animal);
System.out.println(a1.getClass() == a2.getClass());
}
}

true//是否属于类的实例
false//实际对象的类型比较

6. (包装类,类型转换)填空

要把一个int 类型的变量转换为Integer 类型,需要调用_______类的________
方法;要把Integer 类型转为int 类型,需要调用_______类的_________方法;
要把String 类型转为Integer 类型,需要调用______类的_______方法;要把
Integer 转为String 类型,需要调用________类的________方法;
要把int 类型转化为String 类型,需要______________________;要把String 
类型转化为int 类型,需要调用__________类的__________方法。

1. Integer
2. 构造方法
3. Integer
4. intValue
5. Integer
6. 构造方法
7. Integer
8. toString
9. ""+int值
10. Integer
11. parseInt

7. (内部类)Java 中的内部类包括

A. 成员内部类
B. 静态内部类
C. 局部内部类
D. 匿名内部类
E. 公开内部类
F. 抽象内部类

A
B
C(方法内部类)
D

8. (String)写出下面代码输出结果

public class TestString{
public static void main(String args[]){
String str1 = “Hello”;
String str2 = “Hello”;
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
str1 = new String(“Hello”);
str2 = new String(“Hello”);
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
}
}

true
true
false
true

9. 为Worker 类增加equals 和toString 方法。

package chp9;

public class Worker {
    private String name;
    private int age;
    private double salary;
    public Worker(){}
    public Worker(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    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 double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String toString(){
        return name + " " + age + " " + salary;
    }

    public boolean equals(Object obj){
        if (obj == this) return true;
        if (obj == null) return false;
        if (this.getClass() != obj.getClass()) return false;
        Worker w = (Worker) obj;
        if (this.name.equals(w.name) 
                && this.age == w.age
                && this.salary == w.salary){
            return true;
        }else{
            return false;
        }
    }

}

10. (包装类)修改第7 章自动分配id 的Account 类,把id 写成Long 包装类类型的。

注:把 id 写成包装类的好处:可以区分没有分配 id 的对象(默认值为 null)和 id为 0 的对象。

11. 从命令行上读入一个字符串,用两种不同的方法,把该字符串转换为一个int类型

方法一:把String 直接转换为int

方法二:把String 转换为Integer,再把Integer 转换为int 类型

package chp9;

import java.util.Scanner;
public class StringToInt {

    public static void main(String[] args) {
        String str = sc.nextLine();

        //第一种做法,str --> int
        int i1 = Integer.parseInt(str);
        System.out.println(i1);

        //第二种做法,str --> Integer --> int
        Integer ii = new Integer(str);
        int i2 = ii.intValue();
        System.out.println(i2);

    }
}       

12. (toString,字符串加法)*有下面代码

class Student{
private int age;
private String name;
public Student(){}
public Student(String name, int age){
this.name = name;
this.age = age;
}
public String toString(){
return name + “ ” + age;
}
}
public class TestStudent{
public static void main(String args[]){
Student stu1 = new Student(“tom”, 18);
System.out.println(/*1*/);
}
}
问:在/*1*/位置,填入什么代码能编译通过?
A. stu1 + “ ” + 100
B. 100 + “ ” + stu1
C. “ ” + 100 + stu1
D. stu1 + 100 + “ ”

A
B
C

D错在stu1+100为对象+整型没意义

13. (Object 类)*有下面代码

interface IA{
void ma();
}
class MyClass implements IA{
public void ma(){}
public String toString(){
return “MyClass toString()”;
}
}
public class TestMyClass{
public static void main(String args[]){
IA ia = new MyClass();
System.out.println(ia);
}
}
选择正确答案:
A. 编译不通过,因为IA 接口中没有定义toString 方法
B. 编译通过,输出:“IA@地址”
C. 编译通过,输出:“MyClass toString()”

C
虽然 IA 接口中没有定义 toString 方法,但是由于 toString 方法是所有对象都有的方法,
因此依然可以对 IA 类型的引用调用 toString 方法,调用的是子类覆盖以后的方法。

14. (匿名内部类,局部内部类)*写出下面代码执行的结果

interface IA{
void ma();
}
class MyClass {
public static void method(IA ia){
System.out.println(“in method”);
ia.ma();
}
}
public class TestInnerClass{
public static void main(String args[]){
MyClass.method(new IA(){
public void ma(){
System.out.println(“ma in anonymous inner class”);
}
});
class MyMaClass implements IA{
public void ma(){
System.out.println(“ma in local inner class”);
}
}
MyClass.method(new MyMaClass());
}
}

in method
ma in anonymous inner class
in method
ma in local inner class

15. (局部内部类)*有下面代码

class OuterClass{
private int value1 = 100;
private static int value2 = 200;
public void method(int value3){
final int value4 = 400;
class InnerClass{
public void print(){
//1
}
}
}
}
问:下面哪些代码放在//1 处能够编译通过?
A. System.out.println(value1);
B. System.out.println(value2);
C. System.out.println(value3);
D. System.out.println(value4);

A
B
D

16. 已知接口Light 定义如下:

interface Light{
void shine();
}
//定义Lamp 类:
class Lamp{
public void on(Light light){
light.shine();
}
}
//写一个类TestLamp,部分代码如下:
public class TestLamp{
public static void main(String args[]){
/2Lamp lamp = new Lamp();
//1
/
}
}
把TestLamp 类补充完整,要求:
1) 在//1 处使用局部内部类技术,调用lamp 的on 方法要求输出“shine in 
red”
2) 在//2 处使用匿名内部类技术,调用 lamp 的 on 方法要求输出“shine 
in yellow”

package chp9;

interface Light{
    void shine();
}

class Lamp{
    public void on(Light light){
        light.shine();
    }
}

public class TestLamp{
    public static void main(String args[]){
        Lamp lamp = new Lamp();
        //1: 使用局部内部类
        class RedLight implements Light{
            public void shine(){
                System.out.println("shine in red");
            }
        }

        lamp.on(new RedLight());

        //2:使用匿名内部类
        lamp.on(new Light(){
            public void shine(){
                System.out.println("shine in yellow");
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风御浪云帆之上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值