如何在 Java 中编写覆盖方法 [英] How to write override methods in Java

查看:54
本文介绍了如何在 Java 中编写覆盖方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是家庭作业的一部分.

This is part of a homework assignment.

我正在尝试创建一个名为 RomanNumeralclass ,它具有多种方法.第一种方法,RomanNumeral 检查字符串 rn 是否是罗马数字.第二种方法将罗马数字转换为十进制值.我的问题在于其余的方法.我被要求为 .equals().compareTo()toString() 创建一个覆盖文件.我的问题是我将如何开始这样做?覆盖是什么意思?我应该看看他们做什么,然后效仿吗?如果我是,在哪里可以查看定义 .compareTo()toString() 的方法?

I am trying to make a class named RomanNumeral that has several methods. The first method, RomanNumeral checks whether a string rn is a Roman Numeral or not. The second method converts the Roman numeral into a decimal value. My problem lies with the rest of the methods. I am asked to create an override file for .equals(), .compareTo() and toString(). My question is how would I even begin doing this? What does it mean to override? Am I supposed to look at what they do, and emulate it? And if I am, where can look at the method that defines .compareTo() or toString()?

如果有帮助,我可以发布我当前的代码.

I can post my current code if it helps.

推荐答案

好的,据我所知,您在这里有 2 个主要问题.

Ok, from what I can see, you have 2 main questions here.

  1. 重写方法是什么意思?
  2. 如何覆盖方法?

让我们想一个新的班级,这样我就可以不用为你做作业了.

Lets think up a new class, so that I can avoid doing your homework for you.

我们有一个名为 Student 的类,它存储 3 个字符串.

We have a class called Student, that stores 3 Strings.

  • 名字
  • 姓氏
  • GPA

它可能看起来像这样.

public class Student {
    String firstName;
    String lastName;
    String gpa

    public Student(String firstName, String lastName, String gpa){
        this.firstName = firstName;
        this.lastName = lastName;
        this.gpa = gpa;
    }

    // Perhaps some methods here

    public String getIdentity(){
        return this.lastName + ", " + this.firstName;
    }
}

您喜欢 Student 类,但您决定如果您也可以跟踪 College studentId 会更好.一种解决方案是扩展 Student 类.

You like the Student class, but you decide that it would be much better if you could keep track of a College studentId as well. One solution would be to extend the Student class.

通过扩展类,我们可以访问 Student 拥有的所有方法(和构造函数),并且我们可以添加一些我们自己的方法.所以让我们调用我们的新类 CollegeStudent,并为 studentId 添加一个字符串.

By extending the class, we get access all of the methods (and constructors) that Student had, and we get to add some of our own. So lets call our new class CollegeStudent, and add a String for the studentId.

public class CollegeStudent extends Student{

    String studentId;

}

现在,无需额外的工作,我就可以创建一个 CollegeStudent,并获取它的身份.

Now, with no additional work, I can create a CollegeStudent, and get it's identity.

// Code
CollegeStudent myCollegeStudent = new CollegeStudent("Dennis", "Ritchie", "2.2");
String identity = myCollegeStudent.getIdentity();
// Code

好的,足够的设置.让我们回答您的问题.

Ok, enough setup. Lets answer your questions.

因此,假设您不返回Ritchie, Dennis",而是希望 getIdentity() 返回该大学生的studentId".然后您可以覆盖 getIdentity 方法.

So lets assume that instead of returning "Ritchie, Dennis", you would prefer getIdentity() to return the "studentId" for that college student. You could then override the getIdentity method.

通过覆盖该方法,您将重新编写 getIdentity() 以便它返回 studentId.CollegeStudent 类看起来像这样.

By overriding the method, you will get to re-write getIdentity() so that it returns the studentId. The CollegeStudent class would look something like this.

public class CollegeStudent extends Student{

    String studentId;

    @Override
    public String getIdentity(){
        return this.studentId;
    }
}

要覆盖一个方法,你必须返回相同类型的对象(在我们的例子中是String),并且你必须接受相同的参数(我们的例子不接受任何参数)

To override a method, you must return the same type of object (String in our example), and you must accept the same parameters (our example did not accept any parameters)

您也可以覆盖构造函数.

You can also override constructors.

那么这如何适用于您的作业?.equals()、.compareTo() 和 .toString() 已经定义,因为您将要扩展的类(例如 Object).但是,您需要重新定义或覆盖这些方法,以便它们对您的类有用.也许您的 toString() 实现将返回单词four"而不是IV".可能不是,但关键是您现在可以自由决定如何对方法进行编码.

So how does this apply to your assignment? .equals(), .compareTo(), and .toString() are already defined because of the classes that you will be extending (such as Object). However you will want to re-define or override those methods so that they will be useful for your class. Perhaps your implementation of toString() will return the word "four" instead of "IV". Probably not, but the point is that you now have the freedom to decide how the method should be coded.

希望这会有所帮助.

这篇关于如何在 Java 中编写覆盖方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆