简体   繁体   English

跨类传递对象

[英]Passing An Object Across Classes

Extremely simple question from an extremely simple high-school java student. 来自一个非常简单的高中Java学生的一个非常简单的问题。

I've gathered user input and created an object "AccountOne". 我收集了用户输入并创建了一个对象“ AccountOne”。 I must pass AccountOne into class printcode and use the printaccount method to print "myaccount.name, myaccount.address, myaccount.type, myaccount.balance". 我必须将AccountOne传递到类printcode中,并使用printaccount方法来打印“ myaccount.name,myaccount.address,myaccount.type,myaccount.balance”。 My teacher has been trying to explain inheritance and she's just losing me more and more with each day. 我的老师一直在试图解释继承问题,而她却每天都在失去我。 From what I understood, object myaccount of type AccountOne should now "have" myaccount.name, myaccount.address, myaccount.type, myaccount.balance" within itself. And because I passed the object into the method, the method inherits whatever is in the object. Ergo, method printaccount should be able to find symbols "myaccount, my account.name, myaccount.address, myaccount.type, myaccount.balance". I've researched and everything doesn't make sense to me, it all appears to be about way more advanced stuff that I haven't even begun to grasp yet. If there's a duplicate question please point me to it. 据我了解,类型为AccountOne的对象myaccount现在应该在自身内部“具有” myaccount.name,myaccount.address,myaccount.type,myaccount.balance。由于将对象传递给方法,因此该方法继承了其中的任何内容。 ergo,方法printaccount应该能够找到符号“ myaccount,myaccount.name,myaccount.address,myaccount.type,myaccount.balance”。我已经研究过,一切对我来说都没有意义似乎与我尚未开始掌握的更高级的东西有关。如果有重复的问题,请指出。

When compiling i get 10+ errors saying "not a statement" or "cannot find symbol myaccount". 编译时出现10多个错误,提示“不是语句”或“找不到符号myaccount”。 At this point i'll just take the zero, I'm beyond frustrated. 在这一点上,我只取零,我感到无比沮丧。 It's most likely something stupidly simple that i keep glancing over. 我一直在瞥一眼这很简单的事情。

I've tried defining the variables within printcode before the main statement. 我试过在main语句之前在printcode中定义变量。 Defined variables in AccountOne. AccountOne中的已定义变量。 Attempted to research different ways of define a method and couldn't find anything that made sense to me. 尝试研究不同的方法定义方法,但找不到对我有意义的任何东西。

 import java.util.Scanner; public class AccountOne { public String name; public String address; public String type; public double balance; public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter Name "); String name=keyboard.nextLine(); System.out.println(); System.out.print("Enter Address "); String address=keyboard.nextLine(); System.out.println(); System.out.print("Enter type "); String type=keyboard.nextLine(); System.out.println(); System.out.print("Enter Balance "); double balance=keyboard.nextDouble(); System.out.println(); AccountOne myaccount=new AccountOne(); myaccount.name=name; myaccount.address=address; myaccount.type=type; myaccount.balance=balance; printcode.printaccount(myaccount); } } 

Second Class - printcode: 第二类-打印代码:

public class printcode
{
    public static void main(String args[])
    {
      public static printaccount(AccountOne myaccount)
        {
            System.out.println("My Account = " + myaccount.name + " " + myaccount.address + " " + myaccount.type + " $" + myaccount.balance);
        }   
    }

}

First, when we say - myaccount.name it means myaccount is an instance of MyAccount class and it has a member variable defined and which is name 首先,当我们说myaccount.name它表示myaccountMyAccount类的实例,并且它定义了一个成员变量,即name

For example - 例如 -

class MyAccount {
    String name;
    public static void main (String...a) {
        //here am allowed to say myaccount.name
        MyAccount myaccount = new MyAccount();
    }
}

Second, the statement 二,声明

System.out.println("My Account = " + myaccount.name " " + myaccount.address " " + myaccount.type " $" + myaccount.balance);

is all wrong. 都是错的 Modify it to 修改为

System.out.println("My Account = " + myaccount.name + " " + myaccount.address + " " + myaccount.type + " \\$" + myaccount.balance);

Now, you should get another exception which should tell you non-static variable address cannot be referenced from a static context . 现在,您应该获得另一个异常,该异常告诉您non-static variable address cannot be referenced from a static context It means if you want to access name in the context of main which is static - you need to change the declaration of name to 这意味着,如果要在main的上下文中访问name ,该name是静态的-您需要将name的声明更改为

static String name;

Third, we can have only one public class in file. 第三,文件中只能有一个public类。 Change public class printcode to class printcode . public class printcode更改为class printcode Need not to consider this if both classes are in different files. 如果两个类都在不同文件中,则无需考虑这一点。

Hope this helps. 希望这可以帮助。 I copy pasted both the classes in one file. 我将两个类都复制粘贴到一个文件中。

You have defined static method printaccount (in printcode class) within main method.Move it out from main method and should work. 您已经在main方法中定义了静态方法printaccount(在printcode类中),将其从main方法中移出即可使用。

In addition to above, there are couple of more changes as below 除上述内容外,还有其他一些更改,如下所示

  1. You are missing fields like name/address etc (although i wont advice using public field instead use get/set methods) 您缺少名称/地址等字段(尽管我不会建议使用公共字段,而是使用获取/设置方法)
  2. your System.out.println was having error wherein for concatenatign string "+" was missing. 您的System.out.println出现错误,其中缺少连接字符串“ +”。

     import java.util.Scanner; public class AccountOne { public String name; public String address; public String type; public double balance; public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter Name "); String name=keyboard.nextLine(); System.out.println(); System.out.print("Enter Address "); String address=keyboard.nextLine(); System.out.println(); System.out.print("Enter type "); String type=keyboard.nextLine(); System.out.println(); System.out.print("Enter Balance "); double balance=keyboard.nextDouble(); System.out.println(); AccountOne myaccount=new AccountOne(); myaccount.name=name; myaccount.address=address; myaccount.type=type; myaccount.balance=balance; printcode.printaccount(myaccount); } } public class printcode { public static void printaccount(AccountOne myaccount) { System.out.println("My Account = " + myaccount.name + " " + myaccount.address + " " + myaccount.type + " $" + myaccount.balance); } } 

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

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