设置器和获取器

huangapple go评论138阅读模式
英文:

Setters and Getters

问题

public class FoodClass {
    public static void main(String[] args) {
        Food food1 = new Food();
        Food food2 = new Food();

        food1.setDescription("Olives");
        food1.setBarCode("C-234-bb-56");

        food2.setDescription("Duncan Hind Cake Mix");
        food2.setBarCode("23-d-445-66");

        Food food3 = new Food();
        Food food4 = new Food();

        food3.setDescription("Dog Food");
        food3.setBarCode("34-456-77");
        food3.setPrice(1.45);

        food4.setDescription("Cat Food");
        food4.setBarCode("2344-56-2-");
        food4.setPrice(2.44);

        System.out.println(food1.getDescription() + "\t\t\t\t\t" + food1.getBarCode());
        System.out.println(food2.getDescription() + "\t\t\t\t\t" + food2.getBarCode());
        System.out.println(food3.getDescription() + "\t\t\t\t\t" + food3.getBarCode() + "\t" + food3.getPrice());
        System.out.println(food4.getDescription() + "\t\t\t\t\t" + food4.getBarCode() + "\t" + food4.getPrice());
    }
}

public class Food {
    private String description;
    private String barCode;
    private double price;

    public void setDescription(String descriptionGiven) {
        description = descriptionGiven;
    }

    public void setBarCode(String barCodeGiven) {
        barCode = barCodeGiven;
    }

    public void setPrice(double priceGiven) {
        price = priceGiven;
    }

    public String getDescription() {
        return description;
    }

    public String getBarCode() {
        return barCode;
    }

    public double getPrice() {
        return price;
    }

    public Food(String foodDescription, String foodBarCode) {
        description = foodDescription;
        barCode = foodBarCode;
        price = 0;
    }

    public Food(String foodDescription, String foodBarCode, double foodPrice) {
        description = foodDescription;
        barCode = foodBarCode;
        price = foodPrice;
    }

    public Food() {
        description = "";
        price = 0;
        barCode = "";
    }
}
英文:

I have a program that uses setters and getters to return food names, barcode numbers, and prices. I have the program split into two files. The Food.java and the FoodClass.java. My problem is that I keep getting compile errors in my FoodClass.java file and I'm struggling to figure out what I need to fix. here is what I have so far for my FoodClass file code and to create a method for food 1 and 2 and other method for food 3 and 4.

the errors I am getting:

C:\Users\xXxFl\Desktop\FoodClass.java:17: error: cannot find symbol
food1.setDescription( "Olives" );
^
symbol: method setDescription(String)
location: variable food1 of type Food

C:\Users\xXxFl\Desktop\FoodClass.java:20: error: cannot find symbol
food2.setDescription( "Duncan Hind Cake Mix" );
^
symbol: method setDescription(String)
location: variable food2 of type Food

C:\Users\xXxFl\Desktop\FoodClass.java:27: error: cannot find symbol
food3.setDescription( "Dog Food" );
^
symbol: method setDescription(String)
location: variable food3 of type Food

C:\Users\xXxFl\Desktop\FoodClass.java:31: error: cannot find symbol
food4.setDescription( "Cat Food" );
^
symbol: method setDescription(String)
location: variable food4 of type Food

C:\Users\xXxFl\Desktop\FoodClass.java:39: error: cannot find symbol
System.out.println( food3.getDescription() + "\t\t\t\t\t" + food3.getbarCode + food3.getPrice() );
^
symbol: variable getbarCode
location: variable food3 of type Food

C:\Users\xXxFl\Desktop\FoodClass.java:40: error: cannot find symbol
System.out.println( food4.getDescription() + "\t\t\t\t\t" + food4.getbarCode + food4.getPrice() );
^
symbol: variable getbarCode
location: variable food4 of type Food
6 errors

public class FoodClass
{
public static void main(String [] args )
{
//place food1 and food 2 into a String getFoodList method
Food food1 = new Food();
Food food2 = new Food();
food1.setDescription( "Olives" );
food1.setBarCode("C-234-bb-56");
food2.setDescription( "Duncan Hind Cake Mix" );
food2.setBarCode( "23-d-445-66" );
//place food3 and food4 into a string getFoodPrice method
Food food3 = new Food();
Food food4 = new Food();
food3.setDescription( "Dog Food" );
food3.setBarCode("34-456-77");
food3.setPrice( 1.45 );
food4.setDescription( "Cat Food" );
food4.setBarCode( "2344-56-2-" );
food4.setPrice( 2.44 );
System.out.println( food1.getDescription() + "\t\t\t\t\t" + food1.getbarCode() );
System.out.println( food2.getDescription() + "\t\t\t\t\t" + food2.getbarCode() );
System.out.println( food3.getDescription() + "\t\t\t\t\t" + food3.getbarCode + food3.getPrice() );
System.out.println( food4.getDescription() + "\t\t\t\t\t" + food4.getbarCode + food4.getPrice() );
}
}

This is what I have for the Food.java code:

public class Food
{
// Fields
private String description;
private String barCode;
private double price;
public void setDesrciption( String descriptionGiven ) {
description = descriptionGiven;
}
public void setBarCode( String barCodeGiven ) {
barCode = barCodeGiven;
}
public void setPrice(double priceGiven ) {
price = priceGiven;
}
public String getDescription() {
return description;
}
public String getbarCode() {
return barCode;
}
public double getPrice() {
return price;
}
public Food( String foodDescription, String foodBarCode) {
description = foodDescription;
barCode = foodBarCode;
price = 0;
}
public Food( String foodDescription, String foodBarCode, double foodPrice) {
description = foodDescription;
barCode = foodBarCode;
price = foodPrice;
}
public Food() {											  
description = "";
price = 0;
barCode = "";
}
}

答案1

得分: 3

Food 中,setDesrciption 应为 setDescription,以及 food3.getbarCodefood4.getbarCode 应改为 food3.getbarCode()food4.getbarCode()

英文:

setDesrciption in Food should be setDescription and food3.getbarCode and food4.getbarCode should be food3.getbarCode() and food4.getbarCode().

答案2

得分: 1

问题在于FoodClass.javaFood.java中遵循了不统一的命名。

以下两种更改之一可以解决您的问题:

  1. Food.java中使用setDescription而不是setDesrciption

或者

  1. FoodClass.java中使用setDesrciption而不是setDescription

此外,在FoodClass.java中,food3.getbarCodefood4.getbarCode必须分别改为food3.getbarCode()food4.getbarCode()

进行这些更改,将生成如下所示的所需输出。

"C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\jbr\bin\java.exe" "-javaagent:C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\lib\idea_rt.jar=58409:C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath D:\work\trial\out\production\trial FoodClass
Olives					C-234-bb-56
Duncan Hind Cake Mix					23-d-445-66
Dog Food					34-456-771.45
Cat Food					2344-56-2-2.44
Process finished with exit code 0

要再次解决此类重构问题,理想情况下,像IntelliJ这样的IDE可以成为您的完美助手。

英文:

Well the problem here is non-uniform naming followed in FoodClass.java & Food.java.

Either of the below change must get your job resolved.

  1. Use setDescription instead of setDesrciption in Food.java.
    > or
  2. Use setDesrciption instead of setDescription in FoodClass.java.

Also in FoodClass.java food3.getbarCode and food4.getbarCode must be food3.getbarCode() & food4.getbarCode() respectively.

Making this changes, does generate the desired output as below.

"C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\jbr\bin\java.exe" "-javaagent:C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\lib\idea_rt.jar=58409:C:\Users\sai\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath D:\work\trial\out\production\trial FoodClass
Olives					C-234-bb-56
Duncan Hind Cake Mix					23-d-445-66
Dog Food					34-456-771.45
Cat Food					2344-56-2-2.44
Process finished with exit code 0

Again to resolve such issues of refactoring, ideally an IDE like Intellij can be your perfect helper.

huangapple
  • 本文由 发表于 2020年5月4日 05:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61582068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定