-2

这只是一个小问题,但我只需要更清楚地了解这两个。

4

4 回答 4

2

通过导入告诉编译器我的程序将使用导入的类,所以请让它们可用。

import java.util

通过继承类,您将在子类中使用类属性和函数(正在被继承)。

class Maruti extends Car{
}
于 2012-07-05T05:32:21.750 回答
1

import允许您在当前正在编写的类中使用导入的类。

继承或使用extends关键字允许您使用您继承的类的功能来实现当前类。

例如:

public class Animal
{
    public void walk()
    {
        System.out.println("i am walking");
    }
}

public class Cat extends Animal
{
    public void meow()
    {
        System.out.println("Meow!");
    }

    public static void main(String[] args)
    {
        Cat catAnimal = new Cat();
        cat.walk();
        cat.meow();
    }
}

正如你在上面的例子中看到的那样,因为Cat extends AnimalCat可以做类可以做的一切Animal

于 2012-07-05T05:31:11.037 回答
0

import让你看到类,这样你就可以inherit(又名扩展)它。

于 2012-07-05T05:25:50.297 回答
0

导入类只是让您的类可以访问这些类,而无需使用它们的完全限定名。

例如,如果您有以下情况:

import javax.swing.JFrame;
public class Main{
//this class has ACCESS to the JFrame class, but it isn't a JFrame because it doesn't inherit (extend) from one
}

您的 Main 类可以访问类 javax.swing.JFrame 中的方法和变量,而无需使用该全名调用它,它允许您简单地使用 JFrame。

继承一个类是扩展你自己的类以获得对类方法和变量的访问,因为你的类“是一个”继承类。

这是一些不“导入” JFrame 的代码,它使用其完全限定名称来扩展自身,以便它可以访问 JFrame 类中的每个方法/变量(只要修饰符是公共的或受保护的)。

public class MyFrame extends javax.swing.JFrame {
//this class IS a JFrame, and is not importing one, it's instead using the fully qualified name to extend itself and make it a JFrame.
}
于 2012-07-05T05:30:56.613 回答