Java中的“抽象类”是什么?
当前回答
什么是抽象类? 好的!让我们举个例子,你对化学了解不多,我们有一个元素碳(符号C)。碳有一些基本的原子结构,你不能改变,但用碳你可以制造很多化合物,如(CO2),甲烷(CH4),丁烷(C4H10)。
所以这里碳是抽象类,你不想改变它的基本结构,但你想让他们的孩子(CO2,CH4等)使用它。但是以他们自己的方式
其他回答
抽象类不能直接实例化,但必须从抽象类派生才能使用。一个类必须是抽象的,如果它包含抽象方法:或者直接
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
}
}
解决方案——基类(抽象)
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();
}
类,它可以有具体方法和非具体方法,即有主体和没有主体。
没有实现的方法必须包含'abstract'关键字。 抽象类不能被实例化。
推荐文章
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- Java中的操作符重载
- 如何加速gwt编译器?
- 在Hibernate中重新连接分离对象的正确方法是什么?
- 应该……接住环内还是环外?
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?
- 如何在POM.xml中引用环境变量?