简体   繁体   English

扩展类的调用函数(Java)

[英]Calling function of an Extended Class (Java)

So I have code set up such that I have a subclass 'PBill' as an inherited extension of a class 'customer'. 因此,我设置了代码,以便将子类“ PBill”作为类“ customer”的继承扩展。 However, when I try to create a new PBill object in my main function, it says that no such object exists, and it can't figure out what to do. 但是,当我尝试在主函数中创建新的PBill对象时,它表示不存在此类对象,并且无法弄清楚该怎么做。 Here is my example: 这是我的示例:

public class customer {
private int reg;
private int prem;
private int raw;
private int total;
public customer(int re,int pr, int ra){
    this.reg=re;
    this.prem=pr;
    this.raw=ra;
    this.total=re+pr+ra;
}
public customer(int re){
    this(re,0,0);
}
public customer(int re,int pr){
    this(re,pr,0);
}       
public int totalBag(){
    return(reg);
}
public double calctot(){
    if(this.reg>10){
        reg+=1;
    }
    double totcost=reg*10+prem*15+raw*25;
    return(totcost);
}
public String printBill(){
    return("You bought "+reg+" bags of regular food, "+prem+" bags of premium food, and "+raw+" bags of raw food. If you bought more than 10 bags of regular food, you get one free bag! Your total cost is: $"+this.calctot()+".");
}
class PBill extends customer{
public PBill(int re, int pr, int ra){
    super(re, pr, ra);
}
public PBill(int re, int pr){
    super(re,pr);
}
public PBill (int re){
    super(re);
}
public double calcTot(){
    return(super.calctot()*.88);
}
public String printPBill(){
    return("You are one of our valued Premium Customers! We appreciate your continued business. With your 12% discount, your total price is: $"+this.calcTot()+".");
}
}

The error message occurs when I try to call it in another class with a main object to create a new object, like this: 当我尝试在具有主对象的另一个类中调用它来创建新对象时,会出现错误消息,如下所示:

public static void main(String[] args){
PBill c1=new PBill(10,2);

Which is where it gives me the error that PBill can not be resolved to a type. 那是给我的错误是PBill无法解析为一种类型。

So how would I go about creating a new PBill object so that I can access the methods inside of it, and is there an easier way to define object inheritance? 那么我将如何创建一个新的PBill对象,以便可以访问其中的方法,并且有一种更简单的方法来定义对象继承?

PBill is the inner class of customer , if you want to instantiate a instance of PBill , you can move the definition of PBill out of customer , be sure not to add public . PBillcustomer的内部类,如果要实例化PBill的实例, PBill可以将PBill的定义移出customer ,请确保不要添加public
If you don't prefer to that, you can still create a PBill instance by 如果您不喜欢这样做,仍然可以通过以下方式创建PBill实例:

customer c = new customer(1);
customer.PBill b = c.new PBill(1);

Each Java class should be in a file of its own, with a name that's the same as the class name: customer on customer.java , PBill on PBill.java . 每个Java类都应位于其自己的文件中,该文件名应与类名相同: customer上的customer.javaPBill上的PBill.java

Adhere to conventions: class names should start with upper case letters ("Customer"). 遵守约定:类名应以大写字母(“客户”)开头。

Using inheritance to link rather unrelated concepts is not best practice. 使用继承来链接不相关的概念不是最佳实践。 A bill is one entity, and a customer is another one. 账单是一个实体,客户是另一个。 There may, of course, various categories of customers where inheritance is applicable. 当然,可能存在适用继承的各种类别的客户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM