简体   繁体   English

如何在不创建子类和/或继承类的情况下从另一个类调用Object的方法?

[英]How to call an Object's Method from another class without creating a sub-class and/or inheriting class?

I've been learning basic code at a very beginner level. 我一直在初学阶段学习基本代码。 Now I'm finally starting to dabble in actually writing simple programs, and got really stuck. 现在我终于开始涉足实际编写简单的程序,并且真的陷入困境。

  • I'm writing a simple a program that consists of two classes; 我正在写一个简单的程序,它由两个类组成; People, MainPage. 人,MainPage。

  • Once the program runs, the method openApp() is called in main method from (MainPage Class). 程序运行后,方法openApp()在main方法中从(MainPage Class)调用。

     public static void main(String[] args) { openApp(); } 
  • Next, when the openApp() is called, the user has three menus to choose to go to that are selected by entering the corresponding number 接下来,当调用openApp() ,用户有三个菜单可供选择,通过输入相应的数字来选择

    ie 1 = Newsfeed, 2 = Profile or 3 = Friends. 即1 =新闻源,2 =个人资料或3 =朋友。

public class MainPage { 公共类MainPage {

public static void openApp() {


    System.out.println("Welcome to App!");
    System.out.println();
    System.out.println("To Select Option for:");
    System.out.println("Newsfeed : 1");
    System.out.println("Profile :  2");
    System.out.println("Friends :  3");
    System.out.println("Enter corresponding number: ");
    int optionSelected = input.nextInt();

    switch (optionSelected) { 

    case 1: System.out.println("NewsFeed");
             break;
    case 2:  System.out.println("Profile");
             break;
    case 3:  System.out.println("Friends");
        break;

        if (optionSelected == 3) {
            people.friend();// Is it possible to write: friend() from "People" Class without extending to People Class
                    }

    }
}
  • If User selects "friends" then the program calls a method from 如果用户选择“朋友”,则程序从中调用方法
    People Class called friend(People name) in MainPage class that prints out people object's friends. People类 MainPage类中称为friend(People name) 用于打印对象的朋友。

My Attempt: 我的尝试:

  if (optionSelected == 3) {
        people.friend();
                }

The Error I get: 我得到的错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: people cannot be resolved 线程“main”中的异常java.lang.Error:未解决的编译问题:无法解析人员

Problem is I don't want to extend People Class in MainPage and inherit all it's methods, yet I still want to call an Object method from People Class to print people object's friends. 问题是我不想在MainPage中扩展People Class并继承它的所有方法,但我仍然想从People Class调用Object方法来打印people对象的朋友。

Note: just in case anyone would want to look at the friend(People people) method that is located in the People class: 注意:以防任何人想要查看位于People类中的friend(People people)方法:

public void friend(People people) {
    System.out.println(people.friend);

Excellent question format. 优秀的问题格式。

You can declare an Object of type People , and use that. 您可以声明类型为PeopleObject ,并使用它。

Example

public class MainPage
{
    People people = new People();

    // .. Some code.

    if(optionSelected == 3) {
        people.friend();
    } 
}

Explanation 说明

Your friend method is an instance method . 您的friend方法是一种instance method This means that in order to access it, you need to have an instance of the object created. 这意味着要访问它,您需要创建一个对象实例。 This is done with the new keyword. 这是通过new关键字完成的。 Secondly, unless People is some form of utility class, then your friend method should probably read more like: 其次,除非People是某种形式的实用类,否则你的friend方法应该更像是:

 public void friend()
 {
     System.out.println(this.friend);
 }

And for the sake of good code design, remember that your MainPage class is outputting to the user, so you should return the value rather than print it. 为了良好的代码设计,请记住您的MainPage类正在输出给用户,因此您应该return值而不是打印它。 Secondly, you should conform to good naming standards, and in Java we use the get prefix when getting a class member. 其次,您应该遵循良好的命名标准,在Java我们在获取类成员时使用get前缀。

public void getFriend()
{
    return this.friend;
}

and in the MainPage class, you should print this. MainPage类中,您应该打印它。

if(optionSelected == 3)
{
   System.out.println(people.getFriend());
}

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

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