为什么我会从静态上下文中引用非静态方法的错误?

时间:2011-01-10 14:04:07

标签: java

在下面的代码块中,我正在尝试打印location类中定义的monthlyRentnumberOfBedroomsProperty等于此方法参数的属性在LettingAgent类中。当我编译时,错误发生在if行中,它表示无法从静态上下文引用非getlocation()等非静态方法。

/**
 * Java coursework on class property which present detail information about the property

public class Property
{
    // instance variables of class property.
    private String address;
    private char location;
    private double monthlyRent;
    private int numberOfBedrooms;
    private boolean occupied;
    private String tenantName;

    /** 
     * Constructor for object of class Property with required parameters. 
     */
    public Property( String addressinput, char locationinput, double Rentinput, int Bedsinput )
    { 
        //initialise the instance variables or fields of property class.
        address = addressinput;
        location = locationinput;
        monthlyRent = Rentinput;
        numberOfBedrooms = Bedsinput;
        occupied = false;
        tenantName = "";
    }

    /**
     * Return the address of the property
     */
    public String getAdress( )
    {
        return address;
    }

     /**
     * Return the location of the property
     */

    public char getLocation( )
    {
        return location;
    }

     /**
     * Return the monthlyRent of the property
     */
     public double getmonthlyRent( )
    {
        return monthlyRent;
    }

     /**
     * Return the numberOfBedrooms of the property
     */
     public int getnumberOfBedrooms( )
    {
        return numberOfBedrooms;
    }

     /**
     * Return the occupied value of the property
     */
     public boolean isoccupied( )
    {
        return occupied;
    }

     /**
     * Return the tenant's name of the property
     */
     public String gettenantName( )
    {
        return tenantName;
    }

     /**
     * Set the monthly rent to a new value
     */
     public void setmonthlyRent( double newRent )
    {
       monthlyRent = newRent;
    }

     /**
     * Adding tenant's name to the property
     */
     public void addtenantName(String newTenant)
    {
        if(occupied == false) {
            tenantName = newTenant;
            occupied = true; 
        }
        else {
            System.out.println(" The property is already occupied");
        }

    }

     /**
     * Removing tenant's name of the property
     */
     public void removetenantName( )
    {
        if( occupied == true) {
            tenantName = "";
            occupied = false;
        }
        else {
            System.out.println(" The property is new and not occupied");
        }

    }

     /**
     * Print all the property attributes
     */
     public void printProperty( )
    {
        //simulate the printing of the property attributes.
       System.out.println( "The details of the property are as follow" );
       System.out.println("Address:" + address );
       switch(location){
           case 'n': case 'N': System.out.println("Location: North london" );break;
           case 's': case 'S': System.out.println("Location: South london" );break;
           case 'e': case 'E': System.out.println("Location: East london" );break;
           case 'w': case 'W': System.out.println("Location: West london" );
        } 

       System.out.println("Monthly-Rent:" + monthlyRent );
       System.out.println("Number of Bedrooms:" + numberOfBedrooms );
       System.out.println("Status of property:" + occupied );
       if(occupied == true) {
       System.out.println("Tenant Name:" + tenantName );
       }
       else {
           System.out.println("Property is empty at moment" );
        }

    }

}

LettingAgent Class(调用类)

import java.util.ArrayList;

/**
 * Write a description of class LettingAgent here.
 */

public class LettingAgent
{
    // instance variables of LettingAgent Class.
    private ArrayList<Property>agproperty;
    private int Propnumber;

    /**
     * Constructor for objects of class LettingAgent
     */
    public LettingAgent()
    {
        // initialise instance variables
        agproperty = new ArrayList<Property>();
        Propnumber = 0;
    }

    /**
     * Return the property number of the Class LettingAgent. 
     */
    public int getPropnumber(){
        return Propnumber;
    }    

    /**
     *Method to add new property to the class LettingAgent via class Property.
     */
    public void addNewProperty(String addressinput, char locationinput, double Rentinput, int Bedsinput)
    {
        // put your code here
        Property newProperty = new Property(addressinput,locationinput,Rentinput,Bedsinput);
        agproperty.add(newProperty);

    }

    /**
     * @Description Method for removing the propety from the class LettingAgent.
     * @param int propnumber
     */
    public void removalOfProperty(int Propnumber)
    {
        // initialise instance variables
        if( Propnumber<agproperty.size()){
            agproperty.remove(Propnumber);
            System.out.println("The property has been removed");
        }
        else{
            System.out.println("The Property number is not valid");
        }
    }

    /**
     * Method for adding tenant to the property within class LettingAgent via class Property.
     */
    public void addTenant(int Propnumber, String newName, Property newProperty)
    {
        // initialise instance variables
        if( Propnumber<agproperty.size()){
            agproperty.get(Propnumber);
            newProperty.addtenantName(newName);
        }
        else{
            System.out.println("The Property number is not valid");
        }
    }

    /**
     * Method for removing tenant from the class LettingAgent via Property class.
     */
    public void removeTenant(int Propnumber,Property newProperty)
    {
        // initialise instance variables
        if( Propnumber<agproperty.size()){
            agproperty.get(Propnumber);
            newProperty.removetenantName( );

        }
        else{
            System.out.println("The Property number is not valid");
        }
    }

    /**
     *Method for searching property within the LettingAgent class.
     */
    public void searchProperty(String Address)
    {
        // initialise instance variables
        for(Property Property : agproperty){
            if (agproperty.get(Propnumber).equals( Address)){
                System.out.println("The details of the property is as follow"+ Property);
            }
            else{
                System.out.println("The property is not in the Letting agent's Stock");
            }
        }
    }

    /**
     * Method for printing unoccupied property of the class LettingAgent.
     */

    public void printListOfUnoccupiedProperty( char Location, double maxmonthlyRent, int miniNoBeds)
    {
        // initialise instance variables
         for(Property property : agproperty){
        if((Property.getLocation().equals(Location))&&(Property.get(monthlyRent).equals(maxmonthlyRent))&&(agproperty.get(numberOfBedrooms).equals(miniNoBeds))){
            System.out.Println(Propnumber);
            Property.printProperty();
        }
        else{
            System.out.println("The Property number is not valid");
        }
    }
    }

}

2 个答案:

答案 0 :(得分:2)

在您定义的最后一个方法中(格式如下):

/**
 * Method for printing unoccupied property of the class LettingAgent.
 */
public void printListOfUnoccupiedProperty(char Location, double maxmonthlyRent, int miniNoBeds) {
    // initialise instance variables
    for (Property Property : agproperty) {
        if ((Property.getLocation().equals(Location))
                && (Property.get(monthlyRent).equals(maxmonthlyRent))
                && (agproperty.get(numberOfBedrooms).equals(miniNoBeds))) {
            System.out.Println(Propnumber);
            Property.printProperty();
        } else {
            System.out.println("The Property number is not valid");
        }
    }
}

就行:

&& (Property.get(monthlyRent).equals(maxmonthlyRent))

您正在调用get变量的Property方法。我认为那应该是Property.getMonthlyRent()

另一件事,在Java中,您应该以与其类名不同的方式命名变量。我会使用像

这样的东西
for (Property property : agproperty) { ...

我知道这是一个很小的改变,但它对你的代码的可读性产生了很大的影响。您可以在Code Conventions for the Java Programming Language中阅读其他Java代码约定。

答案 1 :(得分:2)

好的,我现在已经在BlueJ中编译了你的代码,这就是我发现的。首先我收到错误消息

non-static method getLocation() cannot be referenced from a static context

在以下行中将变量名称从Property更改为property时:

for(Property property : agproperty){

您还需要在下一行中引用它的地方进行更改:

if((Property.getLocation().equals(Location))&& ...

单词Property现在只引用类名,因此短语Property.getLocation()看起来像一个静态方法调用。您需要将其更改为:

if((property.getLocation().equals(Location))&& ...

请注意property的小写字母。

接下来还有一些需要修复的方法调用,如Property.get(monthlyRent)。同样,您需要将变量名称更改为property并将正确的方法名称放入。

之后我收到错误消息:

char cannot be dereferenced

这意味着您正在尝试在原语 char类型的变量上调用方法。你在同一个方法的开头就是这样做的:

property.getLocation().equals(Location) ...

您不能在equals上使用char方法,因为它是基本类型,而不是对象类型。 (你用什么来比较两个原语?)

当您尝试比较两个double值时,您会再次执行相同的操作。

property.getmonthlyRent().equals(maxmonthlyRent)

接下来你有:

agproperty.get(numberOfBedrooms).equals(miniNoBeds)

这里的问题有点复杂。看起来你在这个短语的开头有一个错误的变量名。想想你想要在这里比较什么,看看你修复过的前两个问题,我想你可以找出应该在这里的内容。