Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

java - 有关方法覆盖的问题(Issue regarding Method Overriding)

I have confusion regarding Method Overriding in java.

(我对Java中的方法重写感到困惑。)

From it's definition it says :

(从定义上说:)

In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.

(在任何面向对象的编程语言中,“覆盖”是一项功能,它允许子类或子类提供其超类或父类之一已经提供的方法的特定实现。)

Now, below is one example regarding it :

(现在,下面是与此有关的一个示例:)

  class Parent { 
    void show() 
    { 
        System.out.println("Parent's show()"); 
    } 
} 
class Child extends Parent {  
    @Override
    void show() 
    { 
        System.out.println("Child's show()"); 
    } 
} 
class Main { 
    public static void main(String[] args) 
    { 
        Parent obj1 = new Parent(); 
        obj1.show(); 

        Parent obj2 = new Child(); 
        obj2.show(); 
    } 
} 

I have doubt at this line :

(我对此有疑问:)

Parent obj2 = new Child(); 

I can do the same thing using :

(我可以使用做同样的事情:)

Child obj2 = new Child();  

then why I need to declare it with the name Parent class?

(那为什么要用类的名字来声明它呢?)

What it's purpose?

(目的是什么?)

  ask by Jaimin Modi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Like you said, you don't need to declare subclass objects as their parent class, but there are other cases where this may be important, such as when you are trying to make things abstract.

(就像您说的那样,您无需将子类对象声明为其父类,但是在其他情况下,这可能很重要,例如,当您尝试使事物抽象时。)

What is Abstraction? (什么是抽象?)

Abstraction is removing everything but the most essential.

(除了删除最基本的内容外,抽象内容正在删除所有内容。)

A really good example you probably already use a ton is Lists.

(您可能已经使用了一吨的一个很好的例子是列表。)

Whether you are using an ArrayList , a LinkedList , or any other type of Java list, you know that there are certain properties that you can always count on being there, like getting the size of the list, getting a certain element at a certain point, etc.

(无论您使用的是ArrayListLinkedList还是任何其他类型的Java列表,您都知道可以始终依靠某些属性,例如获取列表的大小,在特定点获取特定元素等)

This DRAMATICALLY simplifies the use of these, and you can interchange them depending on your application.

(这样就可以简化这些操作的使用,并且可以根据您的应用程序互换它们。)

This is all because they are a subclass of List , in which these methods come from.

(这是因为它们是List的子类,这些方法来自这些子类。)

The ways that an ArrayList , and a LinkedList get and set data are different, but from the perspective of you, the user of these sub classes, the implementation is the same, you just use the classes that were overridden.

(ArrayListLinkedList获取和设置数据的方式不同,但是从您的角度来看,这些子类的用户实现是相同的,您只使用被覆盖的类。)

It's super convenient, because you don't need to know a thing about coding, try whatever you're trying to do with a linkedlist, then with an arraylist, and see whats faster.

(这非常方便,因为您不需要了解编码方面的知识,可以尝试使用链表,数组列表尝试任何操作,然后更快地查看。)

What's your part in this? (您扮演什么角色?)

In the example you gave, it is very simple, and doesn't matter, but say you were making a class that sorted lists in a particular, fun, amazing way.

(在您给出的示例中,它非常简单,并且没有关系,但是您说您正在制作一个类,以一种特殊,有趣,令人惊奇的方式对列表进行排序。)

If you declared everything as just a List , users of your class could pass in both ArrayList s, and LinkedList s, depending on what they were doing, and both would work.

(如果将所有内容都声明为List ,则类的用户可以传入ArrayListLinkedList ,这取决于它们在做什么,并且两者都可以工作。)

So, to be a good programmer, try to keep everything as abstract as possible.

(因此,要成为一名优秀的程序员,请尝试使所有内容尽可能抽象。)

It's a good rule to learn early on.

(早点学习是一个好规则。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...