Java 访问类外的对象
这可能是一个noob问题,因为我刚刚开始使用Java。我用自己的类创建了一个Person对象,一个创建实际对象的主类(也是一个数组)。但是,我现在尝试在另一个类中访问此对象。如何更改当前对象,使其能够在主类之外使用 Main.javaJava 访问类外的对象,java,Java,这可能是一个noob问题,因为我刚刚开始使用Java。我用自己的类创建了一个Person对象,一个创建实际对象的主类(也是一个数组)。但是,我现在尝试在另一个类中访问此对象。如何更改当前对象,使其能够在主类之外使用 Main.java package me.chris.pizzacost; import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(
package me.chris.pizzacost;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
DecimalFormat f = new DecimalFormat("##.00");
Scanner scan = new Scanner(System.in);
static Person[] people;
System.out.println("Welcome to Pizza Cost!\nTo start, how many people are in on this order?");
people = new Person[scan.nextInt()];
scan.nextLine();
System.out.println("Type their names, pressing ENTER between names.");
for (int x = 0; x<people.length; x++) {
//System.out.println("ran");
people[x] = new Person();
people[x].name = scan.nextLine();
//System.out.println("hit the end");
}
}
}
首先
static String name = "blank";
static double cost;
不应该是静态的:
package me.chris.pizzacost;
public class Person {
String name = "blank";
double cost;
}
在主类中也创建一个setter:
public class Main {
//etc...
public static Person[] people; //put the declaration here. as a class member.
public static void setName(int element, String name){
people[element].name = name; //set the name of the specified element in the array.
}
在任何其他类中,只需调用Main.setName(personID,“Name”)代码>在静态上下文中
或者让people
成为publicstatic
类成员,只需调用Main.people[element].name=“任意名称”
(示例:)
package me.chris.pizzacost;
导入java.text.DecimalFormat;
导入java.util.Scanner;
公共班机{
public static Person[]people;//将声明放在这里。作为类成员。
公共静态void main(字符串[]args){
DecimalFormat f=新的DecimalFormat(“##.00”);
扫描仪扫描=新扫描仪(System.in);
//静止的人[]人;(摆脱这个)
System.out.println(“欢迎来到披萨成本!\n首先,这张订单上有多少人?”);
人员=新人员[scan.nextInt()];
scan.nextLine();
System.out.println(“键入它们的名称,在名称之间按ENTER键”);
对于(int x=0;xYou…不会吗?它可以正常工作。而且,名称
和成本
变量不应该是静态的
。@AlexisKing那么我该如何访问它?将它作为参数传递?@kirbyquerby对不起,我最近启动了Java,不知道你到底在说什么。我该如何更改人名[2]在另一个类中?代码是什么样子的?我做了静态部分,但是为什么setter会帮助我在其他地方访问变量?我可以很好地设置它,但是说我想看看实际对象有什么。我该怎么做?@Chris setter帮你设置变量,如果你想让人的数组成为私有的
(其他类无法通过调用Main.people直接看到它)您能通过查看对象的内容来澄清您的意思吗?Eclipse在创建“people”的开头添加“public static”时会出现错误“非法修改参数people;只允许final”@Chris我明白您的意思了,只需将public static Person[]people;
在main方法之外,作为类变量,我将更新答案。
public class Main {
//etc...
public static Person[] people; //put the declaration here. as a class member.
public static void setName(int element, String name){
people[element].name = name; //set the name of the specified element in the array.
}
package me.chris.pizzacost;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static Person[] people; //put the declaration here. as a class member.
public static void main(String[] args) {
DecimalFormat f = new DecimalFormat("##.00");
Scanner scan = new Scanner(System.in);
// static Person[] people; (get rid of this)
System.out.println("Welcome to Pizza Cost!\nTo start, how many people are in on this order?");
people = new Person[scan.nextInt()];
scan.nextLine();
System.out.println("Type their names, pressing ENTER between names.");
for (int x = 0; x<people.length; x++) {
//System.out.println("ran");
people[x] = new Person();
people[x].name = scan.nextLine();
//System.out.println("hit the end");
}
}
}