Loading

java(先放着)

1、dictionaryhashtableproperties的区别

dictionary已被废除,hashtable是dictionary的子类,它与hashmap的区别在于hashtable是线程安全的,可以多线程访问,propertieshashtable的子类,它相比于hashtable增加了持久化的属性,可以将k-v 键值对存储到硬盘中

2、集合框架

map->hashmap

collection->list->ArrayList

​ ->LinkedList

​ ->set

3、泛型

泛型方法

即参数化类型,就是在使用时传入具体的操作数据类型

public class GenericsTest {
    public static T void printArray( T[] tArray) {
        for ( T ele : tArray) {
            System.out.printf("%s",ele);
        }
    }
    public static void main( String args[]){
        Integer[] intArray = {1,2,3,4,5};
        Double[] doubleArray = {1.2,2.3,4.5,5.6,7.8};
        printArray(intArray);
        printArray(doubleArray)
          
    }
}

有界类型参数:对于传入的参数进行限制。声明一个有界参数,类型参数+extends+上界

//数字类实现了Comparable接口
public static <T extends Comparable<T>> T maxnum( T x, T y, T z){
    T max = x;
    if ( y.compareTo(max) > 0) {
        max = y;
    }
    if ( z.compareTo(max) > 0) {
        max = z
    }
    return max;
}

泛型类

public class Box<T> {
    private T t;
    public void add( T t){
        this.t = t;
    }
    public T get(){
        retun t
    }
    public static void main(String[] args){
        Box<Integer> integerBox = new Box<Integer>();
        integerBox.add(new Integer(10))
    }
}

//多个参数用逗号隔开
public class Box2<T,S,V> { ... }

4、序列化

一个对象可以被表示为一个字节序列,该字节序列包括该对象数据的数据、有关对象的类型信息和存储在该对象中的数据类型

类序列化的条件:

  1. 该类必须实现java.io.Serializable对象
  2. 该类的所有属性必须是可序列化的,如果有一个属性不是可序列化的,则该属性必须是短暂的
//序列化对象
import java.io.*;

public class SerializeDemo
{
   public static void main(String [] args)
   {
      Employee e = new Employee();
      e.name = "Reyan Ali";
      e.address = "Phokka Kuan, Ambehta Peer";
      e.SSN = 11122333;
      e.number = 101;
      try
      {
         FileOutputStream fileOut =
         new FileOutputStream("/tmp/employee.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in /tmp/employee.ser");
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }
}
//反序列化对象
import java.io.*;
public class DeserializeDemo
{
   public static void main(String [] args)
   {
      Employee e = null;
      try
      {
         FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("Deserialized Employee...");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
    }
}

注:当对象被序列化时,属性SSN的值为111222333,但是因为该属性是短暂的,该值没有被发送到输出流。所以反序列化后Employee对象的SSN属性为0。

5、网络编程

6、多线程编程

posted @ 2023-01-09 13:03  淡蓝色的点  阅读(20)  评论(0)    收藏  举报