我们如何在java中实现抽象?
根据定义,抽象是隐藏实现细节,只显示功能。但我们究竟藏在哪里,藏在哪里,藏在哪里 AFAIK以下程序是一个抽象示例:我们如何在java中实现抽象?,java,abstraction,Java,Abstraction,根据定义,抽象是隐藏实现细节,只显示功能。但我们究竟藏在哪里,藏在哪里,藏在哪里 AFAIK以下程序是一个抽象示例: public interface ToBeImplemented { public string doThis(); } public class Myclass implements ToBeImplemented { @override public string doThis() { //Implementation return null; } }
public interface ToBeImplemented {
public string doThis();
}
public class Myclass implements ToBeImplemented {
@override
public string doThis() {
//Implementation
return null;
}
}
如果我错了,这不是抽象,那么抽象的正确例子是什么?你对抽象的定义是正确的。您提供的代码绝对是一种抽象形式 为什么? 因为您的代码的用户将只提供该接口。用户只知道您的代码
doThis()
执行特定任务,但他/她不知道代码如何执行特定任务
例如:
public interface myInterface{
public int sumTo(n);
}
public class myClass implements myInterface{
public int sumTo(int n){
int sum=0;
for(int i=0; i<=n; i++){
sum+=i;
}
return sum;
}
}
class SomeClass{
public static void main(String[] args) {
ToBeImplemented someImplementation;
//now you can pick an implementation, based on user input for example
if (userInput == something)
someImplementation = new ImplementationA();
else
someImplementation = new ImplementationB();
//do some staff
//Regardless of the user input, main method only knows about the
//abstract "ToBeImplemented", and so calls the .doThis() method of it
//and the right method gets called based on the user input.
someImplementaion.doThis();
//at That
}
}
公共接口myInterface{
苏门托公共区(北);
}
公共类myClass实现myInterface{
公共综合区(综合区n){
整数和=0;
对于(int i=0;i,在上述示例中,您可以编写如下内容:
public interface ToBeImplemented {
public string doThis();
}
public class ImplementationA implements ToBeImplemented {
@override
public string doThis() {
//ImplementationA of doThis
return null;
}
}
public class ImplementationB implements ToBeImplemented {
@override
public string doThis() {
//ImplementationB of doThis
return null;
}
}
然后您可以拥有另一个类和一个main方法,例如:
public interface myInterface{
public int sumTo(n);
}
public class myClass implements myInterface{
public int sumTo(int n){
int sum=0;
for(int i=0; i<=n; i++){
sum+=i;
}
return sum;
}
}
class SomeClass{
public static void main(String[] args) {
ToBeImplemented someImplementation;
//now you can pick an implementation, based on user input for example
if (userInput == something)
someImplementation = new ImplementationA();
else
someImplementation = new ImplementationB();
//do some staff
//Regardless of the user input, main method only knows about the
//abstract "ToBeImplemented", and so calls the .doThis() method of it
//and the right method gets called based on the user input.
someImplementaion.doThis();
//at That
}
}
抽象是指您可以声明一个ToBeImplemented引用,然后将ImplementationA或ImplementationB(可能还有任何其他实现)分配给它。但是您可以针对抽象ToBeImplemented编写代码,并让一些条件决定ToBeImplemented的正确实现(以及,作为结果,doThis())应该打电话。维基百科是开始学习的好地方:
抽象的本质是保存与给定上下文相关的信息,而忘记与该上下文无关的信息
约翰·V·古塔格
将接口与实现分离是提供抽象的一种方法
这是一个很好的例子。调用应用程序使用声明为接口的变量(引用),例如或,而实际使用的对象实际上是一个具体的类,例如或
如果接口提供了所有需要的功能,那么调用应用程序就不需要了解底层实现
问题中显示的示例代码是一个将接口与实现分离的示例,因此也是一个抽象的示例。如果该示例使用特定的有意义的上下文,例如BankAccount
作为接口,将SavingsAccount
和CheckingAccount
作为实现,则该示例将得到极大的改进。这是思考抽象的一种方式。没错;不是所有的。@duffymo你能帮我理解抽象中还包括哪些内容吗?你的机器上没有安装谷歌吗?你读了什么?你做了什么研究?好的,我会用谷歌搜索。谢谢你的建议。此外,我请求帮助,你没有给出t他回答说。当google提出问题时,人们甚至会问google推荐的stackoverflow。我认为当你可以查看函数名或API并理解它在做什么而不用担心它是如何做的时候,对象提供了封装和抽象。如果给我一个矩阵类,我可以理解它的add()是什么方法不必担心作者是如何实现它的。如果我使用java.util.List接口,我可以专注于它提供的方法,而不用担心它们是如何实现的。这是一种管理复杂性的方法。你能给我提供一篇文章或文章,简要说明抽象是如何实现的吗Java中的ct类和接口。我无法描述重写抽象方法如何提供抽象(隐藏不必要的细节,只提供有意义的内容)。我的理解可能不清楚。