如何消除 main class 中标记 "public" 的语法错误?

How to get rid of syntax error on token "public" in main class?

当我从主程序 运行 所需的另一个 class 调用构造函数时,我在主 class 中遇到语法错误。该程序的重​​点是继承以及构造函数和参数的适当调用。这是我在编译期间收到的错误消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        Syntax error on token "public", record expected after this token

        at a6main.main(a6main.java:7)

这是导致错误的代码行:

PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821",
"2000", true, "1000");

其余代码可以在下面找到:

class person {

    String Name;
    String Address;
    String Telephone;

    person (String Name, String Address, String Telephone) {
        this.Name = Name;
        this.Address = Address;
        this.Telephone = Telephone;
    }
    
    String getName() {
        return Name;
    }
    
    String getAddress() {
        return Address;
    }
    
    String getTelephone() {
       return Telephone;
    }
    
    void setName(String Name) {
        this.Name = Name;
    }
    
    void setAddress(String Address) {
        this.Address = Address;
    }
    
    void setTelephone(String Telephone) {
        this.Telephone = Telephone;
    }
}

public class customer extends person {

    String number;
    boolean OnMailingList;

    //constructor and getters and setters

    customer (String Name, String Address, String Telephone, String number, boolean OnMailingList) {
        //inherit persons information
        super(Name, Address, Telephone);
        this.number = number;
        this.OnMailingList = OnMailingList;
    }
    
    String getnumber() {
        return number;
    }

    void setnumber(String number) {
        this.number = number;
    }

    boolean OnMailingList () {
        return OnMailingList;
    }

    void setOnMailingList(boolean OnMailingList) {
        this.OnMailingList = OnMailingList;
    }
}

public class PreferredCustomer extends customer {

    private int purchase;
    double discount;

    /**public constructor so its accessible to main
     * else ifs for certain percentage of discounts
     * getters and setters for purchase and discount
     * super to inherit other features from other classes */
    public int getpurchase() {
        return purchase;
    }

    public double getdiscount () {
        return this.discount;
    }

    public void setPurchase(int purchase) {
        this.purchase = purchase;
    }

    public PreferredCustomer(String Name, String Address, String Telephone, String number, int pur, 
                            boolean OnMailingList, double Discount, PreferredCustomer preferredCustomer) {
        
        super(Name, Address, Telephone, number, OnMailingList);

        this.purchase = pur;
        preferredCustomer.discount = discount;

        if (this.purchase>= 2000) {
            this.discount = 10;
        } else if (this.purchase>= 1500) {
            this.discount = 7;
        } else if (this.purchase>= 1000) {
            this.discount = 6;
        } else if (this.purchase >= 500) {
            this.discount = 5;
        }
    }
}
    

public class a6main {
    
    public static void main (String [] args) {
        
    public PreferredCustomer() {

    }

    PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821","2000", true, "1000");
   
        System.out.println("Name: " + c.getName());
        System.out.println("Address: " + c.getAddress());
        System.out.println("Telephone number: " + c.getTelephone());
        System.out.println("Customer ID: " + c.getnumber());
        System.out.println("Amount spent: " + c.getpurchase());
        System.out.println("On mailing list: " + c.OnMailingList());
        System.out.println("Discount: " + c.getdiscount());
    }
}

你这里有几个错误。我已更正它们,程序启动,提供结果:

Name: Al
Address: 222BurdSt
Telephone number: 2102223321
Customer ID: 46821
Amount spent: 2000
On mailing list: true
Discount: 10.0

从 main 方法中删除 PreferredCustomer 构造函数。它不能是一个的一部分 方法,它是class的一部分。然后,PreferredCustomer 的构造函数 已经存在 在 PreferredCustomer class.

希望您的客户和 PreferredCustomer class 位于不同的文件中?如果没有,将它们放在名为 customer.java 和 PreferredCustomer.java 的单独文件中。在 PreferredCustomer class 构造函数中,从参数中删除 PreferredCustomer preferredCustomer。这是多余的:为什么您需要将一个客户传递给另一个客户?客户之间有什么关系吗?现在当你调用构造函数时参数的数量将匹配(并且不要使用字符串“2000”,“1000”,其中应该是整数):

PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821",
2000, true, 1000);

进一步在 PreferredCustomer 构造函数中,使用 this 而不是此处的 preferredCustomerthis.discount = Discount; 并使用大写字母打印 Discount,如构造函数的签名中所示。

因此,构造函数的代码应该是:

public PreferredCustomer(String Name, String Address, String Telephone, String number, int pur, boolean OnMailingList, double Discount) {
    super(Name, Address, Telephone, number, OnMailingList);
    this.purchase = pur;
    this.discount = Discount;

    if (this.purchase>= 2000) {
        this.discount = 10;
    } else if (this.purchase>= 1500) {
        this.discount = 7;
    } else if (this.purchase>= 1000) {
        this.discount = 6;
    } else if (this.purchase >= 500) {
        this.discount = 5;
    }
}

a6main中的main方法class:

public static void main (String [] args) {
    PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821", 2000, true, 1000);

    System.out.println("Name: " + c.getName());
    System.out.println("Address: " + c.getAddress());
    System.out.println("Telephone number: " + c.getTelephone());
    System.out.println("Customer ID: " + c.getnumber());
    System.out.println("Amount spent: " + c.getpurchase());
    System.out.println("On mailing list: " + c.OnMailingList());
    System.out.println("Discount: " + c.getdiscount());
}

并注意命名约定,正如其他人指出的那样。