gpt4 book ai didi

java - 为什么我会收到关于从静态上下文中引用非静态方法的错误?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:53 26 4
gpt4 key购买 nike

在下面的代码块中,我尝试打印 中定义的 locationmonthlyRentnumberOfBedrooms 的属性Property 类等于 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类(调用类)

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");
}
}
}

}

最佳答案

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

/**
* 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))

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

还有一件事,在 Java 中,您应该以不同于类名的方式命名变量。我会使用类似

for (Property property : agproperty) { ...

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

关于java - 为什么我会收到关于从静态上下文中引用非静态方法的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4647584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com