简体   繁体   English

如何从另一个类java调用带有参数的方法作为对象?

[英]How to call a method with parameters as objects from another class java?

I am trying NOT to just call the method by inputting the values manually, but to run a method of another class from another class.我试图不只是通过手动输入值来调用该方法,而是从另一个类运行另一个类的方法。 I tried to find out the solution by looking at the similar questions but they did not help me in this context.我试图通过查看类似的问题来找出解决方案,但在这种情况下它们对我没有帮助。

I might be wrong please forgive me, but this requirement is asking me to call a method with parameters, which I do not understand how to do.我可能错了请原谅我,但这个要求是要求我调用一个带参数的方法,我不明白该怎么做。

This is the requirement:这是要求:

a) Add a method payForHoliday() to the Member class. a) 向 Member 类添加一个 payForHoliday() 方法。 This method will simply call a method in the Website class to pay for the holiday (ie to record the transaction with the website).该方法将简单地调用网站类中的一个方法来支付假期费用(即记录与网站的交易)。 And here we have a problem because the object from the Member class doesn't keep a record of which website the member is logged into.在这里我们遇到了一个问题,因为来自 Member 类的对象没有记录该成员登录到哪个网站。

Class Website :班级网址

public class Website {
    private String websiteName;
    private double salesTotal;
    
    public Website(String newWebsiteName)
    {
        websiteName = newWebsiteName;
    }

    // lines omitted

    public void checkout(Member purchase, Holiday chosen) 
    {
        if (checkHitDiscount() == true)
        {
            System.out.println("Congrtulations you are eligible for 10% discount!");
            salesTotal = chosen.price;
        }
        else
        {
            System.out.println("successful completion of the transaction");
            salesTotal = chosen.price;
        }
    }

Class Member :班级成员

public class Member {
    private String email;
    public int membershipNumber;
    Holiday holiday;
    Website website;

    public Member(String newEmail, int newMembershipNumber)
    {
        email = newEmail;
        membershipNumber = newMembershipNumber;
    }

    // lines omitted

    public void payForHoliday(Member purchase, Holiday chosen) 
    {
         // This method needs to run the "checkout()" method in Website class
    }

Before you can execute a (non-static) member of another class, you need an object that is an instance of that class.在您可以执行另一个类的(非静态)成员之前,您需要一个作为该类实例的对象。

To say it another way, your checkout method is an operation that operates on a particular Website, by virtue of it being defined as a member of the Website class.换句话说,您的 checkout 方法是特定网站运行的操作,因为它被定义为网站类的成员。

Therefore, to call it you need因此,要调用它,您需要

    Website xyz = …. something that delivers a website …;

and then you can call然后你可以打电话

    xyz.checkout(…);

The "something that delivers a website" is whatever fits in with your application design. “提供网站的东西”是适合您的应用程序设计的任何东西。 Maybe it's as simple as 'new Website()', but it's not possible to tell without knowing what else a Website actually represents in your design.也许它就像“new Website()”一样简单,但如果不知道网站在您的设计中实际代表什么,就不可能知道。


You are missing details of how this is supposed to work.您缺少有关它应该如何工作的详细信息。 I will supply some guesses.我将提供一些猜测。 Firstly, I assume that there are more members that web sites, that is, that each possible web site has more than one member.首先,我假设该网站有更多成员,即每个可能的网站都有多个成员。

That means that there needs to be a way to hook up a member to a web site.这意味着需要有一种方法将成员连接到网站。 The requirements point this out as a 'problem' - we can infer that you're supposed to solve the problem.需求指出这是一个“问题”——我们可以推断出你应该解决这个问题。

One way would be for the Member constructor to tell you the Website:一种方法是让成员构造函数告诉您网站:

public Member(String newEmail, int newMembershipNumber, Website newSite)
{
    email = newEmail;
    membershipNumber = newMembershipNumber;
    website = newSite;
}

then you can do this:那么你可以这样做:

public void payForHoliday(Member purchase, Holiday chosen) 
{
     website.checkout(purchase, chosen);
}

but that doesn't make a lot of sense to me - why does this method need a 'Member' argument that is different to the Member that contains it?但这对我来说没有多大意义 - 为什么这个方法需要一个与包含它的成员不同的“成员”参数? Are you sure you've got that detail right?你确定你有那个细节吗? This is more plausible:这是更合理的:

public void payForHoliday(Holiday chosen) 
{
     website.checkout(this, chosen); // 'this' means the current Member
}

But whether that's the approach that is intended or not, we can't say, because there is insufficient detail in the question.但是,这是否是有意的方法,我们不能说,因为问题中没有足够的细节。 How are any of these things used?这些东西是如何使用的? What creates Members?什么创建会员? Is the code that creates Members aware of the particular Website it is making a member for?创建会员的代码是否知道它正在成为会员的特定网站?

The problem was that I was trying to add an object of the same class Member to call the method from the other class Website , which had a Member purchase as a parameter.问题是我试图添加同一个类Member的对象来调用另一个类Website的方法,该类有一个Member purchase作为参数。

So in order to correctly call that method, I used this code:因此,为了正确调用该方法,我使用了以下代码:

    public void payForHoliday(Holiday test) 
    {
       website.checkout(this, test);
    }
  • For pointing to the method checkout() in the Website class, I used an address (Website website) declared as an instance variable in Member class.为了指向Website类中的checkout()方法,我使用了在Member类中声明为instance variableaddress (网站网站)。

  • The this keyword tells the program that the parameter is the object itself, which is calling The method. this关键字告诉程序参数是对象本身,它正在调用 方法。

  • The test variable is placed as a parameter due to the fact that is a pointer from another class.由于是来自另一个类的指针,因此test变量作为参数放置。 (I changed the variable name to another name to avoid conflicts with the one in the checkout() method) (为了避免与checkout()方法中的变量名冲突,我将变量名更改为另一个名称)

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

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