方法声明被视为参数

问题描述 投票:0回答:1
public class TicketTest
{
    public static int COUNTER_TICKET_PRICE = 50; 
    public static int WEB_TICKET_PRICE = 30; 
    public static int WEB_TICKET_LT_WEEK_PRICE = 40; 
    public static double DISCOUNT = 0.5;
    int serial = 0;
    
    public static void main(String args[])
    {
        int total_sales = 0;
            tickets[] = {
                    new WebTicket(10),
                    new WebTicket(5),
                    new CounterTicket(),
                    new DiscountTicket(5, "Student"),
                    new DiscountTicket(10, "Senior"),
            };
            

            for(int i=0; i<tickets.length; i++) {
                    System.out.println( tickets[i] );
            }

            for(int i=0; i<tickets.length; i++) {
                total_sales += tickets[i].getPrice();
            }  

            System.out.println();
            System.out.print("Total sales: " + total_sales );


    }
    
    public void Ticket(int days) {
        public int getPrice(int days) {
    }
    }
}

在倒数第 4 行,(public int getPrice..) 它表示 public 不是“参数”getPrice 的有效修饰符。我是不是没有关闭一些括号之类的?

我尝试了很多方法,甚至删除了所有提到 getPrice 的内容,看看这是否会影响它,但我没有得到任何改变。

java methods parameters
1个回答
0
投票
    public void Ticket(int days) {
        public int getPrice(int days) {
    }
    }

您不能像这样在另一个函数中定义一个函数。你可以把它分开:

    public void Ticket(int days) {
    }
    public int getPrice(int days) {
    }

或者将其放在一个单独的类中,但不能将一个类放在另一个类中。

© www.soinside.com 2019 - 2024. All rights reserved.