类 - Java中的内部类和属性

时间:2017-05-04 05:36:49

标签: java

我一直在尝试制作一个包含内级啤酒的瓶子类。为什么外类“瓶子”中的啤酒属性无效?这样“私人'啤酒'领域从未被分配过?

这是我的代码

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

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

正如Harmlezz所说,你有一些语法错误。您正在使用各种get方法返回值,但将返回类型称为void

纠正这些错误后,您可以在Bottle对象中设置Beer对象,如下所示:

public static void main(String[] args) {
        Bottle bottle = new Bottle();
        Beer beer = bottle.new Beer("La La Beer", 2);
        bottle.setBeer(beer);

        System.out.println(beer.getbrandName());

    }

答案 2 :(得分:0)

您的代码中存在很多错误。

你应该做这样的事情

public class Bottle {
private Beer beer; 
private double volume;


public Bottle () {
}

public Beer getBeer() {
  return beer;
}
public void setBeer(Beer beer){
  this.beer = 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;

    }
}
}

一些建议首先清楚你的基础知识,比如如何编写函数,然后跳转到代码中。