首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在接口的子类中实现方法

无法在接口的子类中实现方法
EN

Stack Overflow用户
提问于 2013-04-14 18:42:01
回答 3查看 2.9K关注 0票数 0

我知道这是家庭作业,所以这听起来可能有点奇怪。现在,我正在尝试摆脱编译错误,说池必须实现抽象方法。Pool是由BackYard接口实现的,而deck是pool的子类,而护柱是deck的子类。我不允许更改driver类中显示输出方法中的代码,也不允许更改套装或系桩中的代码。编译器一直坚持让我重新编码pool中的所有子类方法,或者让pool成为抽象方法,这两种方法我都做不到。我到底需要解决什么问题。如果我真的需要在后院接口中编写所有的get方法,也请让我知道

下面是驱动程序类:

代码语言:javascript
复制
public class YardOrders 
{
        //Constants
        final static int POOL_ONLY = 1;
        final static int POOL_N_DECK=2;
        final static int POOL_DECK_N_BOLLARD=3;
        final static int DISPLAY_ORDERS=4;
      final static int DEFAULT_INT=0;

      //Methods
        public static void main(String[] args) 
        {
            int numberOfOrders=DEFAULT_INT;
            BackYard backYard[] = new BackYard[100];
            int selection = DEFAULT_INT;
             do
             {
                 selection = Integer.parseInt(JOptionPane.showInputDialog(null,       
                            "Options:\nEnter "+ POOL_ONLY +" for a pool.\n" +                               
                            "Enter "+ POOL_N_DECK +
                            " for a pool and a concrete " +
                            "deck surrounding the pool.\n"+
                            "Enter "+POOL_DECK_N_BOLLARD+" for a pool," +
                            " deck, and bollards.\n"+
                            "Enter "+DISPLAY_ORDERS+" to display orders and exit.",
                            "Pool Options", JOptionPane.PLAIN_MESSAGE));
                    if(selection > DEFAULT_INT && selection < DISPLAY_ORDERS)
                    {

                            getPoolInput(backYard,numberOfOrders,selection);
                            numberOfOrders++;
                            System.out.println(numberOfOrders);
                    }
                    else if(selection==DISPLAY_ORDERS)
                    {
                        displayOrders(backYard,numberOfOrders);
                        System.out.println(numberOfOrders);
                        System.exit(DEFAULT_INT);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null,"Invalid input. Values" +
                    " must be between 1 and 4.");
                    }
             }while(selection != DISPLAY_ORDERS);                                                   
        }     

        private static void getPoolInput(BackYard backYard[],int numberOfOrders,int selection)
        {
            //Pool attributes
            String lastName = JOptionPane.showInputDialog(null,
                    "Enter last name.\n","Last Name",
                    JOptionPane.PLAIN_MESSAGE);

            String firstName = JOptionPane.showInputDialog(null,
                    "Enter first name.","First Name",
                    JOptionPane.PLAIN_MESSAGE);

            double poolDepth = Double.parseDouble(
                    JOptionPane.showInputDialog(null,
                    "Enter pool depth in inches.","Pool Depth",
                    JOptionPane.PLAIN_MESSAGE)); //In inches.

            double poolDiameter = Double.parseDouble(
                    JOptionPane.showInputDialog(null,
                    "Enter pool diameter in feet.","Pool Diameter",
                    JOptionPane.PLAIN_MESSAGE));//In feet.
            if(selection == POOL_ONLY)
            {
              //Pool instantiation.

                backYard[numberOfOrders]= new Pool(lastName,firstName,
                            poolDepth,poolDiameter);
            }
            else
            {
                getDeckInput(backYard, 
                                numberOfOrders,selection,
                                        lastName,firstName,
                                        poolDepth, poolDiameter);
            }

        }//End of method

        private static void getDeckInput(BackYard[] backYard, 
                                                                         int numberOfOrders, int selection,
                                                                        String lastName, String firstName,
                                                                         double poolDepth, double poolDiameter)
        {
            //Deck attributes
            double deckLength=Double.parseDouble(
                    JOptionPane.showInputDialog(null,
                    "Enter deck length in feet.","Deck Length",
                    JOptionPane.PLAIN_MESSAGE));

            double deckWidth= Double.parseDouble(
                    JOptionPane.showInputDialog(null,
                    "Enter deck width in feet.","Deck Width",
                    JOptionPane.PLAIN_MESSAGE));
            if(selection==POOL_N_DECK)
            {

                backYard[numberOfOrders]= new Deck(lastName,firstName,
                                                                    poolDepth,poolDiameter,
                                                             deckLength,deckWidth);

            }
            else
            {
                getBollardInput(lastName,firstName,
                                                poolDepth,poolDiameter,
                                                deckLength,deckWidth);
            }
        }
        public static void getBollardInput(String lastName, String firstName,
                                                                         double poolDepth, double poolDiameter,
                                                                         double deckLength, double deckWidth)
        {
            //Bollard attributes
            double bollardHeight=Double.parseDouble(
                JOptionPane.showInputDialog(null,
                "Enter bollard height in inches.","Bollard Height",
                JOptionPane.PLAIN_MESSAGE));

            double bollardDiameter=Double.parseDouble(
                JOptionPane.showInputDialog(null,
                "Enter bollard diameter in incehs.","Bollard Diameter",
                JOptionPane.PLAIN_MESSAGE));

            int numberOfBollards=Integer.parseInt(
                JOptionPane.showInputDialog(null,
                "Enter the number of bollards.","Number of bollards",
                JOptionPane.PLAIN_MESSAGE));

            //Bollard instantiation
            Bollards bollards= new Bollards(lastName,firstName,
                    poolDepth,poolDiameter,
                    deckLength,deckWidth,
                    bollardHeight, bollardDiameter,
                    numberOfBollards);
        }

        private static void displayOrders(BackYard[] orders, int numberOfOrders)
        {
            DecimalFormat dec3 = new DecimalFormat("0.000");
            String divider = "******************************************************" +
                                             "***********\n";
            JTextArea textOut = new JTextArea(divider, 10, 30);
            JScrollPane scroller = new JScrollPane(textOut);

            for(int sub = 0; sub < numberOfOrders; sub++)
            {
                textOut.append("Customer Name: " + orders[sub].getLastName() + ", ");
                textOut.append(orders[sub].getFirstName() + "\n");
                textOut.append("Pool Depth:" +
                                            dec3.format(orders[sub].getInsideDepth()) + "\n");
                textOut.append("Pool Diameter: "+
                                            dec3.format(orders[sub].getInsideDiameter()) + "\n");
                textOut.append("Deck Width: " +
                                            dec3.format(orders[sub].getDeckWidth()) + "\n");
                textOut.append("Deck Length: " +
                                                dec3.format(orders[sub].getDeckLength()) + "\n");
                textOut.append("Number of Bollards Ordered: " +
                                                orders[sub].getNumberOfBollards() + "\n");
                textOut.append("Height of Bollards: " +
                                                dec3.format(orders[sub].getBollardHeight()) + "\n");
                textOut.append("Diameter of Bollards: " +
                                            dec3.format(orders[sub].getBollardDiameter()) + "\n");
                textOut.append("Cubic Yards of Concrete Needed: " +
                                            dec3.format(orders[sub].getConcreteVolume()) + "\n");
                textOut.append(divider);
            } // end for loop
            JOptionPane.showMessageDialog(null, scroller, "Orders Placed",
                                                                        JOptionPane.PLAIN_MESSAGE);
        } // end method DisplayOrders*/


}

下面是BackYard接口:

代码语言:javascript
复制
public interface BackYard 
{
    //Universal constants 
        public static final int CU_IN_TO_CU_YD = 46656;
        public static final int FT_TO_IN = 12;
        public static final double DENSITY = 3.75; // in inches

        //Pool constants. 
        public static final String DEFAULT_NAME = "Unknown"; 
        public static final int DEFAULT_DIAM_DEPTH = 0;
        public static final int STANDARD_DEPTH = 24; // in inches
        public static final int STANDARD_DIAMETER = 6; // in feet
        public static final int MIN_DEPTH = 10; // in inches
        public static final int MAX_DEPTH = 72; // in inches
        public static final int MIN_DIAMETER = 3; // in feet
        public static final int MAX_DIAMETER = 25; // in feet

        //Deck constants
        public final static double MAX_DECK_LENGTH = 50.0; // in feet
        public static final double MAX_DECK_WIDTH = 50.0;  // in feet
        public static final int DEFAULT_WIDTH_AND_LENGTH = 0;

      //Bollard constants
        public static final double MAX_BOLLARD_HEIGHT = 60.0;  // in inches
        public static final double MIN_BOLLARD_HEIGHT = 24.0;  // in inches
        public static final double MAX_BOLLARD_DIAMETER = 18.0;  // in inches
        public static final double MIN_BOLLARD_DIAMETER = 3.0;  // in inches
        public static final int MIN_NUMBER_OF_BOLLARDS = 4; // units

        //Methods.
        public abstract String getLastName();
        public abstract String getFirstName();
        public abstract double getInsideDepth();
        public abstract double getInsideDiameter();
        public abstract  double getDeckWidth();
        public abstract double getDeckLength();
      public abstract int getNumberOfBollards();
      public abstract double getBollardHeight();
      public abstract double getBollardDiameter();
        public abstract double getConcreteVolume();
}

这是池类

代码语言:javascript
复制
public class Pool implements BackYard
{
    // instance variable(s)
    private double insideDiameter; // in feet
    private double insideDepth; // in inches
    private String lastName;
    private String firstName;

    // class variable(s)
    public static int numberOfOrders;

    // Zero argument constructor.  Sets instance variables to default values
    public Pool()   
    {
        setInsideDiameter(DEFAULT_DIAM_DEPTH);
        setInsideDepth(DEFAULT_DIAM_DEPTH);
        setLastName(DEFAULT_NAME);
        setFirstName(DEFAULT_NAME);
    }

    // Two parameter constructor.  
    // Sets names to input values and measurements to standard values
    public Pool(String lastNameIn, String firstNameIn)
    {
        setInsideDiameter(STANDARD_DIAMETER);
        setInsideDepth(STANDARD_DEPTH);
        setLastName(lastNameIn);
        setFirstName(firstNameIn);
        numberOfOrders++;
    }   

    // Three parameter constructor.  
    // Sets names and depth to input values and diameter to standard value
    public Pool(String lastNameIn, String firstNameIn, double depthIn)
    {
        setInsideDiameter(STANDARD_DIAMETER);
        setInsideDepth(depthIn);
        setLastName(lastNameIn);
        setFirstName(firstNameIn);
        numberOfOrders++;
    }   

    // Three parameter constructor.  
    // Sets all instance variables to input values
    public Pool(String lastNameIn, String firstNameIn, double depthIn, 
        double diameterIn)
    {
        setInsideDiameter(diameterIn);
        setInsideDepth(depthIn);
        setLastName(lastNameIn);
        setFirstName(firstNameIn);
        numberOfOrders++;
    }   

    // returns depth
    public double getInsideDepth()
    {
        return insideDepth;
    }

    // validates input and sets depth
    public void setInsideDepth(double inDepth)
    {
        insideDepth =  ((inDepth >= MIN_DEPTH && 
                    inDepth <= MAX_DEPTH) ? inDepth : DEFAULT_DIAM_DEPTH);
    }

    // returns diameter
    public double getInsideDiameter()
    {
        return insideDiameter;
    }

    // validates diameter and sets diameter
    public void setInsideDiameter(double inDiameter)
    {
        insideDiameter = ((inDiameter >= MIN_DIAMETER && 
                    inDiameter <= MAX_DIAMETER) ? inDiameter : DEFAULT_DIAM_DEPTH);
    }

    // validates and sets last name
    public void setLastName(String lastNameIn)
    {
        lastName = ((lastNameIn.length()) > 0 ? lastNameIn : DEFAULT_NAME);
    }

    // returns last name
    public String getLastName()
    {
        return lastName;
    }

    // validates and sets first name
    public void setFirstName(String firstNameIn)
    {
        firstName = ((firstNameIn.length()) > 0 ? firstNameIn : DEFAULT_NAME);
    }

    // returns first name
    public String getFirstName()
    {
        return firstName;
    }

    // calculates total concrete necessary in cubic yards and returns that value
    @Override
    public double getConcreteVolume()
    {
        if(getInsideDiameter() == 0 || getInsideDepth() == 0)
            return 0.000;
        else
            return (getCylinderVolume(getInsideDiameter() * FT_TO_IN + DENSITY + 
                         DENSITY, getInsideDepth() + DENSITY) / CU_IN_TO_CU_YD) -
                       (getCylinderVolume(getInsideDiameter() * FT_TO_IN, 
                         getInsideDepth())) / CU_IN_TO_CU_YD;
    }

    // private utility method used to calculate the volume of a cylinder
    public double getCylinderVolume(double diameter, double height)
    {
        return (Math.PI * Math.pow(diameter / 2.0, 2)) * height;
    }
} //end class Pool
EN

回答 3

Stack Overflow用户

发布于 2013-04-14 19:10:38

以前签过合同吗?

这段代码:

代码语言:javascript
复制
public class Pool implements BackYard

就像一个。这就像后院说:“嘿后院,我正在签署一份合同,保证我将为你拥有的所有方法创建代码。”

但是Pool违反了合同。

警察(编译器)发现了这一点,并说:去做吧,伙计,或者让你的孩子去做。

要么你自己完成契约(即为后院中提到的所有方法创建代码),要么让你的后代来完成它(子类将是那些添加代码的)。你是一种“惩罚”--让你处于抽象的状态,直到完成承诺。

票数 15
EN

Stack Overflow用户

发布于 2013-04-14 18:49:34

第一个具体类必须实现来自其超类型的所有抽象方法。在你的例子中,你要么让Pool抽象,要么从尚未实现的超类型中实现所有抽象方法。

换句话说,如果您不允许抽象类池具有抽象方法,那么您的库的客户端可以这样做

代码语言:javascript
复制
Pool p = new Pool();
p.getBollardHeight();

它不能工作,因为此方法未实现。另一方面,如果您将Pool设为抽象的,则不允许实例化它,并且不会发生上述问题。

票数 2
EN

Stack Overflow用户

发布于 2013-04-14 18:54:15

您必须在Pool类中创建在BackYard中看到的所有方法

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15998164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档