Java接口与内部类

public interface Comparable<T>{
    int compareTo(T other);
}
任何实现Comparable接口的类都需要包含compareTo方法
java.lang.Comparable<T>
    int compare(T other)用这个对象与other进行比较

java.lang.Integer
static int compare(int x,int y)

在接口声明中,默认接口中的所有方法都是public的,但,在实现接口时,必须把方法声明为public,否则,编译器将认为该方法的访问属性为protected。

class Employee implements Comparable<Employee>{
    public int compare(Employee other){
        return Double.compare(salary,other.salary);
    }
}

接口的特性
接口不能被实例化。
接口中能包含常量,

double a();
double b=95;// 自动为public static final 

如果需要实现多个接口,
class Employee implements Cloneable,Comparable

浅复制和深复制

package com.cloneTest;

public class CloneTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            Employee original=new Employee("John",50000);
            original.setHireDay(2000, 1, 1);
            Employee coo=original;
            coo.raiseSalary(20);
            Employee copy=original.clone();
            copy.raiseSalary(10);
            System.out.println(coo);
            System.out.println("original="+original);
            System.out.println("copy="+copy);

        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
    }

}
执行结果:
Employee [name=John, salary=60000.0, hireDay=Sat Jan 01 00:00:00 CST 2000]
original=Employee [name=John, salary=60000.0, hireDay=Sat Jan 01 00:00:00 CST 2000]
copy=Employee [name=John, salary=66000.0, hireDay=Sat Jan 01 00:00:00 CST 2000]
由于clone实现的是深拷贝,所有对变量的改变并没有影响原始对象。

另一种克隆对象的机制,使用Java的序列化功能,容易实现且安全,但效率低。
对象流与序列化
可以将任何对象写出到流中,并在之后将其读回。

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("employee.dat"));
//为了保存对象,可以直接使用ObjectOuputStream的writeObject方法
Employee harry=new Employee("Harry Hacker",50000,1989,10,1);
Manager boss=new Manager("Carl Cracker",80000,1987,12,15);
out.writeObject(harry);
out.writeObject(boss);
//为了将这些对象读回,首先需要获得一个ObjectInputStream对象:
ObjectInputStream in=new ObjectInputStream(new FileInputStream("Employee.dat"));
//然后用readObject方法将这些对象被写出时的顺序获得它们
Employee e1=(Employee)in.readObject();
Employee e2=(Employee)in.readObject();
//但,对希望在对象流中存储或恢复的所有类都应进行一下修改,这些类都必须实现Serializable接口
class Employee implements Serializable{...}
//只有在写出对象时才能用writeObject/readObject方法,对于基本数据类型,
需要使用诸如writeInt/readInt writeDouble/readDouble这样的方法。

对象流输出中包含所有对象的类型和数据域
每个对象都被赋予一个序列号
相同对象的重复出现将被存储为对这个对象的序列号的引用
内部类
内部类方法可以访问该类定义所在的作用域中的数据,包括私有的数据。
内部类可以对同一包中的其他类隐藏起来。
匿名内部类比较便捷。
只有内部类可以是私有类,而常规类只可以具有包可见性protected或公有可见性public。

匿名内部类
public void start(int interval,final boolean beep){
    ActionListener listener=new ActionListener(){
        public void actionPerformed(ActionEvent event){
            Date now=new Date();
            System.out.println("At the tone,the time is"+now);
            if(beep)
                Toolkit.getDefaultToolkit().beep();
        }
    };
    Timer t=new Timer(interval,listener);
    t.start();
}

由外部方法访问final变量
与其他内部类相比较,局部类还有一个优点,它们不仅能够访问包含它们的外部列,还可以访问局部变量。不过,那些局部变量必须被声明为final,

public void start(int interval,**final boolean beep**)
{
    class TimePrinter implements ActionListener{
        public void actionPerformed(ActionEvent event){
            Date now=new Date();
            System.out.println("the time is"+now);
            if(beep)
                Toolkit.getDefaultTookit().beep();
        }
    }
    ActionListener listener=new TimePrinter();
    Timer t=new Timer(interval,listener);
    t.start();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值