hashMap put方法 第二行代码 1

public interface Zi<K,V> {
        //我是接口
        //通过Zi<K,V> zi的方式可以让我实现多态
        //多态的好处是:
        //1. 应用程序不必为每一个派生类编写功能调用,只需要对抽象基类进行处理即可。大大提高程序的可复用性。//继承 
        //2. 派生类的功能可以被基类的方法或引用变量所调用,这叫向后兼容,可以提高可扩充性和可维护性。 //多态的真正作用
        
    interface Entry<K,V> {
        K getKey();
        V getValue();
    }
}
public class Zs<K,V> implements Zi<K,V>{
                                        //我是一个带泛型参数的类
                                        //通过Zs<Integer,String> t = new Zs<Integer,String>()创建实例时不用转化类型
                                        //我实现了接口Zi.Entry<K,V>
                                        
    
    static final Entry<?,?>[] EMPTY_TABLE = {};
                                            //我先是一个数组
                                            //数组里的元素的数据结构是Entry<?,?>形式的
                                            //Entry是内部静态类
                                            //我被赋值为一个空的数组但我的结构是Entry<?,?>的
                                            //我不能被修改和继承
                                            //通过字面意思EMPTY_TABLE可以知道我的作用就是定义一个标识空表的常量
                                    
                                            
    
    transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
                                                //EMPTY_TABLE被转换成了Entry<K,V>[]的形式
                                                //因为Entry<?,?>里的参数是泛型所以可以被转换
                                                //transient是瞬值的意思
                                                //瞬值意味着table不会被序列化
                                                //不会被序列化也意味着不能通过反序列化的形式获取它
                                                //在这里table被赋值成了一个Entry<K,V>[]形式的空数组
                                                //虽然是空数组其内部元素保留了Entry<K,V>的结构
                                                //通过字面意思table可以知道我的作用就是定义了一个数据且我的初始值为一个空数组
                                                
    
    public String put(K key, V value) {
        if (table == EMPTY_TABLE) {//假如table还没有被赋值或者装入数据
            return "table->"+table+"-----"+"///EMPTY_TABLE->"+EMPTY_TABLE+"-----"+"///value->"+value+"///null";
        }
         return "table->"+table+"-----"+"///EMPTY_TABLE->"+EMPTY_TABLE+"-----"+"///value->"+value+"///not null";
    }
    
    
    //这里是我对接口Zi.Entry<K,V>的具体实现
    static class Entry<K,V> implements Zi.Entry<K,V>{//to realization the interface
        
        final K key;//我被final修饰
                    //不可修改
                    //不可被继承
        
        V value; //V是泛型
                //是键值对里的值
        
        Entry(K k, V v) {    
                        //我是构造函数
            key = k;    //初始化key的值
            value = v;  //初始化value的值
        }
        
        public final K getKey() {
            return key;
        }

        public final V getValue() {
            return value;
        }
        
    }
    
}
public class Zh {


    
    
    public static void main(String []args) {
       Zs<Integer,String> t = new Zs<Integer,String>();
       String s = t.put(1,"hello world").toString();
       System.out.println(s);
       System.out.println(s.hashCode());
    }
} 

接口、接口实现、调用

这里只是对一些不常见的语法和概念进行一个大概的认知

可能并不是完全正确的

原文地址:https://www.cnblogs.com/hellowzd/p/9722875.html