简体   繁体   English

在不同类之间导入和使用方法?

[英]import and use methods among different classes?

I created a method called calRoomCharges(int cusID) in the class public class transactions extends javax.swing.JFrame 我在public class transactions extends javax.swing.JFrame创建了一个称为calRoomCharges(int cusID)public class transactions extends javax.swing.JFrame

Then I import this class to the class called public class AddReservation extends javax.swing.JFrame 然后,将此类导入到名为public class AddReservation extends javax.swing.JFrame

I imported the class as follows. 我按如下方式导入了该类。

import myhotel.FrontOffice.transactions;

these both classes are in the same project. 这两个类都在同一个项目中。

But when I'm going to call method calRoomCharges(int cusID) in AddReservation class it says that there is no such method. 但是,当我要在AddReservation类中调用方法calRoomCharges(int cusID) ,它说没有这种方法。

I did this in netbeans. 我在netbeans中做到了。

So howshould I import the class and use the method that I created in another class? 那么,如何导入该类并使用在另一个类中创建的方法呢?

here is the method I created 这是我创建的方法

  public double calRoomCharges(int cusId)
   {
       double tot=0.0;

        try
        {


            //Get Rates
            String sql="SELECT nights FROM reservation where cus_id ='"+cusId+"'";
            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery();
            int nights = 0;
                if(!rs.next())
                {
                    //handle no result
                } 
                else 
                {
                    nights = rs.getInt("nights") ;

                }




            //Get number of rooms  

            String sql2="SELECT no_of_rooms FROM reservation where cus_id ='"+cusId+"'";
            pst=conn.prepareStatement(sql2);
            rs=pst.executeQuery();
            int rooms =0;

                if(!rs.next())
                {
                    //handle no result
                } 
                else 
                {
                    rooms = rs.getInt("no_of_rooms") ;

                }


            //Get rates

           String sql3="SELECT rates from room_type a,rooms b ,reservation r where a.type=b.type AND b.room_no=r.room_no AND r.cus_id='"+cusId+"' group by r.cus_id";
            pst=conn.prepareStatement(sql3);
            rs=pst.executeQuery();
            double roomRates =0.0;

                if(!rs.next())
                {
                    //handle no result
                } 
                else 
                {
                    roomRates = rs.getDouble("rates") ;

                }



            //Calculate room charges

            tot =roomRates * rooms * nights;



        }
        catch(Exception e){}

       return tot;
   }

And this is the way I called it 这就是我所说的方式

private void EmpnamefieldMouseClicked(java.awt.event.MouseEvent evt) {                                          

        int cusNo =Integer.parseInt(cusID.getText());
        double roomCharges = calRoomCharges(cusNo);
    }           

You have 2 possibilities of calling the calRoomCharges method: 您有两种调用calRoomCharges方法的可能性:

the method is a static method and you can call it via: 该方法是静态方法,您可以通过以下方式调用它:

transactions.calRoomCharges(123);

the method is an object method, so you have to create an object first like: 该方法是一个对象方法,因此您必须首先创建一个对象,例如:

transactions obj = new transactions();

obj.calRoomCharges(123);

You simply have to either make the method static or call it through an object 您只需要使方法静态或通过对象调用即可

public static double calRoomCharges(int cusId)
{
     // ...
}

and call it like that: 并这样称呼它:

double roomCharges = transactions.calRoomCharges(cusNo);

or call it through a created object: 或通过创建的对象调用它:

transactions t = new transactions();
double roomCharges = t.calRoomCharges(cusNo);

And a good thing is to name classes with a first capital letter so you'd have class Transactions 一件好事是用首字母大写来命名班级,这样您就可以进行班级交易

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

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