java Java从抽象类访问子类方法/变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12875129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Accessing subclasses methods/variables from abstract class
提问by mariofalcao
This code is not mine and i need to add some improvements to it, but i'm stuck in this problem.
这段代码不是我的,我需要对其进行一些改进,但我被这个问题困住了。
I have an abstract class "CallNode" and a lot of subclasses, one of them is "Call". The "Checker" class is intercepting missed calls, but return them as CallNode. My problem is that i need to get the id of this call, but i can't access it by CallNode.
我有一个抽象类“CallNode”和许多子类,其中之一是“Call”。“Checker”类正在拦截未接来电,但将它们作为 CallNode 返回。我的问题是我需要获取此调用的 ID,但我无法通过 CallNode 访问它。
Do you have any suggestions to solve this problem?
你有什么建议来解决这个问题吗?
I let you the code, so that you can better understand the problem:
我让你代码,以便你更好地理解问题:
public abstract class CallNode {
public abstract CallNode hasMissingCall();
}
public class Call extends CallNode {
public int id;
// Simplification of method
public CallNode hasMissingCall() {
if (true)
return this;
// ...
}
}
public class Checker{
private static CallNode rootExpected;
CallNode missing = rootExpected.hasMissingCall();
System.out.println( missing.id ); // THE PROBLEM!!!
}
Thank's in advance!!!
提前致谢!!!
回答by Heisenbug
Define a template method returning the id in the abstract class:
在抽象类中定义一个返回 id 的模板方法:
public abstract class CallNode {
public abstract CallNode hasMissingCall();
public abstract int getId();
}
let subclass implement it:
让子类实现它:
public class Call extends CallNode {
public int id;
// Simplification of method
public CallNode hasMissingCall() {
if (true)
return this;
// ...
}
public int getId()
{
return this.id;
}
}
That's an option. Personally I'll move the id field upward in the CallNode class, since I think it's supposed to be unique and shared by all subclasses.
这是一个选择。我个人将在 CallNode 类中向上移动 id 字段,因为我认为它应该是唯一的并且由所有子类共享。
回答by Rohit Jain
You should never directly access your fields
.
你应该never directly access your fields
。
In general the fields
should be declared private
and you should have public accessor
methods to access them.
一般来说,fields
应该声明private
并且你应该有public accessor
方法来访问它们。
If you can modify your classes, then add a method getId()
in both abstract CallNode
class and Call
class. And in Call class have this method return your this.id
. This will do the job for you.
如果你可以修改你的类,那么getId()
在abstract CallNode
类和Call
类中添加一个方法。并且在 Call 类中有这个方法返回你的this.id
. 这将为您完成工作。
So, add this method to your Call
class: -
因此,将此方法添加到您的Call
课程中:-
public int getId() {
return this.id;
}
Let this method be abstract
in your Abstract
class.
让这个方法abstract
在你的Abstract
课堂上。
public abstract class CallNode {
public abstract CallNode hasMissingCall();
public abstract int getId();
}
And invoke it using : -
并使用以下方法调用它:-
missing.getId();
This will call the getId()
of your Call
class.
这将调用getId()
您的Call
类的 。
This code is not mine and i need to add some improvements to it,
这段代码不是我的,我需要对其进行一些改进,
PS: -One improvement you really need to add is, change the hasMissingCall
to getMissingCall
as it is returning you a missed call
. Always follow naming conventions
in your code.
Remember, you don't write code for yourself, but you write them for others to use and maintain.
PS:-您真正需要添加的一项改进是,更改为hasMissingCall
,getMissingCall
因为它会返回missed call
. 始终遵循naming conventions
您的代码。
请记住,您不是为自己编写代码,而是为他人编写代码以供使用和维护。
回答by Vlad
You could cast down to Call
:
你可以转换为Call
:
System.out.println( ((Call) missing).id );
That's if you can't change the other classes, and if you know missing
will be an instance of Call
.
那是如果您不能更改其他类,并且如果您知道missing
将是Call
.
回答by Anton Bessonov
You can specify absract method getId() in CallNode and implement it in children. Then you an access it in your Checker.
您可以在 CallNode 中指定抽象方法 getId() 并在 children 中实现它。然后您可以在 Checker 中访问它。
public abstract class CallNode {
public abstract CallNode hasMissingCall();
public abstract int getId();
}
public class Checker{
private static CallNode rootExpected;
CallNode missing = rootExpected.hasMissingCall();
System.out.println( missing.getId() );
}
public class Call extends CallNode {
public int id; // better set private modifier
public int getId() {
return id;
}
...
}