gpt4 book ai didi

java - Getter 和 Setter 函数帮助和操作方法

转载 作者:行者123 更新时间:2023-12-01 11:56:20 25 4
gpt4 key购买 nike

我目前正在编写一个程序,需要在其中合并 get 函数来为程序生成和输出。该计划需要座位数 A = 30,每个座位 A 的价格 = 120.99。我认为我的程序设置方式我需要做的就是调用这些函数,然后设置金额,然后打印。我尝试过使用 ConcertSales ConcertSales= new ConcertSales();但每当我准备调用该函数时,我都会使用 ConcertSales.getNumberOfSeatsTypeA();或 ConcertSales.setNumberOfSeatsTypeA(30) ,它给了我一条错误消息。我究竟做错了什么?请帮忙!这是我的代码:

public static void main(String[] args) {
class ConcertSales{
ConcertSales concertSales = new ConcertSales();
public int numberOfSeatTypesA;
public int numberOfSeatTypesB;
public double pricePerSeatA;
public double pricePerSeatB;
public double totalSales;

public int getNumberOfSeatTypesA()
{ return numberOfSeatTypesA;}
//did the same for Types B

public void setNumberOfSeatTypesA(int newValue)
{ numberOfSeatTypesA = newValue; }
//same done for SeatsB

public double computeTotalSales()
{ return totalSales = numberOfSeatTypesA*pricePerSeatA +
numberOfSeatTypesB*pricePerSeatB }
}
concertSales.setNumberOfSeatTypesA(30); //this is where i keep getting
my error messages.

感谢任何帮助!谢谢您

最佳答案

我认为你是一个Java初学者,或者是一个OOP初学者。在Java中,所有函数/方法都应该在类的内部,甚至是main函数。

这是代码:

class ConcertSales{
public int numberOfSeatTypesA;
public int numberOfSeatTypesB;
public double pricePerSeatA;
public double pricePerSeatB;
public double totalSales;

public int getNumberOfSeatTypesA(){
return numberOfSeatTypesA;
}

public void setNumberOfSeatTypesA(int newValue){
numberOfSeatTypesA = newValue;
}

public double computeTotalSales(){
return totalSales = numberOfSeatTypesA*pricePerSeatA +
numberOfSeatTypesB*pricePerSeatB;
}

public static void main(String[] args) {
ConcertSales concertSales = new ConcertSales();
concertSales.setNumberOfSeatTypesA(30);
System.out.println(concertSales.getNumberOfSeatTypesA());
}
}

关于java - Getter 和 Setter 函数帮助和操作方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28421983/

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