抽象类和方法,为什么呢?
问题说明
希望任何人都可以提供帮助,我正在学习Java,并且与该论坛的其他人一样,我想我也是编程的新手.
Hope anyone can help, i am learning Java and as comparable to anyone else in this forum i guess i am a newbie to programming as well.
我看过一章有关抽象类和方法的文章,但并没有真正完全理解它们的用途以及原因,并认为我会从经验丰富的程序员那里得到解释.
I have come across a chapter on abstract class and methods but don't really fully understand what they are used for and why, and thought i would get an explanation from someone who is an experienced programmer.
在下面的代码示例中,我一直在努力,在本书的帮助下,我不确定的是为什么在类Dims中,当每个子类都使用一个区域时,我是否必须具有抽象的double area()?无论如何,还是从超类中调用area方法,为什么必须要重写一些方法?
Below i have example code i have been working on, with the help from this book, what i am not sure about is why in the class Dims do i have to have abstract double area() when each sub class uses an area method anyway, or is it calling the area method from the super class, why do you have to have methods that override?
// Using abstract methods and classes
package Training2;
abstract class Dims {
double dim1;
double dim2;
Dims(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangles extends Dims {
Rectangles(double a, double b) {
super(a, b);
}
// Override area for Rectangles
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangles extends Dims {
Triangles(double a, double b) {
super(a, b);
}
// Override area for right Triangles
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 /2;
}
}
public class AbstractAreas {
public static void main(String args[]) {
// Dims d = new Dims(10, 10); // Illegal now
Rectangles r = new Rectangles(9, 5);
Triangles t = new Triangles(10, 8);
Dims dimref; // This is OK, no object is created
dimref = r;
System.out.println("Area is " dimref.area());
dimref = t;
System.out.println("Area is " dimref.area());}
}
道歉,但我确实需要一些指导.
Apologies for the waffling on but i really need some guidance.
谢谢.
正确答案
我们这样做是为了说该类具有完整的(ish) API ,但没有完整的实施."除其他外,这意味着您可以执行以下操作:
We do this to say "This class has a complete (ish) API, but it does not have a complete implementation." Among other things, it means that you can do this:
public void doSomething(Dims d) {
System.out.println("The area is " d.area());
}
// ...using it somewhere:
doSomething(new Triangle(4.0, 6.0));
在doSomething
中,即使对对象的引用是Dims
而不是Triangle
,我们也可以调用area
(并且最终会调用Triangle#area
),因为它是在Dims
API(有时称为合同").只是Dims
将实现推迟到子类中.这就是为什么您不能创建抽象类的实例的原因. doSomething
方法不知道给出的是Triangle
还是Rectangle
,只是它是某种的Dims
.
In doSomething
, even though the reference we have to the object is a Dims
, not a Triangle
, we can call area
(and we end up calling Triangle#area
) because it's defined in the Dims
API (sometimes called a "contract"). It's just that Dims
defers the implementation to subclasses. Which is why you can't create instances of abstract classes. The doSomething
method doesn't have any idea whether what it was given is a Triangle
or Rectangle
, just that it's some kind of Dims
.
如果Dims
没有定义area
,则doSomething
不会编译.我们必须声明一个doSomething
接受一个Triangle
,另一个声明一个接受Rectangle
,依此类推.不能对通用代码中的所有Dims
内容进行操作.
Whereas if Dims
didn't define area
, doSomething
wouldn't compile. We'd have to declare a doSomething
accepting a Triangle
and another one accepting a Rectangle
and so on. We couldn't get the benefit of being able to operate on all Dims
things in common code.
在Java中,接口和抽象类之间有很多交叉.从根本上讲,您可以将抽象类视为具有部分实现的接口(需要注意的是,一个类可以实现多个接口,但只能从单个抽象类派生).现在,接口可以定义方法的默认"实现,这条线变得更加模糊. (有些人认为接口现在可以具有默认方法,它们是新"抽象类,并且使真正的抽象类作为语言功能已过时,但是
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfiafe
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20