java 接口抽象方法不指定主体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17091206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
interface abstract methods do not specify a body
提问by user2482844
im tring to make an interface that would serve as a short cut for System.out.print(); but when i compile i get abstract methods do not specify a body
and i have no clue what this means
我正在尝试制作一个接口,作为 System.out.print() 的捷径;但是当我编译时,我得到了abstract methods do not specify a body
,我不知道这是什么意思
public interface Printing{
public abstract void prt(String print,boolean line){
if(line=true) {
System.out.println(print);
}
}
public abstract void prt(String print){
System.out.print(print);
}
}
回答by fge
Methods declared in an interface
are automatically public
and abstract
. So you can start by ditching these two modifiers.
在 aninterface
中声明的方法是自动public
和abstract
. 所以你可以从放弃这两个修饰符开始。
And abstract methods, by definition, do not have a body... So maybe an interface is not what you are looking for here.
抽象方法,根据定义,没有主体......所以也许接口不是你在这里寻找的东西。
If you DO NOT want to be able to instantiate your Printing
but want "default implementations", use an abstract
classwhich provides the base implementation for these two methods:
如果您不想能够实例化您Printing
但想要“默认实现”,请使用一个为这两种方法提供基本实现的abstract
类:
public abstract class Printing
{
public void ptr(String print, boolean line) {
// do stuff
}
public void ptr(String print) {
// do stuff
}
}
Implementations will then have to extends Printing
and @Override
the default methods if they want to.
那么实现将不得不extends Printing
和@Override
默认的方式,如果他们想。
回答by Tudor Zgureanu
An Interface is a contract, you just specify the methods that should be implemented. An abstract method doesn't contain any body, just signature.
接口是一种契约,您只需指定应该实现的方法。抽象方法不包含任何主体,只包含签名。
回答by John B
make this an abstract class instead of an interface.
使其成为抽象类而不是接口。
Interfaces may not have ANY implementation of the methods. So the only thing you can have is.
接口可能没有任何方法的实现。所以你唯一能拥有的就是。
void prt(String print,boolean line);
void prt(String print);
回答by Prasad Kharkar
Abstract methods never specify the body. Thats why they are made abstrat. If you are creating methods in interface, they will always be public and abstract.
抽象方法从不指定主体。这就是为什么它们是抽象的。如果您在接口中创建方法,它们将始终是公共和抽象的。
Maybe in this case you want to write an abstract class and write one method with implementation and another as abstract one.
也许在这种情况下,您想编写一个抽象类并编写一个具有实现的方法,另一个作为抽象方法。
回答by grepit
Interfaces are like empty shells and members are not implemented.
接口就像空壳,成员没有实现。
Here is what you need it to change your code to:
这是您将代码更改为所需的内容:
void prt(String print,boolean line);
void prt(String print);
http://docs.oracle.com/javase/tutorial/java/concepts/interface.htmlhttp://csis.pace.edu/~bergin/papers/Interface.html
http://docs.oracle.com/javase/tutorial/java/concepts/interface.html http://csis.pace.edu/~bergin/papers/Interface.html
回答by fz_salam
You can notspecify a body to abstract methods. Abstract methods means they are incomplete.
您不能为抽象方法指定主体。抽象方法意味着它们是不完整的。
I guess you need to understand more about interfaces and abstract classes.
我想您需要更多地了解接口和抽象类。
To understand what Interfacesand Abstract Classesmean in Java or any other object oriented programming languageyou should first understand inheritance.
要了解接口和抽象类在 Java 或任何其他面向对象的编程语言中的含义,您应该首先了解继承。
Inheritance
遗产
Consider a car and a bus. They are two different vehicles. But still they share some common properties like they have a steering, brakes, gears, engine etc.
考虑汽车和公共汽车。它们是两种不同的车辆。但是它们仍然具有一些共同的特性,例如它们具有转向、制动器、齿轮、发动机等。
So with the inheritance concept this can be represented as following..
因此,使用继承概念,这可以表示如下..
public class Vehicle {
public Driver driver;
public Seat[] seatArray; //In java and most of the Object Oriented Programming(OOP) languages, square brackets are used to denote arrays(Collections).
//You can define as many properties as you want here..
}
Now a Bicycle..
现在是自行车..
public class Bicycle extends Vehicle {
//You define properties which are unique to bicycles here..
public Pedal pedal;
}
And a Car..
还有车..
public class Car extends Vehicle {
public Engine engine;
public Door[] doors;
}
That's all about Inheritance. We use them to classify objects into simpler Base forms and their children as we saw above.
这就是继承的全部内容。我们使用它们将对象分类为更简单的基本形式及其子级,如上所示。
Abstract Classes
抽象类
Abstract classes are incompleteobjects. To understand it further, let's consider the vehicle analogy once again.
A vehicle can be driven. Right? But different vehicles are driven in different ways.. For example, You cannot drive a car just as you drive a Bicycle.
So how to represent the drive function of a vehicle? It is harder to check what type of vehicle it is and drive it with its own function; you would have to change the Driver class again and again when adding a new type of vehicle.
Here comes the role of abstract classes and methods. You can define the drive method as abstract to tell that every inheriting children must implement this function.
So if you modify the vehicle class..
抽象类是不完整的对象。为了进一步理解它,让我们再次考虑车辆类比。
可以驾驶车辆。对?但是不同的车辆以不同的方式驾驶。例如,您不能像驾驶自行车一样驾驶汽车。
那么如何表示车辆的驱动功能呢?更难检查它是什么类型的车辆并以其自身的功能驱动它;添加新类型的车辆时,您将不得不一次又一次地更改 Driver 类。
这里是抽象类和方法的作用。您可以将驱动方法定义为抽象方法,以告诉每个继承的孩子都必须实现此功能。
因此,如果您修改车辆类别..
//......Code of Vehicle Class
abstract public void Drive();
//.....Code continues
The Bicycle and Car must also specify how to drive it. Otherwise the code won't compile and an error is thrown.
自行车和汽车还必须指定如何驾驶它。否则代码将无法编译并抛出错误。
In short.. an abstract class is a partially incomplete class with some incomplete functions, which the inheriting children must specify their own.
简而言之..抽象类是一个部分不完整的类,具有一些不完整的功能,继承的孩子必须指定他们自己的。
Interfaces
接口
Interfaces are totally incomplete. They do not have any properties. They just indicate that the inheriting children is capable of doing something..
接口完全不完整。他们没有任何属性。它们只是表明继承的孩子有能力做某事。
Suppose you have different types of mobile phones with you. Each of them have different ways to do different functions; Ex: call a person. The maker of the phone specifies how to do it. Here the mobile phones can dial a number - that is, it is dial-able. Let's represent this as an interface.
假设你有不同类型的手机。他们每个人都有不同的方式来完成不同的功能;例如:打电话给一个人。手机制造商指定了如何操作。在这里,手机可以拨打一个号码——也就是说,它是可拨号的。让我们将其表示为一个接口。
public interface Dialable {
public void Dial(Number n);
}
Here the maker of the Dialable defines how to dial a number. You just need to give it a number to dial.
Dialable 的制造者在这里定义了如何拨打号码。你只需要给它一个号码就可以拨打。
Dialable myPhone1 = new Dialable() {
public void Dial(Number n) {
//Do the phone1's own way to dial a number
}
}
Dialable myPhone2 = new Dialable() {
public void Dial(Number n) {
//Do the phone2's own way to dial a number
}
}
Here by using interfaces instead of abstract classes, you need not worry about it's properties. Ex: Does it have a touch-screen or dial pad, Is it a fixed landline phone or mobile phone. You just need to know if it is dialable. Does it inherit(or implement) the Dialable interface.
在这里通过使用接口而不是抽象类,您不必担心它的属性。例如:是否有触摸屏或拨号盘,是固定座机电话还是移动电话。您只需要知道它是否可拨号。它是否继承(或实现)了 Dialable 接口。
Interfaces are commonly used by developers to ensure interoperability between objects, as far as they share a common function (just like you may change to a landline or mobile phone, as far as you just need to dial a number). In short, interfaces are much simpler version of abstract classes, without any properties.
开发人员通常使用接口来确保对象之间的互操作性,只要它们共享一个共同的功能(就像您可以更改为座机或移动电话,只要您只需要拨打一个号码)。简而言之,接口是抽象类的更简单版本,没有任何属性。
Also note that that you may implement(inherit) as many interfaces as you want but you may only extend(inherit) a single parent class.
另请注意,您可以根据需要实现(继承)任意数量的接口,但您只能扩展(继承)单个父类。
More Info
更多信息
Abstract classes vs Interfaces
Another similar question in Stackoverflow
回答by fz_salam
If you just mean that to add a shortcut to the System.out.println() method,
There is no need to add the abstract keyword. Also change the interface into a class.
如果你只是想给 System.out.println() 方法添加一个快捷方式,
就没有必要添加abstract关键字了。还将接口更改为一个类。
public class Printing{
public void prt(String print,boolean line){
if(line=true) {
System.out.println(print);
} else {
System.out.print(print);
}
}
public void prt(String print){
System.out.print(print);
}
}
But here you first need to create an instance of Printing object. Just like:
但是这里首先需要创建一个Printing 对象的实例。就像:
Printing p = new Printing();
//Now you may call the functions on p.
p.prt("Without Line Terminator");
p.prt("Also Without Line Terminator",false);
p.prt("With Line Terminator",true);
If you need to call the functions without instantiating it, Just like Printing.prt("sample")
instead of Printing p = new Printing(); p.prt("sample")
如果您需要在不实例化的情况下调用函数,就像Printing.prt("sample")
代替Printing p = new Printing(); p.prt("sample")
You need to define prt() method as static.
您需要将 prt() 方法定义为静态方法。
public class Printing{
public static void prt(String print,boolean line){
if(line=true) {
System.out.println(print);
} else {
System.out.print(print);
}
}
public static void prt(String print){
System.out.print(print);
}
}
Now call..
现在打电话..
//You may call the functions on Printing itself.
Printing.prt("Without Line Terminator");
Printing.prt("Also Without Line Terminator",false);
Printing.prt("With Line Terminator",true);