这个抽象类怎么写?
来源:百度知道 编辑:UC知道 时间:2024/05/22 00:33:45
public abstract class Ship {
/* Constants to represent the two possible orientations - DO NOT CHANGE. */
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
/**
* Ship constructor. Initialises the ship. Sets the name and length as
* specified by the respective parameters, and also initialises the ship
* so that it has not received any hits.
*/
public Ship(String name, int length) {
}
可能你还没弄清楚什么是抽象类吧!抽象类的一个特点就是无法实例化,创造这个类就是用来其他类来继承的。
抽象类,必须要用子类来实现!!还有一般不要去继承一个普通类!!
public abstract class Ship {
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
String name;
int length;
public Ship(String name, int length) {
this.name = name;
this.length = length;
}
}
1楼说的没错,抽象类不能实例化,但这并不能表示抽象类不能有属性,不能有构造方法,我刚写了段代码你看看,抽象类通常作为规范或是协议,下面这段代码我们就保证了batman肯定会有名字和年龄.(不过这种用法很怪,这里只是举个例子)
public class temp2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
batman a = new batman("aaa",12);
a.print();
}
}
abstract class man
{
String name;
int age;
man(String name,int age)
{
this.name = name;
this.age = age;
}
}
class batman extends man
{
Strin