在 OOP 中返回计算值
Returning a calculated value in an OOP
我是面向对象编程的新手,所以我不确定如何 return 在字符串中计算此计算的结果。我正在寻找这样的输出:
约翰的信用等级为 187。
我的问题是功劳似乎未被计算或与对象无关。我应该 return 从 calcCredit 方法中获取一些值吗?
public class Person
{
private String name;
private int age;
private double rating;
public Person(String name, int age)
{
}
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public void calcCredit()
{
//credit calculation would go here, for my purpose a static number right now.
rating = 500;
}
//method to returns the status
public String findStatus()
{
//return my desired output
}
public class CreditDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name: ");
String n = keyboard.nextLine();
System.out.print("Enter the age: ");
int a = keyboard.nextInt();
Person user = new Person(n, a);
//how do I call findStatus to get the credit associated with the user?
}
}
}
将此设为您的 calcCredit() 方法:
public int calcCredit()
{
int credit = 500;
//Do some more calculations on credit here
return credit;
}
这样实现:
Person user = new Person(n, a);
String status = user.findStatus();
int credit = user.calcCredit();
System.out.println("This person's status is " + status);
System.out.println("Their credit is " + credit);
您应该有一个 getter 方法来从 class 获取数据。此外,另一种选择是立即计算相关数据并return。这是一个基于您的 Person
class:
的示例
public class Person {
//current fields
public double getCredit() {
double credit = ...; //calculations here
//return the value of this variable to clients
return credit;
}
}
然后,在您的客户端代码中,调用此方法并使用数据:
public class CreditDemo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name: ");
String n = keyboard.nextLine();
System.out.print("Enter the age: ");
int a = keyboard.nextInt();
Person user = new Person(n, a);
//how do I call findStatus to get the credit associated with the user?
double credit = user.getCredit();
System.out.println("Credit for " + user.getName() + " is: " + credit);
}
}
你忘了在构造函数中用参数初始化变量。
关于你的问题,你可以这样定义 findStatus() 方法,
public String findStatus() {
return "The credit rating of " + name + " is " + rating;
}
在main方法中可以调用下面的方法打印结果,
System.out.println(user.findStatus());
既然您已经为姓名和年龄设置了方法,也许也可以为一个人的这些特定属性定义 'get' 方法,只是一个想法。
现在你已经创建了一个人,接下来你应该计算他的信用等级。为此,请致电
user.calcCredit();
然后你需要打印出他的状态。您可以这样做:
System.out.println("The credit rating of John is " + user.getStatus());
为了得到您的信任,请在 class 人中创建一个 public 方法:
public int getCredit() {
return this.credit;
}
然后您可以在主方法中调用 user.getCredit() 来打印值。
我是面向对象编程的新手,所以我不确定如何 return 在字符串中计算此计算的结果。我正在寻找这样的输出:
约翰的信用等级为 187。
我的问题是功劳似乎未被计算或与对象无关。我应该 return 从 calcCredit 方法中获取一些值吗?
public class Person
{
private String name;
private int age;
private double rating;
public Person(String name, int age)
{
}
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public void calcCredit()
{
//credit calculation would go here, for my purpose a static number right now.
rating = 500;
}
//method to returns the status
public String findStatus()
{
//return my desired output
}
public class CreditDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name: ");
String n = keyboard.nextLine();
System.out.print("Enter the age: ");
int a = keyboard.nextInt();
Person user = new Person(n, a);
//how do I call findStatus to get the credit associated with the user?
}
}
}
将此设为您的 calcCredit() 方法:
public int calcCredit()
{
int credit = 500;
//Do some more calculations on credit here
return credit;
}
这样实现:
Person user = new Person(n, a);
String status = user.findStatus();
int credit = user.calcCredit();
System.out.println("This person's status is " + status);
System.out.println("Their credit is " + credit);
您应该有一个 getter 方法来从 class 获取数据。此外,另一种选择是立即计算相关数据并return。这是一个基于您的 Person
class:
public class Person {
//current fields
public double getCredit() {
double credit = ...; //calculations here
//return the value of this variable to clients
return credit;
}
}
然后,在您的客户端代码中,调用此方法并使用数据:
public class CreditDemo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name: ");
String n = keyboard.nextLine();
System.out.print("Enter the age: ");
int a = keyboard.nextInt();
Person user = new Person(n, a);
//how do I call findStatus to get the credit associated with the user?
double credit = user.getCredit();
System.out.println("Credit for " + user.getName() + " is: " + credit);
}
}
你忘了在构造函数中用参数初始化变量。
关于你的问题,你可以这样定义 findStatus() 方法,
public String findStatus() {
return "The credit rating of " + name + " is " + rating;
}
在main方法中可以调用下面的方法打印结果,
System.out.println(user.findStatus());
既然您已经为姓名和年龄设置了方法,也许也可以为一个人的这些特定属性定义 'get' 方法,只是一个想法。
现在你已经创建了一个人,接下来你应该计算他的信用等级。为此,请致电
user.calcCredit();
然后你需要打印出他的状态。您可以这样做:
System.out.println("The credit rating of John is " + user.getStatus());
为了得到您的信任,请在 class 人中创建一个 public 方法:
public int getCredit() {
return this.credit;
}
然后您可以在主方法中调用 user.getCredit() 来打印值。