Java中的“抽象类”是什么?
当前回答
来自oracle文档
抽象方法和类:
抽象类是声明为抽象的类——它可以包含也可以不包含抽象方法 抽象类不能被实例化,但可以被子类化
抽象方法是一种声明时没有实现(没有大括号,后面跟着一个分号)的方法,如下所示:
abstract void moveTo(double deltaX, double deltaY);
如果一个类包含抽象方法,那么类本身必须声明为抽象的,如下所示:
public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}
当抽象类被子类化时,子类通常为其父类中的所有抽象方法提供实现。然而,如果不是这样,那么子类也必须声明为抽象的。
由于抽象类和接口是相关的,看看下面的SE问题:
接口和抽象类的区别是什么?
我应该如何解释接口和抽象类之间的区别?
其他回答
解决方案——基类(抽象)
public abstract class Place {
String Name;
String Postcode;
String County;
String Area;
Place () {
}
public static Place make(String Incoming) {
if (Incoming.length() < 61) return (null);
String Name = (Incoming.substring(4,26)).trim();
String County = (Incoming.substring(27,48)).trim();
String Postcode = (Incoming.substring(48,61)).trim();
String Area = (Incoming.substring(61)).trim();
Place created;
if (Name.equalsIgnoreCase(Area)) {
created = new Area(Area,County,Postcode);
} else {
created = new District(Name,County,Postcode,Area);
}
return (created);
}
public String getName() {
return (Name);
}
public String getPostcode() {
return (Postcode);
}
public String getCounty() {
return (County);
}
public abstract String getArea();
}
它什么都不做,只是提供一个公共模板,它将被它的子类共享
简单地说,您可以将抽象类看作是具有更多功能的接口。
你不能实例化一个接口,这也适用于一个抽象类。
On your interface you can just define the method headers and ALL of the implementers are forced to implement all of them. On an abstract class you can also define your method headers but here - to the difference of the interface - you can also define the body (usually a default implementation) of the method. Moreover when other classes extend (note, not implement and therefore you can also have just one abstract class per child class) your abstract class, they are not forced to implement all of your methods of your abstract class, unless you specified an abstract method (in such case it works like for interfaces, you cannot define the method body).
public abstract class MyAbstractClass{
public abstract void DoSomething();
}
否则,对于抽象类的普通方法,“继承者”可以像往常一样只使用默认行为或重写它。
例子:
public abstract class MyAbstractClass{
public int CalculateCost(int amount){
//do some default calculations
//this can be overriden by subclasses if needed
}
//this MUST be implemented by subclasses
public abstract void DoSomething();
}
在这里找到你的答案:
Java中的抽象类与接口
抽象类可以有final方法吗?
顺便说一句,这是你最近问的问题。思考一个建立声誉的新问题……
编辑:
刚刚意识到,这个和参考问题的海报有相同或至少相似的名称,但用户id总是不同的。所以,要么,有一个技术问题,键盘有问题再次登录,并找到他的问题的答案,或者这是一种娱乐So社区的游戏;)
抽象类不能直接实例化,但必须从抽象类派生才能使用。一个类必须是抽象的,如果它包含抽象方法:或者直接
abstract class Foo {
abstract void someMethod();
}
或间接地
interface IFoo {
void someMethod();
}
abstract class Foo2 implements IFoo {
}
然而,一个类可以是抽象的,而不包含抽象方法。这是一种防止直接瞬化的方法,例如。
abstract class Foo3 {
}
class Bar extends Foo3 {
}
Foo3 myVar = new Foo3(); // illegal! class is abstract
Foo3 myVar = new Bar(); // allowed!
抽象类的后一种风格可以用来创建“类接口”类。与接口不同,抽象类允许包含非抽象方法和实例变量。您可以使用它为扩展类提供一些基本功能。
另一种常见的模式是在抽象类中实现主要功能,并在由扩展类实现的抽象方法中定义部分算法。愚蠢的例子:
abstract class Processor {
protected abstract int[] filterInput(int[] unfiltered);
public int process(int[] values) {
int[] filtered = filterInput(values);
// do something with filtered input
}
}
class EvenValues extends Processor {
protected int[] filterInput(int[] unfiltered) {
// remove odd numbers
}
}
class OddValues extends Processor {
protected int[] filterInput(int[] unfiltered) {
// remove even numbers
}
}
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap