您可以通过以下几种方式做到这一点:
extended
在课堂上参考你的MyObject
班级。在类中实例化MyObject
变量时extended
,将extended
.
像这样的东西:
class MyObject{
private base baseObj;
public MyObject(base baseObj){
this.baseObj = baseObj;
}
public void DoSomething()
{
//here I need to call method add from class base.
//use baseObj to call the methods
}
}
class base
{
protected final void add(){}
}
class extended extends base {
private MyObject pObject;
...
public extended(){
pObject = new MyObject(this);
}
{
pObject.DoSomething();
}
}
- 声明
base
类中的方法static
。这样你就可以调用方法而不需要base
类的实例。
像这样的东西:
class MyObject{
public void DoSomething()
{
//here I need to call method add from class base.
//call like this
base.add();
}
}
class base
{
protected static final void add(){}
}
class extended extends base {
private MyObject pObject;
...
public extended(){
pObject = new MyObject(this);
}
{
pObject.DoSomething();
}
}
还有一件事:这是题外话,但您可能想阅读Java Naming Conventions。类名以小写开头是您在命名约定中找不到的。