gpt4 book ai didi

java从另一个类访问变量

转载 作者:行者123 更新时间:2023-12-01 11:38:49 25 4
gpt4 key购买 nike

我在从另一个类访问字符串时遇到问题,我不知道我可以让它变得更加简单。例如,我有一个名为“Person”的类,并且有一个字符串名称。我通过构造函数以及直接设置名称的“名称”。我还有另一个名为 Loan 的类,我还没有尝试访问任何内容。我的最后一个类叫做“App”,一切都在那里完成。

顺便说一句 - Person 是我的父类(super class),Loan 扩展了 Person,App 扩展了 Loan,它们都在同一个包中。

现在,在我的应用程序类中,我尝试访问我创建的唯一人员对象的字符串“名称”。我再次通过构造函数直接分配了名称,但不知何故这不起作用。最初我的名字集是 protected ,因为我是这样理解的:protected = 访问同一类、同一包或子类中的方法/变量即使在我公开我的字符串之后,它仍然无法工作。我完全迷失了......

当我输入时

  System.out.println(omid.name);

它说它无法解析为变量,我需要创建一个本地变量。

谢谢!

这是我的 Person 代码:

package Programs;

public class Person {
public String name;
protected int age;
protected int id;
protected String status;
protected int alienNumber;
protected int socialSecurityNumber;

Person()
{

}

Person(String name, int age, int id)
{
this.name = name;
this.age = age;
this.id = id;
}



public void setid(int id)
{
if(id >= 100000)
{
this.socialSecurityNumber = id;
System.out.println("You are now a citizen " + name);
System.out.println("Social Security Number: " + socialSecurityNumber);
}
else
{
System.out.println("Sorry " + name + " you do not meet the qualitifactions for citizenship");
alienNumber = id;
System.out.println("You alien number is: " + alienNumber);

}
}



public String toString()
{
if(id >=99000 )
{
status = "citizen";
}
else
{
status = "immigrant";
}

return "Name: " + name +
"Age: " + age +
"Id: " + id +
"Status " + status;
}
public static void main(String[] args)
{
Person omid = new Person("omid nassir", 28, 120000);
omid.name = "omid";
}




}

贷款代码

package Programs;

import Programs.Person;


public class Loan extends Person{


public static final int LOANAMOUNT1 = 250000;
public static final int LOANAMOUNT2 = 500000;
public static final int LOANAMOUNT3 = 750000;
public static final int LOANAMOUNT4 = 1000000;
public int income;
public int loanRequested;


Loan(int income, int socialSecurityNumber, int loanRequested)
{
this.income = income;
this.socialSecurityNumber = socialSecurityNumber;
this.loanRequested = loanRequested;
}


public void applyForLoan(String name, int socialSecurityNumber, int income, int loanAmount)
{
int obtainable = LOANAMOUNT1 / 4;
if(socialSecurityNumber >= 99999)
{
if(income > obtainable)
{
obtainable = loanAmount;
System.out.println("Congradulations " + name + " you are eligible for loan amount of " + loanAmount);
}
}
else if(socialSecurityNumber <=99999)
{
obtainable = LOANAMOUNT1;
System.out.println("Sorry " + name + " in order to qualify for " + loanAmount + " you need to be"
+ " a citizen and have a minimum income of " + obtainable);
System.out.println("Loan amount granted: " + LOANAMOUNT1);
}
}


}

应用程序代码

package Programs;

public class App extends Person {

public static void main(String[] args)
{
System.out.println(omid.name);
}


}

最佳答案

您需要在Appmain方法中定义omid。您在 Personmain 中定义了 omid,但无法访问此变量,因为这是一个完全不同的作用域。您可能想阅读static及其语义。

关于java从另一个类访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29724469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com