OOP:计算对象的平均值

时间:2021-04-02 16:26:23

标签: java

我正在用 Java 学习 OOP。我有一个名为 Personage 的对象,其中包含以下元素:

public class Personage {

  public String name; 
  public int note1;
  public int note2;


  public Personage(String name, int note1, int note2){
    this.name = name;
    this.note1 = note1;
    this.note2 = note2;

  }

  public void display(){
    System.out.println("Name : " + name);
    System.out.println("Note 1 : " + note1);
    System.out.println("Note 2 : " + note2);
  }

}

在文件 Main.java 中,我有这个:

class Main {
  public static void main(String[] args) {
    
    Personage perso1 = new Personage("Eric", 7, 6);
    perso1.display();
    System.out.println("-----------------");
    Personage perso2 = new Personage("Sandrine", 8, 9);
    perso2.display();

  }

}

我想计算每个人物的平均值。我知道要从事过程式编程,但我不懂 OOP。

我试过了?

public class Personage {

  public String name; 
  public int note1;
  public int note2;
  public int noteFinale;



  public Personage(String name, int note1, int note2){
    this.name = name;
    this.note1 = note1;
    this.note2 = note2;
  }

  public int calculateAverage(String name, int note1, int note2, int noteFinale){
    int noteFinale = note1 + note2;
    return noteFinale / 2;

  }

  public void display(){
    System.out.println("Name : " + name);
    System.out.println("Note 1 : " + note1);
    System.out.println("Note 2 : " + note2);
  }

}

Main.java

class Main {
  public static void main(String[] args) {
    
    Personage perso1 = new Personage("Eric", 7, 6);
    int average = perso1.calculateAverage();
    System.out.println("Average => " + average);
    perso1.display();
    System.out.println("-----------------");
    Personage perso2 = new Personage("Sandrine", 8, 9);
    perso2.display();

  }

}

我不明白这个概念,事实上!!

感谢您的帮助。

8 个答案:

答案 0 :(得分:2)

班级人物

  public int getAverage(){
    this.noteFinale = (this.note1 + this.note2)/2; 
    return this.noteFinale;
  }

主类

class Main {
  public static void main(String[] args) {
    
    Personage perso1 = new Personage("Eric", 7, 6);
    System.out.println("Average => " + perso1.getAverage());
    perso1.display();
    System.out.println("-----------------");
    Personage perso2 = new Personage("Sandrine", 8, 9);
    perso2.display();

  }

}

如果对Java中OOP的主要概念有疑问,可以查看https://docs.oracle.com/javase/tutorial/java/concepts/index.html

答案 1 :(得分:1)

public class Personage {

  ...
  private double average;

  public Personage(String name, int note1, int note2){
    this.name = name;
    this.note1 = note1;
    this.note2 = note2;
    this.average = (double)(note1 + note2 )/2
  }
  ...
  public double getAverage() {
     return average;
  }
}

public class Personage {
    ...
    public double getAverage() {
     return (double)(note1+note2)/2;
    }
}

Personage perso1 = new Personage("Eric", 7, 6);
System.out.println("Average => " + perso1.getAverage());

答案 2 :(得分:1)

这里,for j in range(len(negative_ratings_id)): if negative_ratings_id[j] == negative_ratings_id[j+1] + 1 or negative_ratings_id[j] == negative_ratings_id[j+1] - 1: negative_ratings_val.pop(j) print(negative_ratings_id) 将是一个派生成员,它从成员变量 averagenote1 中获取其值。您可以定义一个方法,根据 note2note1 的值计算平均值。

note2

或者,您可以定义一个静态 public class Personage{ //Other code here public int getAverage{ return (this.note1 + this.note2)/2; } } ,它接受​​ Function 的参数并返回平均值。

Personage

答案 3 :(得分:1)

看,您的 Personage 类有两个字段 note1、note2。为了计算平均值,现在需要一种方法,您应该在其中实现平均值计算逻辑。计算后需要返回平均值。

平均值将是一个浮点数,因此您不应为平均值声明整数类型。

public class Personage {

  public String name; 
  public int note1;
  public int note2;


  public Personage(String name, int note1, int note2){
    this.name = name;
    this.note1 = note1;
    this.note2 = note2;
  }

  public double calculateAverage(){
    return (double)(this.note1 + this.note2) / 2;

  }

  ...
}

答案 4 :(得分:1)

离你不远了。 OOP 的一大原则 encapsulation 意味着一个对象应该知道如何对其成员变量进行操作,并将该操作对外界隐藏。

隐藏成员变量

您离简单 Java 对象(也称为普通旧 Java 对象 (POJO))的规范结构不远了。您的类应将其实例变量标记为 private,并使其只能通过 getters 访问。这将确保每个 Personage 实例将其业务保密,并且没有其他对象干扰它。

使用成员变量

您将平均计算代码作为 Personage 的方法是正确的。但就目前而言,您的代码将无法编译,因为 Personage 类没有 calculateAverage() 方法,它只有一个 calculateAverage(String name, int note1, int note2, int noteFinale) 方法。 calculateAverage 方法中的代码已经可以访问所有成员变量(note1note2noteFinale)。所以你不需要再次接收它们作为参数,你可以使用现有的成员变量:

public int calculateAverage() {
  int noteFinale = note1 + note2;
  return noteFinale / 2;
}

让对象初始化自己的状态

可以对您的代码进行的另一项改进是,假设 note1note2 对于给定的 Personage 实例始终相同,您已经可以计算出构造函数。 calculateAverage() 因此可以是私有的,并作为构造函数中的最后一条指令调用。

这使得类“不可变”,这意味着一旦它的状态被设置(在构造函数中),它就永远不会改变。 Java 中不可变类的另一个示例是 String。这对于管理大量对象和减少错误非常有帮助。

命令查询分离

许多 OOP 开发人员遵循的另一个原则是 Command Query Separation。这意味着一个方法应该改变对象(如初始化一个变量,或者以某种方式改变它的状态),或者返回一个值。这意味着如果您将 calculateAverage() 拆分为两个单独的方法,您的代码将更容易推理:

public int getAverage()
// and
private void calculateAverage()

在我写这篇文章的时候,我可以看到其他 5 个人已经回答了你,所以我相信你会有很多工作要做。互联网上有很多关于 OOP 的资源,从初学者到专家级别。这是one。对我来说,我是通过阅读有关 C++ 的书籍和观看大量 Youtube 视频开始接触 OOP 的。快乐编码!

答案 5 :(得分:1)

您选择的方法并非完全错误。 我可以列出你犯的错误:

  • 创建一个名为 calculateAverage() 的方法是可以的,但是,由于您已经创建了 Personage 的实例,因此再次发送您已经拥有的作为实例属性的参数是没有意义的( String name, int note1, int note2, int noteFinale).
  • 由于之前的错误,代码将无法编译,因为在 Main 类中,您有以下行:
    int average = perso1.calculateAverage();
    没有名为 calculateAverage() 的方法,因为您创建的方法具有以下签名:
    public int calculateAverage(String name, int note1, int note2, int noteFinale)
  • 无需创建属性 noteFinale 作为平均值的中间结果。即使您需要将 note1note2 的总和用于其他目的,可以使用原始属性计算的值也应该进行计算,而不是存储为新属性。立>
  • 该方法返回一个 int。由于平均值可能是浮点数,因此您需要返回 double(除非您想通过计算 floor 平均值来故意返回 int)。< /li>

因此,Personage 类的正确实现是:

人物.class

public class Personage {

  public String name; 
  public int note1;
  public int note2;


  public Personage(String name, int note1, int note2){
    this.name = name;
    this.note1 = note1;
    this.note2 = note2;
  }

  public double calculateAverage(){
    return (double)(note1 + note2) / 2;
  }

  public void display(){
    System.out.println("Name : " + name);
    System.out.println("Note 1 : " + note1);
    System.out.println("Note 2 : " + note2);
    System.out.println("Average : " + calculateAverage());
  }

}

如果您了解这些其他更正,我建议您使用 getters 而不是具有公共属性并覆盖 toString() 方法以将您的 Personage 实例表示为字符串(而是使用 display())。

答案 6 :(得分:0)

您作为程序代码提供的方式没有错,但您应该考虑计算行为是否是“人员”的责任。

让我们想象一个超市,你在那里购买了几种产品,谁负责给你最终价格?注册表?产品?因此,通过这种方式,您应该将产品的总和委托给对其负责的“事物”。

在简历中,将计算平均值的逻辑放在“人物”类之外。

答案 7 :(得分:0)

人物.class

  public int calculateAverage(){
   return (note1 + note2) / 2
 }

之后,我强烈建议您将属性设置为私有并使用 getter 和 setter 访问它

在main.class中你是对的,但是如果你想使用多个Personage,你应该使用Collection并创建Management类

祝你好运!!!

相关问题