Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 我有一个子类,需要用另一个方法调用它。这是说课堂上没有';不存在_Variables_Call_Subclass - Fatal编程技术网

Variables 我有一个子类,需要用另一个方法调用它。这是说课堂上没有';不存在

Variables 我有一个子类,需要用另一个方法调用它。这是说课堂上没有';不存在,variables,call,subclass,Variables,Call,Subclass,我已经创建了RaceBoat子类,我需要在main方法中调用它,这是一个用于打印值的演示程序。 具有构造函数的程序代码: //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 priv

我已经创建了RaceBoat子类,我需要在main方法中调用它,这是一个用于打印值的演示程序。 具有构造函数的程序代码:

//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(); 

}

}问题是你的船里面有一艘赛艇。你应该把赛艇移到一个新的档位

Boat.java

public class Boat {
    public Boat(){

    }

    public void display(){
        System.out.println("I'm a Boat");
    }
}
public class Raceboat extends Boat{
    public Raceboat(){

    }

    public void display(){
        System.out.println("I'm a Raceboat");
    }
}
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();
    }
}
Raceboat.java

public class Boat {
    public Boat(){

    }

    public void display(){
        System.out.println("I'm a Boat");
    }
}
public class Raceboat extends Boat{
    public Raceboat(){

    }

    public void display(){
        System.out.println("I'm a Raceboat");
    }
}
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();
    }
}
Main.java

public class Boat {
    public Boat(){

    }

    public void display(){
        System.out.println("I'm a Boat");
    }
}
public class Raceboat extends Boat{
    public Raceboat(){

    }

    public void display(){
        System.out.println("I'm a Raceboat");
    }
}
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();
    }
}

这确实有效,我必须对main做更多的工作,但这只是在超类中包含子类的问题。非常感谢。