英文:
Acessing grandparent's method in java
问题
我正在解决使用Java的简单面向对象编程概念的练习问题。问题提供了一个UML图,要求实现。我遇到了一个问题,它要求我从子类中访问祖父类中的一个方法。
看下面的示例代码:
class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
}
class Parent extends Grandparent {
public void Print() {
System.out.println("Parent's Print()");
}
}
class Child extends Parent {
public void Print() {
super.super.Print(); // 尝试访问祖父类的Print()方法
System.out.println("Child's Print()");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}
我已经在各处进行了研究,发现Java不允许这样做,因为它涉及封装,然而在C++中是允许的。我的问题是,除了在Java中不允许的(super.super.method())语句之外,我是否可以做些什么来从祖父类中获取该方法。
我的意思是,是否可以以某种方式更改结构,保持继承不变,并且可以在不丢失UML的情况下访问该方法。
英文:
i am working on practice problems simple OOP concepts using java
the question gives a uml diagram and asks to implement .
i went through a problem it asks me to access a method in the grandparent class from the child class.
see illustration :
class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
}
class Parent extends Grandparent {
public void Print() {
System.out.println("Parent's Print()");
}
}
class Child extends Parent {
public void Print() {
super.super.Print(); // Trying to access Grandparent's Print()
System.out.println("Child's Print()");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}
i have done my reasearch everywhere i found that java doesn't allow that due to apllying Encpculation however it is allowed in C++ , my question is can i do something to get that method from grandparent class beside the not allowed ( super.super.method() ) statement in java .
i mean can i change the structure in such a way i kep the inhertiance as it is and can access that method with missing the UML .
答案1
得分: 3
- 在 Java 中无法调用 super.super.foo()。
- 你可以通过隐式方式实现:
public class Grandparent {
public void print() {
System.out.println("Grandparent's Print()");
}
}
class Parent extends Grandparent {
public void print() {
super.print(); // 在这里我们可以调用来自 Grandparent 的 print()
System.out.println("Parent's Print()");
}
}
class Child extends Parent {
public void print() {
super.print();
System.out.println("Child's Print()");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.print();
}
}
注意:尝试使用命名约定来命名方法。将 Print() 改为小驼峰命名法:print()。
英文:
-
There is no way to call super.super.foo() in java.
-
You can do it implicitly by:
public class Grandparent { public void Print() { System.out.println("Grandparent's Print()"); } } class Parent extends Grandparent { public void Print() { super.Print(); // Here we can add a call to Print() from Grandparent. System.out.println("Parent's Print()"); } } class Child extends Parent { public void Print() { super.Print(); System.out.println("Child's Print()"); } } public class Main { public static void main(String[] args) { Child c = new Child(); c.Print(); } }
- Note: Try to use naming conventions for methods. Use lowerCamelCase for Print() -> print()
答案2
得分: 1
你可以创建GrandParent
类的引用变量和Child
类的对象。所以代码变成了这样:
class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
}
class Parent extends Grandparent {
public void Print() {
System.out.println("Parent's Print()");
}
}
class Child extends Parent {
public void Print() {
//super.super.Print(); // 尝试访问Grandparent的Print()方法
System.out.println("Child's Print()");
}
}
public class Main {
public static void main(String[] args) {
GrandParent p = new Child();
p.Print();
}
}
在这里发生的情况是,所使用的引用变量是父类之一的,因此即使为子类创建了对象,引用变量只知道存在于类“GrandParent”内部的方法,并且从相同的方法中输出显示在屏幕上。
对于所有的父类都允许这样做,也就是说所有父类的引用变量都可以使用子类的对象来引用,但反之则不成立。
英文:
You can create the reference variable of GrandParent class and object of Child class. So the code becomes like this:
class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
}
class Parent extends Grandparent {
public void Print() {
System.out.println("Parent's Print()");
}
}
class Child extends Parent {
public void Print() {
//super.super.Print(); // Trying to access Grandparent's Print()
System.out.println("Child's Print()");
}
}
public class Main {
public static void main(String[] args) {
GrandParent p = new Child();
p.Print();
}
}
Here what happens is, the reference variable used is of one of the parent class so even though the object is created for the child class the reference variable only knows the method which is present inside class "GrandParent" and the output from the same method is displayed in the screen.
This is allowed for all the parent classes, that being said means all the parent class reference variable can be referred using the object of child class, but the vice versa of the same is not true.
答案3
得分: 1
我认为你应该像这样编写父类的print()方法:
public void Print() {
super.print();
System.out.println("Parent's Print()");
}
还有子类的print()方法。
通过这样做,它将会工作。
英文:
I think you should write the Parent's print() method like this :
public void Print() {
super.print()
System.out.println("Parent's Print()");
}
and also the child's print() method. <br>
by doing this it will work.
答案4
得分: 0
可以使用反射来获得预期的结果,但是为此您需要确切地知道超类向上的层级数。
Child c = new Child();
Constructor<?> constructor = c.getClass().getSuperclass()
.getSuperclass().getDeclaredConstructor();
constructor.setAccessible(true);
((Grandparent)constructor.newInstance()).Print();
英文:
You can get the expected result using reflection, but for this you need to know exactly how many levels up the super class is.
Child c = new Child();
Constructor<?> constructor = c.getClass().getSuperclass()
.getSuperclass().getDeclaredConstructor();
constructor.setAccessible(true);
((Grandparent)constructor.newInstance()).Print();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论