2012-07-05 87 views
-2

这只是一个小问题,但我只需要更清楚地了解这两个问题。导入类和继承类

+3

在搜索中寻求清晰度。总结你的发现,然后***问一个问题。 –

+0

你可以下一次做一些网络搜索之前问一个问题吗?谷歌很容易找到关于你的问题的答案。 – Sabbath

回答

2

通过导入,您告诉编译器我的程序将使用导入的类,因此请使它们可用。

import java.util 

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

class Maruti extends Car{ 
} 
0

import让你看到类,所以你可以inherit(又扩展)它。

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 
} 

主类将有机会获得在类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. 
} 
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类即可。