在java中使用两个类

vwhgwdsa  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(333)

我试着用两个班成员班和网站班互相交谈。我想使用member类的setloggedinstatus()方法中的代码,并且能够在website类的memberlogin()方法中使用它。我用了 Member memberObject = new setloggedInStatus(); 但它给了我一个错误。
如果有任何帮助,我将不胜感激。提前谢谢
网站类

public class Website
{
    // declaration of vars 
    private String websiteName;
    private int hits; 
    private double salesTotal; 

    /**
     * Constructor for objects of class Website
     */
    public Website(String websiteName)
    {
        // initialise instance variables
         this.websiteName = websiteName;  
    }

    Member memberObject = new setloggedInStatus(); 

   public void memberLogin() {

    }
}

成员类

public class Member
{
    // varibales declaration 
    private String email;  
    private int membershipNumber; 
    private boolean loggedInStatus; 

    /**
     * Constructor for objects of class Member
     */
    public Member(String memberEmail, int newMembershipNumber )
    {
        // initialise instance variables
        email = memberEmail;
        membershipNumber = newMembershipNumber;
    }

    //loggedInStatus method
    public void setloggedInStatus() {
    if (email != null && membershipNumber != 0) {
        loggedInStatus = true;
        System.out.println("you are logged in ");

    } 

    else {
        loggedInStatus = false;
        System.out.println("you are not logged in");
    }
}

    }
bvjxkvbb

bvjxkvbb1#

如果你想在班级网站中使用会员的功能,你需要通过 import Member 在网站文件的顶部(取决于它们是否在同一文件夹/包中)。这将使其在文件中可用。
然后可以通过 Member member = new Member( 我们走吧 ); 然后,可以从创建的 member 对象,例如 member.setLoggedInStatus(); 编辑:这回答了你的问题吗?

9jyewag0

9jyewag02#

如果website类和member类在同一个包中,则必须创建member对象,如下所示:

String email = "Example@examplemail.com";
int id = 3; //Example id
Member the_member = new Member(email,3);

如果你想打电话给 public void setloggedInStatus() 方法只需对创建的上一个对象执行以下操作:

the_member.setloggedInStatus();

如果两个类不在同一个包中,则必须使用 import Member

相关问题