英文:
Do inherited methods use the visibility, methods and attributes of the superclass or subclass?
问题
- 这是真的吗?通常继承的方法会使用子类的方法和属性吗?
- 静态/动态类型在这个任务中扮演什么角色?("A b = new B")
- 继承的方法是否使用了父类的可见性?
对不清晰的表述我道歉。我仍在努力理解大部分内容,并需要查找英文中的许多术语,以便进行翻译。任何指出不清晰表述的帮助都将不胜感激。
英文:
What I assume is that an inherited method will, by standard, use the methods and attributes of the class whose object is used to execute that method.
Here's an example for my question, it's from a task from an older exam:
public class Test {
public static void main(String[] args) {
A a = new A(3);
A b = new B(1, 4);
b.methodOne(6); // <----- This. I think that this uses "b.m" and "b.increase"
}
}
public class A {
private int m;
private int n;
public A(int n) {
m = n;
}
public void methodOne(int i) {
m -= i;
increase(i);
}
public void increase(int i) {
m += 2 * i;
}
public void visilibityTest() {
n++; // <----- I think that b.visibilityTest() would work
// Because it uses the visibility "rights" of A.
}
}
public class B extends A {
private int m;
public B(int m, int n) {
super(n);
this.m = m + 1;
}
public void increase(int i) {
m += i;
}
}
As I said in the comments, I think that by executing b.methodOne
, the attribute "b.m" and the method "b.increase" are used, even though methodOne
is inherited from class A. (I mean this.m of b, not super.m)
1. Is this true? Do inherited methods normally use the methods and attributes of the subclass?
2. What role do the static/dynamic type play in this task? ("A b = new B")
And what about visibility? In another task I found out that if you use inherited methods to access private attributes of the superclass (that should not be visible to a subclass), you can access those attributes, as if you were accessing superclass's visibility rights. I added an example method called visibilityTest()
to show that example on this task. Would that work?
3. Do inherited methods use the visibility of the superclass?
I apologize for any unclear wording. I'm both still trying to understand most of this, and also have to find out what many terms are called like in English, for the purpose of translation.
Any pointing out of unclear wording will be appreciated.
答案1
得分: 1
是的,这是正确的。当一个方法从超类继承时,子类可以覆盖它以更改行为,但如果没有覆盖,它将使用超类中的实现。在这种情况下,调用b.methodOne(6)将使用B类中的m并递增,因为b是B类型的对象。
静态类型是指引用变量的类型(在这种情况下是A b),而动态类型是指由引用变量引用的对象的实际类型(在这种情况下是B)。当你调用b.methodOne(6)时,静态类型是A,但动态类型是B。这意味着方法methodOne是根据对象b引用的类型(动态类型B)而不是引用变量b的类型(静态类型A)来解析的。
继承的方法使用超类的可见性,除非它们在子类中被覆盖。在这种情况下,b.visibilityTest()会起作用,因为它是在超类A中的方法,可见性不会因继承而改变。
英文:
Yes, this is correct. When a method is inherited from a superclass, the subclass can override it to change the behavior, but if not overridden, it will use the implementation in the superclass. In this case, calling b.methodOne(6) will use the m and increase from the B class, since b is an object of type B.
The static type refers to the type of a reference variable (in this case A b) whereas the dynamic type refers to the actual type of the object referred to by the reference variable (in this case B). When you call b.methodOne(6), the static type is A but the dynamic type is B. This means that the method methodOne is resolved based on the type of the object b refers to (dynamic type B) rather than the type of the reference variable b (static type A).
Inherited methods use the visibility of the superclass unless they are overridden in the subclass. In this case, b.visibilityTest() would work, because it is a method in the superclass A and visibility is not changed by inheritance.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论