Java类和接口名称冲突 [英] Java Class and Interface Name Collision

查看:209
本文介绍了Java类和接口名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interface A
{
void print();
}


class A implements A
{

public void print()
{
System.out.println("Hello");
}

public static void main(String args[])
{
A a=new A();
a.print();
}

}

当我使用此代码时,它说的是重复类:A".为什么这样?我可以没有相同的类和接口名称吗?

When i am using this code then it is saying "duplicate class:A". Why so? Can I not have same class and interface name

推荐答案

因为Java语言不允许,所以不能具有相同名称的类和接口.

You can't have a class and an interface with the same name because the Java language doesn't allow it.

首先,它是模棱两可的.如果您声明这样的变量:

First of all, it's ambiguous. If you declare a variable like this:

A a;

该变量的类型是什么?是类还是接口?

What is the type of that variable? Is it the class, or the interface?

第二个已编译的Java代码存储在以文件中定义的类或接口命名的.class文件中.名为A的接口和名为A的类都将编译为名为A.class的文件.同一文件夹中不能有两个同名文件.

Second, compiled Java code is stored in .class files named after the class or interface defined in the file. An interface named A and a class named A would both compile to a file named A.class. You can't have two files with the same name in the same folder.

错误消息显示重复类",因为Java在内部将接口视为一种特殊的类.

The error message says "duplicate class" because Java internally treats an interface as a special kind of class.

这篇关于Java类和接口名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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