从零开始学java - 第十九天

76 阅读2分钟

这是我参与11月更文挑战的第19天,活动详情查看:2021最后一次更文挑战

今天继续~

泛型类

  • 与泛型方法一样,包含一个或多个类型参数,用逗号隔开
  • 可接受不同类型的实参
public class Game<P>{
    private P p;
    
    public void play(P p){
        this.p = p;
    }
    
    public P get(){
        return p;
    }
    
    public static void main(String[] args){
        Game<Integer> integerGame = new Game<Integer>();
        Game<String> stringGame = new Game<String>();
        
        integerGame.add(11);
        stringGame.add("CSGO");
        
        System.out.println(integerGame.get());
        System.out.println(stringGame.get());
    }
}

类型通配符

  • 一般用?代表具体的参数,例如Game<?>就可以匹配Game<Integer>Game<String>
import java.util.*;
public class Test{
    public static void getData(Game<?> data){
        System.out.println(data.get(0));
    }
    
    public static void main(String[] args){
        Game<String> name = new ArrayList<String>();
        Game<Integer> price = new ArrayList<Integer>();
        
        name.add("CSGO");
        price.add(0);
        
        getData(name);
        getData(price);
    }
}

ps:因为Game<?>用的是类型通配符,所以传入任何类型的实参都可以

  • 可以限制类型通配符的范围:
Game<? extend Number>

ps:指接收类型的上限为Number,如果传入String等类型就会报错

序列化

  • 在序列化中,一个对象可以被作为一个字节序列,该序列中包含对象的数据、对象的类型信息和存储在对象中数据的类型
  • 序列化对象可以被写入文件,也能被读取出来,整个过程中java虚拟机(JVM)是独立的,也就是说可以在两个完全不同的平台完成对象的传递
  • 类ObjectInputStream和ObjectOutputStream包含着反序列化和序列化对象的方法

序列化对象

  • ObejctOutputStream类用来序列化一个对象
import java.io.*;

public class Serialize{
    public static void main(String[] args){
        Employee e = new Employee();
        e.name = "CSGO";
        e.price = 0;
        e.time = 19200054620;
        try{
               FileOutputStream fileOut = new FileOutputStream("/java/serialize.ser");
               ObjectOutputStream out = new ObjectOutputStream(fileOut);
               out.writeObject(e);
               out.close();
               fileOut.close();
               System.out.println("success");
        }catch(IOException i){
            i.printStackTrace();
        }
    }
}

ps:

  • 所有的输入输出都在io包里,所以需要引入
  • Employee构建一个数组,并往里写入三个值
  • 创建文件对象->打开文件->写东西->关上

反序列化对象

import java.io.*;
public class Deserialization{
    public static void main(String[] args){
        Employee e = null;
        try{
            FileInputStream fileIn = new FileInputStream("/java/serialize.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (Employee) in.readObject();
            in.close();
            fileIn.close();
        }catch(IOException i){
            i.printStackTrace();
        }catch(ClassNotFoundException c){
            System.out.println("Employee is not found");
            c.printStackTrace();
            return;
        }
        System.out.println(e.name);
        System.out.println(e.price);
        System.out.println(e.time);
    }
}

ps:设置空数组->获取文件->打开文件->读取数据到e->关闭文件->将读取到的输出

网络编程

  • 是指编写运行在多个设备的程序,这些设备都通过网络连接
  • java.net包存在低层次的通信细节,可直接使用这些类或接口
  • java.net提供两种通信支持
    • TCP - 面向连接,可靠,基于字节流的通信层传输协议,位于ip层之上,应用层之下,它保障了两个应用程序之间的通信,通常用于互联网协议
    • UDP - 无连接,不可靠,位于OSI模型的传输层,提供应用程序之间发送数据的数据报。由于UDP的不可靠性,它的数据包一般都是容许丢失,错误和重复的数据包