2014-04-29 18 views
0

我已经创建了子类RaceBoat,我需要在主要的方法是打印值的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

bump..anyone帮助? – user3558183

回答

0

问题是你有船内的赛船。您应该将赛船移至新文件。

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(); 
    } 
} 
+0

这样做的工作我不得不多做一些主,但它只是在超类内具有子类的问题。谢谢! – user3558183

相关问题