java 什么是公共接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6076890/
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-10-30 14:12:57  来源:igfitidea点击:

What is a public interface?

java

提问by Seaker33

"Using only the public interface of the linked list class, write a method public static void reverse(LinkedList staff) that reverses the entries in a linked list."

“仅使用链表类的公共接口,编写一个方法 public static void reverse(LinkedList staff) 来反转链表中的条目。”

The part of all of that I'm not understanding is the first part. What does it mean by public interface of linked list class? Do I create a new class file that starts with something like public interface linkedlist? Can I add the method as a inner class within my main class?

我不明白的所有部分是第一部分。链表类的公共接口是什么意思?我是否创建了一个以公共接口链表之类的东西开头的新类文件?我可以将该方法作为内部类添加到主类中吗?

回答by dlev

The question is unnecessarily vague. What it's trying to ask is "using only the methods and fields of LinkedListthat have the access modifier public, write this method."

这个问题不必要地含糊不清。它试图询问的是“仅使用LinkedList具有访问修饰符的方法和字段public,编写此方法。”

You can put the method you write in any class you like, but the restriction says that you may only use publicmethods and fields on LinkedListto write it.

你可以把你写的方法放在你喜欢的任何类中,但限制说你只能使用public方法和字段LinkedList来编写它。

This also means that you can't create a subclass of LinkedListand use its protectedmethods.

这也意味着您不能创建子类LinkedList并使用其protected方法。

回答by Bob Kaufman

The public interface of a class are its public properties (variables or fields you can read the values of or assign to) and methods (functions you can call).

类的公共接口是其公共属性(您可以读取其值或分配给的变量或字段)和方法(您可以调用的函数)。

So, the assignment is to create something that is not a subclass of LinkedList. Creating a subclass would give you access to protected methods for example. You need to create something external to LinkedList, for example:

因此,任务是创建不是LinkedList. 例如,创建子类可以让您访问受保护的方法。你需要在 LinkedList 之外创建一些东西,例如:

void MyMethod()
{
    LinkedList l = new LinkedList();

    // do something with l here.
}

回答by Corkscreewe

"Public interface of linked list class" means only public methods of the LinkedList class. See the javadoc, there is a list of all public methods, or create new LinkedList instance and let your IDE suggest.

“链表类的公共接口”仅指LinkedList类的公共方法。请参阅javadoc,其中包含所有公共方法的列表,或者创建新的 LinkedList 实例并让您的 IDE 提出建议。