Java 如何停止打印我的打印方法的一半
我只是需要一些帮助来停止打印方法。它将我的输出打印为car1.print()的两倍;car2.print();位于底部的打印方法中。如何排除此项而不删除它。它必须放在super.print()部分Java 如何停止打印我的打印方法的一半,java,class,inheritance,superclass,Java,Class,Inheritance,Superclass,我只是需要一些帮助来停止打印方法。它将我的输出打印为car1.print()的两倍;car2.print();位于底部的打印方法中。如何排除此项而不删除它。它必须放在super.print()部分 class车辆{//基本类 国际能力; 串制; 车辆(国际容量,品牌){ 容量=容量; make=theMake; } 作废打印(){ System.out.println(“车辆信息:”); System.out.println(“容量=“+capacity+”cc”); System.out.p
class车辆{//基本类
国际能力;
串制;
车辆(国际容量,品牌){
容量=容量;
make=theMake;
}
作废打印(){
System.out.println(“车辆信息:”);
System.out.println(“容量=“+capacity+”cc”);
System.out.println(“make=“+make”);
}
}
二等车{
公共字符串类型;
公共字符串模型;
公共汽车(容量、品牌、类型、型号){
超级(能力、品牌);
类型=类型;
模型=模型;
super.print();
{
System.out.println(“type=“+theType”);
System.out.println(“Model=“+theModel”);
}
}
}
班级任务1{
公共静态void main(字符串[]args){
Car car1=新车(1200辆,“霍顿”、“轿车”、“巴里纳”);
car2=新车(1500辆,“马自达”、“轿车”、“323”);
car1.print();
car2.print();
}
}
您可以在构造函数中使用super
关键字来调用超类的构造函数并向其传递参数。
请注意,它必须是构造函数中的第一条语句:
class Car extends Vehicle {
public String type;
public String model;
public Car(int theCapacity, String theMake, String theType, String theModel) {
super(theCapacity, theMake); // Here
type = theType;
model = theModel;
}
}
您缺少
构造函数
public Car (int theCapacity, String theMake, String theType, String theModel) {
capacity = theCapacity;
make = theMake;
Type = theType;
Model = theModel;
}
或
您必须通过在子类中传递简单的参数来调用超级构造函数
public Car(int capacity, String make, String type, String model) {
super(capacity, make); // simply call super
this.type = type;
this.model = model;
}
解决方案之一是使用super关键字从子类调用基类构造函数,并从子类构造函数添加其他参数,如@Mureinik所述 根据基类的要求,还可以尝试使用抽象方法。下面是示例代码
abstract class Vehicle {
static int capacity;
static String make;
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
protected static void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
// you can use these methods where you want in this base class.
System.out.println(" type = " + getType());
System.out.println(" model = " + getModel());
}
protected abstract String getType();
protected abstract String getModel();
}
public class Car extends Vehicle{
Car(int theCapacity, String theMake) {
super(theCapacity, theMake);
}
/**
* @param args
*/
public static void main(){
print();
}
@Override
protected String getType() {
// TODO Auto-generated method stub
return "Audi";
}
@Override
protected String getModel() {
// TODO Auto-generated method stub
return "Q7";
}
}
官方文件有一个例子:你必须更清楚你的问题
abstract class Vehicle {
static int capacity;
static String make;
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
protected static void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
// you can use these methods where you want in this base class.
System.out.println(" type = " + getType());
System.out.println(" model = " + getModel());
}
protected abstract String getType();
protected abstract String getModel();
}
public class Car extends Vehicle{
Car(int theCapacity, String theMake) {
super(theCapacity, theMake);
}
/**
* @param args
*/
public static void main(){
print();
}
@Override
protected String getType() {
// TODO Auto-generated method stub
return "Audi";
}
@Override
protected String getModel() {
// TODO Auto-generated method stub
return "Q7";
}
}