提交 6c6261df 编写于 作者: M Mars Liu

class, interface, io

上级 32959c54
{
"type": "code_options",
"author": "刘鑫",
"source": "classic.md",
"notebook_enable": true,
"exercise_id": "80d2fc513df3404288cdd3d487f50893"
}
\ No newline at end of file
# 类和类型
## aop
### template
```java
public class Main {
static class Human {
String label;
public String getLabel() {
return "Human";
}
}
static class Employee extends Human{
public String getLabel() {
return "$code";
}
}
public static void main(String[] args) throws IOException {
Human joe = new Employee();
System.out.println(joe);
}
}
```
我们有两个类型:
```java
class Human {
String label;
public String getLabel() {
return "Human";
}
}
class Employee extends Human{
public String getLabel() {
return "Employee";
}
}
```
现在我们有对象
```java
Human joe = new Employe(...)
```
调用 `joe.getLable()` 的时候,返回的是?
## 答案
Employee
## 选项
### A
Human
### B
HumanEmployee
### C
Main$Employee@7a81197d
### D
Main$Human@7a81197d
{
"keywords": [],
"children": [],
"node_id": "java-461a08a44ae845018b7f0356ea928085"
"node_id": "java-461a08a44ae845018b7f0356ea928085",
"export": ["classic.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "abstract.md",
"notebook_enable": false
}
\ No newline at end of file
# 抽象类
关于抽象类和接口的区别,以下说法正确的是
## 答案
一个实现类型只能有一个抽象基类,但是可以有多个实现接口。
## 选项
### 抽象类才能定义构造方法
接口可以抽象构造行为
### 并发安全与接口或类无关
接口是线程安全的,抽象类不是
### 接口和类型都可以使用 instanceof 判别
可以用 instanceof 判断对象是否是某一个抽象类的实现,但是不能用来判断接口
### 接口和抽象类都支持泛型
抽象类可以是泛型类型,接口不能带有模板参数
\ No newline at end of file
{
"keywords": [],
"keywords": ["面向对象", "抽象类", "接口"],
"children": [],
"node_id": "java-a02b334b2e364161badae34636a0284f"
}
\ No newline at end of file
{
"keywords": [],
"keywords": ["面向对象", "类型", "接口"],
"children": [],
"node_id": "java-5d28bfbed25f4ae2b4d21fc1f2222e49"
"node_id": "java-5d28bfbed25f4ae2b4d21fc1f2222e49",
"export":["interface.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "interface.md",
"notebook_enable": false,
"exercise_id": "462fc2e521564aa2b358cbcffb964db9"
}
\ No newline at end of file
# 接口
关于接口,错误的选项是
## 答案
```java
public interface Named{
Named(String value);
String literal();
}
```
## 选项
### interface 可以有 default 实现
```java
public interface Parsec{
Try<String> parse(State<String> state);
default T ask(State<String> state) {
var result = parse(state);
if(result.isSuccess()){
return result.get();
} else {
throw result.err();
}
}
}
```
### interface 可以有类型参数
```java
public interface Parsec<E, T>{
Try<T> parse(State<E> state);
default T ask(State<E> state) {
var result = ask(state);
if(result.isSuccess()){
return result.get();
} else {
throw result.err();
}
}
}
```
### interface 可以实现
```java
public class One implements Parsec<String, String>{
default T parse(State<String> state) {
return state.next();
}
}
```
### SAM
```java
public interface Parsec<E, T> {
public Parsec<E, T> then(Parsec<E, T> psc) {
return state -> {
this.parse(state);
return psc.parse(state)
}
}
}
```
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "anonymous.md",
"notebook_enable": false,
"exercise_id": "31ea397861004b0e9df6d767380093ef"
}
\ No newline at end of file
# 匿名类
以下代码
```java
public class App {
public Parsec<Character, String> text(String txt){
return state -> {
if(String.equals(state.read(txt.legth), txt)){
return new Success(txt);
} else {
return new NotMatch(state);
}
}
}
}
```
返回的类型是:
## 答案
实现了 Parsec 接口的匿名类型。
## 选项
### A
`App` 类型
### B
`String` 类型
### C
`Parsec` 接口类型
### D
`Function<Charactor, String>` 类型
{
"keywords": [],
"keywords": ["面向对象", "匿名类", "lambda"],
"children": [],
"node_id": "java-b06b5b2c7a6b44cc87744168729b31e6"
"node_id": "java-b06b5b2c7a6b44cc87744168729b31e6",
"export":[
"anonymous.json"
]
}
\ No newline at end of file
......@@ -2,6 +2,6 @@
"node_id": "java-fc2dfe1a80f64143b4ad339378ba61da",
"keywords": [],
"children": [],
"export": [],
"export": ["unzip.json"],
"title": "InputStream类型"
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "unzip.md",
"notebook_enable": false,
"exercise_id": "cc0ec19a231d4e6e940755474c7a5b47"
}
\ No newline at end of file
# 解压流
已知 GZIPInputStream 是 Java 标准库中用于从 gzip 格式文件读取信息的类型,那么从一个gzip文件读取信息并解压的正确实现应该是:
## 答案
```java
String fileZip = "path_to_file.zip";
File destDir = new File("path_to_dest");
byte[] buffer = new byte[1024];
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip))) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
// ...
}
zis.closeEntry();
}
```
## 选项
### 没有关闭资源
```java
String fileZip = "path_to_file.zip";
File destDir = new File("path_to_dest");
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip))
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
// ...
}
```
### zip 流需要接到字节输入流上工作
```java
String fileZip = "path_to_file.zip";
File destDir = new File("path_to_dest");
byte[] buffer = new byte[1024];
try (ZipInputStream zis = new ZipInputStream(fileZip)) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
// ...
}
zis.closeEntry();
}
```
### 缓冲区需要分配空间
```java
String fileZip = "path_to_file.zip";
File destDir = new File("path_to_dest");
byte[] buffer = null;
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip))) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
// ...
}
zis.closeEntry();
}
```
\ No newline at end of file
......@@ -2,6 +2,6 @@
"node_id": "java-cc8c2e511db54a2ca2ee798f2cdbd03b",
"keywords": [],
"children": [],
"export": [],
"export": ["zip.json"],
"title": "OutputStream类型"
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "zip.md",
"notebook_enable": false,
"exercise_id": "3f6e7226750b4af5b13ee2cfbb0034fb"
}
\ No newline at end of file
# 压缩流
已知 ZipOutputStream 是 Java 标准库的压缩输出流,那么下列哪个选项可以正确的将指定文件压缩为zip文件?
## 答案
```java
String sourceFile = "path_to_source";
File fileToZip = new File(sourceFile);
try (FileOutputStream fos = new FileOutputStream("path_to_zip");
FileInputStream fis = new FileInputStream(fileToZip);
ZipOutputStream zipOut = new ZipOutputStream(fos);) {
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
}
```
## 选项
### 没有关闭资源
```java
String sourceFile = "path_to_source";
File fileToZip = new File(sourceFile);
FileOutputStream fos = new FileOutputStream("path_to_zip");
FileInputStream fis = new FileInputStream(fileToZip);
ZipOutputStream zipOut = new ZipOutputStream(fos);
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
```
### ZipOutputStream 不能直接读写文件,需要接入其它字节流
```java
String sourceFile = "path_to_source";
File fileToZip = new File(sourceFile);
try (FileInputStream fis = new FileInputStream(fileToZip);
ZipOutputStream zipOut = new ZipOutputStream("path_to_zip");) {
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
}
```
### 缓冲区需要初始化
```java
String sourceFile = "path_to_source";
File fileToZip = new File(sourceFile);
try (FileOutputStream fos = new FileOutputStream("path_to_zip");
FileInputStream fis = new FileInputStream(fileToZip);
ZipOutputStream zipOut = new ZipOutputStream(fos);) {
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = null;
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
}
```
\ No newline at end of file
......@@ -1803,21 +1803,33 @@
{
"抽象类": {
"node_id": "java-a02b334b2e364161badae34636a0284f",
"keywords": [],
"keywords": [
"面向对象",
"抽象类",
"接口"
],
"children": []
}
},
{
"接口": {
"node_id": "java-5d28bfbed25f4ae2b4d21fc1f2222e49",
"keywords": [],
"keywords": [
"面向对象",
"类型",
"接口"
],
"children": []
}
},
{
"匿名类": {
"node_id": "java-b06b5b2c7a6b44cc87744168729b31e6",
"keywords": [],
"keywords": [
"面向对象",
"匿名类",
"lambda"
],
"children": []
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册