导入和扩展类有什么区别? [英] What's the difference between importing and extending a class?

查看:165
本文介绍了导入和扩展类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中导入和扩展类之间有什么区别

What's the difference between importing and extending a class in Java

推荐答案

这是两个截然不同的事情。

Those are two very different things.

导入一个类,这样就可以使用该类,而无需在你正在编写的当前类中限定全名。

Importing a class, is making it so you can use that class without needing to qualify the full name in the current class you are writing.

import java.util.Scanner
// now you can use the Scanner class in your code like so:
Scanner stdin = new Scanner(System.in);
// instead of having to do
java.util.Scanner stdin = new java.util.Scanner(System.in);

扩展类正在创建一个新类,它是其他类的子类。这将允许您添加或更改要扩展的类的功能。

Extending a class is creating a new class that is a subclass of some other class. This will allow you to add or change functionality of the class you are extending.

// this is a very contrived example
public class EmptyList extends ArrayList {
    @Override
    public boolean add(Object o){
        return false; // will not add things to a list
    }
}

这篇关于导入和扩展类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆