簡體   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