我创建了子类RaceBoat,我需要在main方法中调用它,这是Demo程序来打印值。 带有构造函数的程序代码:
//Values Based on a CAL 29 boat
公共班船{
private int boatLength; //29 feet
private double boatBeam; //9.25 feet
private int waterlineLength; //24 feet
private double boatDraft; //4.50 feet
private int mastHeight; //41 feet
private String boatType;
private String boatName;
private int boatPrice;
public class RaceBoat extends Boat {
int boatPrice;
public RaceBoat(int boatLength, double boatBeam, int waterlineLength,
double boatDraft, int mastHeight, String boatType,
String boatName, int boatPrice)
{
super(boatLength, boatBeam, waterlineLength, boatDraft, mastHeight, boatType,
boatName);
this.boatPrice = boatPrice;
}
public RaceBoat()
{
}
public void display()
{
System.out.println("The length of the boat is " + boatLength + " feet.");
System.out.println("The width of the boat is " + boatBeam + " feet.");
System.out.println("The length of the part of the boat that is under water is " + waterlineLength + "feet.");
System.out.println("The depth of the hull is " + boatDraft + " feet.");
System.out.println("The height of the mast is " + mastHeight + " feet.");
System.out.println("This boat is a " + boatType);
System.out.println("The name of this boat is " + boatName);
System.out.println("The price of this boat is $" + boatPrice + "dollars.");
}
}
public Boat(int boatLength, double boatBeam, int waterlineLength, double boatDraft, int mastHeight, String boatType, String boatName) {
super();
this.boatLength = boatLength;
this.boatBeam = boatBeam;
this.waterlineLength = waterlineLength;
this.boatDraft = boatDraft;
this.mastHeight = mastHeight;
this.boatType = boatType;
this.boatName = boatName;
}
public Boat()
{
}
public void setPrice (int pri)
{
boatPrice = pri;
}
public void setLength (int len)
{
boatLength = len;
}
public void setWidth (double wid)
{
boatBeam = wid;
}
public void setWll (int wll)
{
waterlineLength = wll;
}
public void setDraft (double draft)
{
boatDraft = draft;
}
public void setMast (int mast)
{
mastHeight = mast;
}
public void setType (String type)
{
boatType = type;
}
public void setName (String name)
{
boatName = name;
}
public int getLength (int len)
{
return boatLength;
}
public double getWidth (double wid)
{
return boatBeam;
}
public int getWll (int wll)
{
return waterlineLength;
}
public double getDraft (double draft)
{
return boatDraft;
}
public int getMast (int mast)
{
return mastHeight;
}
public String getType (String type)
{
return boatType;
}
public String getName (String name)
{
return boatName;
}
public int getPrice (int pri)
{
return boatPrice;
}
public void display()
{
System.out.println("The length of the boat is " + boatLength + " feet.");
System.out.println("The width of the boat is " + boatBeam + " feet.");
System.out.println("The length of the part of the boat that is under water is " + waterlineLength + "feet.");
System.out.println("The depth of the hull is " + boatDraft + " feet.");
System.out.println("The height of the mast is " + mastHeight + " feet.");
System.out.println("This boat is a " + boatType);
System.out.println("The name of this boat is " + boatName);
}
}
演示代码是:
公共课演示{
public static void main (String[] args)
{
Boat boat1 = new Boat();
Boat boat2 = new Boat(29, 9.25, 24, 4.50, 41, "sailboat", "CAL 29");
System.out.println("-----Boat1-----");
System.out.println();
boat1.display();
System.out.println();
System.out.println("-----Boat2-----");
System.out.println();
boat2.display();
RaceBoat boat3 = new RaceBoat();
}
}
答案 0 :(得分:0)
问题是你在Boat里面有Raceboat。您应该将Raceboat移动到新文件。
Boat.java
public class Boat {
public Boat(){
}
public void display(){
System.out.println("I'm a Boat");
}
}
Raceboat.java
public class Raceboat extends Boat{
public Raceboat(){
}
public void display(){
System.out.println("I'm a Raceboat");
}
}
和Main.java
public class main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Boat boat = new Boat();
boat.display();
Boat raceboat = new Raceboat();
raceboat.display();
}
}