Java 导入和扩展类有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3468252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:02:39  来源:igfitidea点击:

What's the difference between importing and extending a class?

java

提问by selvam

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

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

采纳答案by jjnguy

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
    }
}

回答by Bozho

When, in your class, you reference a class that is not in the same package as your class, you need to importthe other one.

当在您的类中引用一个与您的类不在同一个包中的类时,您需要导入另一个类。

When you want to use oop inheritance, you extenda class - i.e. your class has the functionality of the superclass + whatever your write in your class.

当您想使用 oop继承时,您可以扩展一个类 - 即您的类具有超类的功能+您在类中编写的任何内容。

The two things are rather different, so perhaps you should create some simple programs, and see for yourself the obvious difference.

这两件事相当不同,所以也许您应该创建一些简单的程序,并亲自看看明显的区别。

回答by Damian Leszczyński - Vash

Import is a optional clause that declare which class you will use in you potential class interface or enumeration. The extend says to class that she can use the functionality of that parent class.

Import 是一个可选子句,它声明您将在潜在的类接口或枚举中使用哪个类。扩展对类说她可以使用该父类的功能。

To extend a public class you need to import it first.

要扩展公共类,您需要先导入它。

回答by Brian Agnew

Importing means that you can reference it in a non-qualified way. e.g.

导入意味着您可以以非限定方式引用它。例如

import java.util.List;

List list = ...

as opposed to

java.util.List list =

Extending is completely different, and means inheriting behaviour and structure from a class

扩展是完全不同的,意味着从类继承行为和结构。

回答by Rasmikanta Guru

Extending
Extending means enhancing the functionality of an existing class. Importing
Importing means use some api as a helper to your class for desinging.

扩展
扩展意味着增强现有类的功能。 导入
导入意味着使用一些 api 作为您的类的助手进行设计。

回答by Md Alam

Import doesn't change your program, it just allows you to write the short form of declaring a class. In your own class you can use any other class from any package within the Java library.
Lets say you want to use the Scanner class to take input from the keyboard. Instead of writing java.util.Scanner sc = new java.util.Scanner(System.in);, you can simply write Scanner sc = new Scanner(System.in);.
If you import the package or a package followed by the class name at the top of your class which is import java.util.*;or import java.util.Scanner;

导入不会改变您的程序,它只是允许您编写声明类的简短形式。在您自己的类中,您可以使用 Java 库中任何包中的任何其他类。
假设您想使用 Scanner 类从键盘获取输入。java.util.Scanner sc = new java.util.Scanner(System.in);您可以简单地写而不是写Scanner sc = new Scanner(System.in);
如果您导入包或包后跟类顶部的类名,即 importjava.util.*;或 import java.util.Scanner;

Extending a class is not as simple as importing a class. When you extend a class you are adding all instances (fields) and methods of the extended class into your own class. In other words, you have access to all of the fields and methods of the extended class.

扩展一个类并不像导入一个类那么简单。当您扩展类时,您将扩展类的所有实例(字段)和方法添加到您自己的类中。换句话说,您可以访问扩展类的所有字段和方法。

回答by gaurav

first of all ,import is use to increase the efficiency of compiler to find the correct class the main difference ,is the import use aggregation(has-a) concept & in extends we use (is-a) relation
in has-a or import , the obj of our class is not maintain a lifetime relation with import classes we need to get the obj of particular class that we want to use in our application but in extends the obj of our class is make a life time relation with parent class... HAPPY if i am right other wise give right ans....

首先,import 是用来提高编译器的效率以找到正确的类的主要区别,是 import 使用聚合(has-a)概念&在扩展中我们
在 has-a 或 import 中使用(is-a)关系,我们的类的 obj 不与导入类保持生命周期关系,我们需要获取我们想要在应用程序中使用的特定类的 obj,但在扩展中,我们类的 obj 与父类建立生命周期关系。 .. 如果我是对的,那么很高兴,否则请给出正确的答案....

回答by joy

importing a class provides us to access its predefined methods.we cnn't chnge those methods but we can use them. extending a class means we can override the methods defined in the class to be inherited.

导入一个类使我们能够访问它的预定义方法。我们不能更改这些方法,但我们可以使用它们。扩展类意味着我们可以覆盖要继承的类中定义的方法。

回答by F.O.O

Package import is just a way to tell the classloader where to look for your classes and also differentiate between classes with the same name. Class extend tells JVM the heirarchical relationship between your classes. OO rules apply once the class is found.

包导入只是告诉类加载器在何处查找您的类以及区分具有相同名称的类的一种方式。类扩展告诉 JVM 类之间的层次关系。一旦找到类,OO 规则就适用。

回答by ram

In Simple:

简单:

Import refers to Has arelationship. With keyword new.

进口是指关系。使用关键字new

Extend refers to IS arelationship. With keyword extends.

扩展指的是一种关系。随着关键字extends

Please refer thislink for good example.

请参考链接作为很好的例子。