gpt4 book ai didi

java - 类 - Java 中的内部类和属性

转载 作者:行者123 更新时间:2023-12-02 02:51:33 27 4
gpt4 key购买 nike

我一直在尝试制作一个瓶子类,其中包括啤酒的内部类。为什么外部类“bottle”中的属性 beer 无效?这样“私有(private)‘啤酒’字段永远不会分配?

这是我的代码

public class Bottle {
private Beer beer; //THE PROBLEM IS IN THIS LINE//
private double volume;


public Bottle () {
}

public Beer getBeer {return beer;}
public void Beer setBeer(Beer beer){return beer;}

public double getVolume {return volume};
public void setVolume (double volume) {return this.volume = volume;}


public class Beer {
private String brandName;
private double strength;

public Beer (String brandName, double strength) {
this.brandName = brandName;
this.strength = strength;
}

public String getbrandName() {return brandName;}
public void setbrandName(String brandName) {return this.brandName = brandName;}
public double getstrength() {return strength;}
public void setstrength(double strength) {return this.strength = strength;}
}
}

最佳答案

您的代码没有一个问题,而是有很多问题。主要是语法错误。我更正了它以使其编译:

public class Bottle {

private Beer beer = new Beer("Beck's", 5.1);
private double volume;

public Bottle () {
}

public Beer getBeer() { return beer; }
public Beer setBeer(Beer beer) {return beer;}

public double getVolume() { return volume; }
public void setVolume(double volume) { this.volume = volume; }

public class Beer {
private String brandName;
private double strength;

public Beer (String brandName, double strength) {
this.brandName = brandName;
this.strength = strength;
}

public String getbrandName() {return brandName;}
public void setbrandName(String brandName) { this.brandName = brandName;}
public double getstrength() {return strength;}
public void setstrength(double strength) { this.strength = strength;}
}
}

关于java - 类 - Java 中的内部类和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43774621/

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