繁体   English   中英

Java-已声明方法但无法引用它

[英]Java - Method declared but cannot reference it

尽管我已经在其他过程语言中编程了25年,但我还是Java的新手。 因此,我正在尝试自学Java。 试图编写一个包含两个文件的愚蠢程序:一个名为“ Customer”的类和一个名为“ Silly4”的主程序。

我假装为一家银行类型的公司实施信用卡系统(即使我的大部分经验是在国防承包方面)。 我认为这对我来说将是一个很好的教学示例。

试图建立一个称为“客户”的信用卡数据结构,以便(暂时)它可以容纳1000个客户。 在主程序“ Silly4”中,我将此客户类实例化为“ cust2”,然后从那里尝试使用“ cust2”。 我尝试检索客户编号5的(j = 5)信用卡余额。 到现在为止还挺好。

然后从那里尝试在类Customer中声明另一个方法(供将来使用),我随意调用“ bal44”,然后尝试在主程序Silly4中将其引用为“ ball44(5541);”。

因此,我编译了类Customer,然后编译了程序Silly4,并且在主程序“ Silly4”中收到对方法“ bal44(5541)”的引用的编译错误“ java:52:错误:找不到符号”。 我糊涂了。 我已经声明并成功编译了其中带有“ bal44”的Customer类,但是Java告诉我找不到它。 我糊涂了。

请原谅所有多余的println,我用它们来查看程序的运行情况。

这是类客户:

// Data Structure for credit card database

public class Customer {
    private String name;
    private int accountNo;
    private double balance;
    private boolean Overdue;

    // Getters, setters, constructor...


    public void setName( String new_name )
        {  name = new_name;  }


    public void setAccount( int new_account )
        {  accountNo = new_account;  }


    public void setBalance( double new_bal )
        {  System.out.println( " Start proc setBalance ");
           balance = new_bal;
           System.out.println( " Finish proc setBalance ");
            }

    public double getBalance()
        {  System.out.println( " Start proc getBalance ");
           System.out.println( " proc getBalance , balance= " + balance + " end print");
           return balance;
           // how to specify which element of array[1000] ? balance I want ?
           // System.out.println( " Finish proc getBalance ");
            }

    // Add new customer to credit card system
    //   (note - index in array Customer[i] is worry of main program
    //
    public void addCustomer( String name2, int account2, double bal2 )
         { name      = name2;
           accountNo = account2;
           balance   = bal2;
             }

    public void bal44 ( int account3 )
         { accountNo = account3; }


    // Constructor

    Customer ()
        {   name      = "John Smith";
            accountNo = 1005;
            balance   = 125.43;
            Overdue   = false;    }

    // see page 1032 Liang for definition complex Object and get-Procs for it


}    

这是主程序/类Silly4:

Silly4类

{//信用卡数据库程序

public static void main(String[] args)
 {
     double bal2, bal3;
     int i;                 // loop counter
     int j;

     bal2 = 151.47;
     bal3 = 5.0;           // just initialize

    // And then you can create an array of it:

    System.out.println("** Program Silly4 Running **"); 

    Customer[] cust2 = new Customer[1000];

    System.out.println("** Array cust2 instantiated **"); 

    for(i=0; i<=999; ++i)
    {
         cust2[i] = new Customer();
    }

    System.out.println("** Array2 cust2 Obj initialized **"); 



    //  try to code this eventually  -   cust2.balance = 151.47 ;
    //

    j = 5;                 // customer no. 5

    cust2[j].setBalance( bal2 );


    bal3 = cust2[j].getBalance() ;


    System.out.println("** Balance Customer " + j + " is " + bal3);

    // Add a new customer
    //  comment out -  addCustomer( "Steve Jones", 5541, 1.0 );

    bal44( 5541 );      // test out declaring new method "bal44"


    System.out.println("** End of Silly4  **");

    }

}

你打电话时

 bal44( 5541 ); 

如果没有任何对象,Java会将其视为“此”对象,并在Silly4中而不是在Customer类中搜索该方法。 就像您正在调用客户类的其他方法一样,从Silly4调用该方法。 例如

cust2[j]. bal44( 5541 ); or something like that.

在此处阅读有关“ this”的更多信息https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

PS在这里要注意的一个好处是,静态方法(如main方法)没有引用。 因此,从技术上讲,它是在Silly4中搜索类级(静态)方法,而不是实例方法。 :)

您基本上是在查询的第一句话中回答了自己。 Java不是纯粹的过程语言,它是一种面向对象的语言。

这意味着在声明方法时,它们不只是作用域函数。 在类中声明的方法与在C中的include d文件中声明的函数不同。只能在封闭类对象范围内调用该方法。 换句话说,您没有名为bal44的函数。 您拥有类Customer方法 bal44 ,这意味着您需要让类Customer的对象为您执行该对象,类似于您如何调用setBalance()

编辑:正如另一条评论所述,可能使您更困惑的是,每当您使用不带限定符的方法名称时,Java会将其视为要求this (当前对象)执行该方法的快捷方式,因此从技术上讲, method(args)this.method(args)的快捷方式。 因此,只要您在单个类的范围之内,一个类的方法将几乎像传统函数一样工作。

瞧,这是问题所在:您正在使用bal44(5541) ,但是使该方法成为非静态的,这意味着您需要创建您的类(客户)的对象,然后说例如Customer customer = new Customer() customer.bal(5541); 如果要在不创建该类对象的情况下调用该方法,只需在该方法的标题中放入关键字“ static”。 public static void bal44(int num)

进一步说明:由于您来自过程语言,因此回想起来的主要区别是对象的用法。 对象是具有多个特征或属性以及动作或行为的东西。 例如,一辆汽车,其属性是颜色,速度等,其主要动作/行为是驾驶。 属性是变量,行为/动作是方法。 例如

car.drive(); car.setColor("red");

希望对您有帮助吗? 如果您不理解,可以给我PM,或者在这里回复,我会进一步详细解释

暂无
暂无

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

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