如何以及何时使用抽象类
这是我用Java编写的测试程序。我想知道抽象类在这里有多重要以及为什么我们为此使用抽象类。
这是强制性的还是最好的方法;如果是这样怎么办?
class Shape1 {
int i = 1;
void draw() {
System.out.println("this is shape:" + i);
}
}
class Shape2 {
int i = 4;
void draw() {
System.out.println("this is shape2:" + i);
}
}
class Shape {
public static void main(String args[]) {
Shape1 s1 = new Shape1();
s1.draw();
Shape2 s2 = new Shape2();
s2.draw();
}
}
This is my test program in Java. I want to know how much abstract class is more important here and why we use abstract class for this.
Is it a mandatory or is it best method; if so how?
class Shape1 {
int i = 1;
void draw() {
System.out.println("this is shape:" + i);
}
}
class Shape2 {
int i = 4;
void draw() {
System.out.println("this is shape2:" + i);
}
}
class Shape {
public static void main(String args[]) {
Shape1 s1 = new Shape1();
s1.draw();
Shape2 s2 = new Shape2();
s2.draw();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以在此处使用抽象类或接口,以便创建提供
void draw()
方法的公共基类/接口,例如我通常会使用接口。但是,如果满足以下条件,您可以使用抽象类:
int i
成员)。void draw()
将具有包可见性。You'd use an abstract class or interface here in order to make a common base class/interface that provides the
void draw()
method, e.g.I'd generally use an interface. However you might use an abstract class if:
int i
member in your case).void draw()
would have package visibility.抽象类是至少有一个未实现的方法或关键字abstract 的类。例如,一个抽象方法可能如下所示:
public abstract String myMethod(String input);
(注意该方法以分号结尾)。
以及一个类可能看起来像这样:
抽象类无法实例化。抽象类需要一个子类来实现缺失的行为,以便可以实例化它。
抽象类的主要目标是提供公共行为的共享实现 - 促进代码的重用。
在 Java 中,通过使用类的组合而不是从广泛定义的抽象继承,可以实现相同的效果类。这允许更多模块化、功能特定的类促进代码重用,从而提高可维护性。
我的建议是仅在绝对必要时才使用抽象类,特别是避免将其用作充满各种功能的技巧包。
在 Scala 中,人们会使用特征,这是解决这个问题的一种优雅的方式。然而,它确实需要大量的关注才能顺利通过。
编辑:从 Java 8 开始,接口中的默认方法是添加常见行为的另一种方法。
An abstract class is a class, which has at least one method not implemented, or the keyword abstract. For example, an abstract method may look like this:
public abstract String myMethod(String input);
(note that the method ends with a semi-colon).
And a class may look like this:
An abstract class cannot be instantiated. Abstract classes require a subclass to implement the missing behaviour so that it can be instantiated.
The main goal of an abstract class is to provide shared implementation of common behaviour - promoting the reuse of code.
In Java the same effect can be achieve by using a composition of classes instead of inheritance from broadly defined abstract classes. This allows more modular, function specific classes promoting code reuse, that in turn increase maintainability.
My advice would be to use abstract class only when strictly necessary, and in particular avoid using it as a trick bag full of all sorts of functionality.
In Scala one would use traits, which are an elegant way to solve this. It does however, require a lot of attention to get it right through.
Edit: Starting Java 8, default methods in interface are another way to add common behavior.
抽象类与您的子类具有“is-a”类型关系。例如,您可以有一个抽象类
Shape
,其中包含任何形状所具有的内容(如绘图函数),然后是一个类SquareShape
。每个正方形都是一个形状,但并非所有形状都是正方形。在您的示例中,您有 2 个形状类型,以及一个具有 2 个实例的类。我认为这不是你应该用
abstract
定义的关系。不过,您可能想对您的名字做一些事情,因为这是一个相当小的示例,很难看出这些文件的真正含义以及它们应该如何工作。
An abstract class has an "is-a" type relationship with your subclasses. So for instance, you could have an abstract class
Shape
which has stuff any shape has (like a draw function), and then a classSquareShape
. Every squareshape is a shape, but not all shapes are squareshapes.In you example you have 2 shape-types, and a class that has 2 instances of these. That is not a relationship you should define with
abstract
I think.You might want to do something with your names though, because this is a rather small example, it's hard to see the real implications of the files, and how they should work.
常见的东西+不常见的东西=抽象类
抽象类最适合有大量可重用代码的场景,您不想一次又一次地编写这些代码+每个类都有一些特定的内容。
仪式:
常见的事情:唱国歌、升旗等
具体内容:印度国旗、印度国歌(印度)、中国国旗、中国国歌(中国)等
1)创建抽象类
2)将所有内容放在常用的公共方法中
3)将所有内容放入特定于子类的抽象方法中
基类:
在子类中,您只需提供不常见部分的实现即可。
ChildClass
优点
Common things + Uncommon things = Abstract class
An abstract class is best suited for the scenarios where there is a lot of reusable code which you don't want to write again and again + There are few things which are specific to each class.
Ceremony:
Common things: Sing Anthem, Hoist Flag etc
Specific things: Indian Flag, Indian anthem(For India), China Flag, China anthem(For China) etc.
1) Create an abstract class
2) Put everything in public methods which are common
3) Put everything in abstract methods which are specific to child classes
Base class:
In the child class, you just have to provide the implementation of the uncommon part.
ChildClass
Bonus
在 java 中使用抽象类的示例。
sample example for using abstract class in java.
抽象类,顾名思义就是抽象的。
抽象类不讲实现部分。事实上,我们扩展了抽象类以提供其抽象方法的实现。它也可以有非抽象方法。
检查此处:在 Java 中使用抽象类
编辑:
示例代码:
Abstract class, as its name suggest is abstract.
Abstract class doesn't talk about the implementation part. In fact, we extend the abstract class to provide the implementation for its abstract methods. It can also have non-abstract methods.
Check here : Use of Abstract Class in Java
EDIT :
Sample Code :