猿问

j2me父类中的某些public函数在子类中不可见?

public class Server extends Form implements Runnable, CommandListener {

public Server(MIDlet midlet)
{
...
}

Server.this.getHeight();// 1

class MessageArea extends CustomItem 
{

Server.this.getHeight();// 2
}

}

Form中的getHeight()
Overrides:
getHeight in class Displayable
在1处和在2处的语句编译器都报错,说是getHeight()不可见。getHeight()是public int 类型的,在1处或2处调用Form的append等一些函数是可以成功的。我在j2me的javadoc中看到这两函数的区别是getHeight()标注了Overrides: getHeight in class Displayable
,Displayable是Form父类的父类。在Form中可以调用getHeight(),为什么在Form的子类Server中getHeight()不可见呢

波斯汪
浏览 513回答 3
3回答

ABOUTYOU

我按照你的代码试了 完全没问题 不知道你那是什么情况 或许不是代码问题 是eclipse出问题了吧 试试 project>clean 一下工程试试. 再不行 换个别的模拟器的试试public Server(MyMid middlet) {super("你好");this.middlet = middlet;MessageArea area = new MessageArea("123");append(area);}class MessageArea extends CustomItem {protected MessageArea(String label) {super(label);Server.this.getHeight();//这里调用完全没问题}protected void paint(Graphics g, int width, int height) {g.setColor(0xff0000);g.fillRect(0, 0, width, height);}}

猛跑小猪

如果需要子类里也可见,请把方法定义为protected,而非public刚我看了下MIDP的源码,3个类继承关系Displayable->Screen->Form其中Form和Displayable中都有getHeight方法且都是public。但是,注意了Form中的getHeight()方法重写过了,和Displayable中的getHeight方法没有半毛钱关系。所以Form中用getHeight方法是用Form自己的,和Displayable中的getHeight没关系。据我记忆,子类本来就不能访问父类中的public对象。如果需要访问,请把对象定义为protected所以1处应该用super.getHeight();

qq_花开花谢_0

一下是API上截取的:getHeightpublic int getHeight()Returns the height in pixels of the displayable area available for items. This value is the height of the form that can be displayed without scrolling. The value may depend on how the device uses the screen and may be affected by the presence or absence of the ticker, title, or commands.Overrides:getHeight in class DisplayableReturns:the height of the displayable area of the Form in pixelsSince:MIDP 2.0
随时随地看视频慕课网APP

相关分类

Java
我要回答